Update menu.py
Browse files
menu.py
CHANGED
@@ -1,13 +1,7 @@
|
|
1 |
from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
|
2 |
import os
|
3 |
-
import base64
|
4 |
-
import logging
|
5 |
from salesforce import get_salesforce_connection
|
6 |
|
7 |
-
# Configure logging
|
8 |
-
logging.basicConfig(level=logging.DEBUG)
|
9 |
-
logger = logging.getLogger(__name__)
|
10 |
-
|
11 |
menu_blueprint = Blueprint('menu', __name__)
|
12 |
|
13 |
# Initialize Salesforce connection
|
@@ -23,7 +17,7 @@ SECTION_ORDER = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "C
|
|
23 |
if not os.path.exists(PLACEHOLDER_PATH):
|
24 |
with open(PLACEHOLDER_PATH, 'wb') as f:
|
25 |
f.close()
|
26 |
-
|
27 |
|
28 |
def get_valid_video_path(item_name, video_url=None):
|
29 |
"""Get valid video path for item with placeholder fallback."""
|
@@ -37,73 +31,24 @@ def get_valid_video_path(item_name, video_url=None):
|
|
37 |
if not os.path.exists(PLACEHOLDER_PATH):
|
38 |
with open(PLACEHOLDER_PATH, 'wb') as f:
|
39 |
f.close()
|
40 |
-
|
41 |
return f"/static/{PLACEHOLDER_VIDEO}"
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
selected_category = request.args.get("category", "All")
|
46 |
-
highlight_item = request.args.get("highlight") # Get item to highlight from query parameter
|
47 |
-
user_email = session.get('user_email')
|
48 |
-
|
49 |
-
# Handle user authentication
|
50 |
-
if not user_email:
|
51 |
-
user_email = request.args.get("email")
|
52 |
-
user_name = request.args.get("name")
|
53 |
-
if user_email and user_name:
|
54 |
-
session['user_email'] = user_email
|
55 |
-
session['user_name'] = user_name
|
56 |
-
else:
|
57 |
-
return redirect(url_for("login"))
|
58 |
-
else:
|
59 |
-
user_name = session.get('user_name')
|
60 |
-
|
61 |
-
first_letter = user_name[0].upper() if user_name else "A"
|
62 |
-
user_image = session.get('user_image')
|
63 |
-
|
64 |
-
# Check if Avatar__c field exists on Customer_Login__c
|
65 |
-
try:
|
66 |
-
describe_result = sf.Customer_Login__c.describe()
|
67 |
-
fields = [field['name'] for field in describe_result['fields']]
|
68 |
-
avatar_field_exists = 'Avatar__c' in fields
|
69 |
-
except Exception as e:
|
70 |
-
logger.error(f"Error describing Customer_Login__c object: {str(e)}")
|
71 |
-
avatar_field_exists = False
|
72 |
-
|
73 |
-
# Build the SOQL query dynamically based on field availability
|
74 |
-
query_fields = ["Id", "Referral__c", "Reward_Points__c"]
|
75 |
-
if avatar_field_exists:
|
76 |
-
query_fields.append("Avatar__c")
|
77 |
-
user_query = f"SELECT {', '.join(query_fields)} FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
78 |
-
|
79 |
# Fetch user referral and reward points
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
return redirect(url_for('login'))
|
85 |
-
except Exception as e:
|
86 |
-
logger.error(f"Error querying user data: {str(e)}")
|
87 |
-
return jsonify({"success": False, "error": "Failed to fetch user data from Salesforce"}), 500
|
88 |
-
|
89 |
-
user_record = user_result['records'][0]
|
90 |
-
user_id = user_record['Id']
|
91 |
-
referral_code = user_record.get('Referral__c', 'N/A')
|
92 |
-
reward_points = user_record.get('Reward_Points__c', 0)
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
session['user_image'] = user_record['Avatar__c']
|
97 |
-
user_image = session['user_image']
|
98 |
|
99 |
# Get cart item count
|
100 |
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
101 |
-
|
102 |
-
|
103 |
-
cart_item_count = cart_count_result.get('totalSize', 0)
|
104 |
-
except Exception as e:
|
105 |
-
logger.error(f"Error fetching cart item count: {str(e)}")
|
106 |
-
cart_item_count = 0
|
107 |
|
108 |
# Fetch all Menu_Item__c records with required fields
|
109 |
menu_query = """
|
@@ -112,12 +57,8 @@ def menu():
|
|
112 |
IngredientsInfo__c, NutritionalInfo__c, Allergens__c
|
113 |
FROM Menu_Item__c
|
114 |
"""
|
115 |
-
|
116 |
-
|
117 |
-
food_items = menu_result.get('records', [])
|
118 |
-
except Exception as e:
|
119 |
-
logger.error(f"Error fetching menu items: {str(e)}")
|
120 |
-
food_items = []
|
121 |
|
122 |
# Process menu items
|
123 |
for item in food_items:
|
@@ -128,7 +69,7 @@ def menu():
|
|
128 |
item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
|
129 |
item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
|
130 |
item['Allergens__c'] = item.get('Allergens__c', "None listed")
|
131 |
-
item['is_menu_item'] = True
|
132 |
|
133 |
# Fetch all Custom_Dish__c records with only existing fields
|
134 |
custom_dish_query = """
|
@@ -137,12 +78,8 @@ def menu():
|
|
137 |
FROM Custom_Dish__c
|
138 |
WHERE CreatedDate >= LAST_N_DAYS:7
|
139 |
"""
|
140 |
-
|
141 |
-
|
142 |
-
custom_dishes = custom_dish_result.get('records', [])
|
143 |
-
except Exception as e:
|
144 |
-
logger.error(f"Error fetching custom dishes: {str(e)}")
|
145 |
-
custom_dishes = []
|
146 |
|
147 |
# Process custom dishes
|
148 |
for item in custom_dishes:
|
@@ -150,7 +87,7 @@ def menu():
|
|
150 |
item['Video1__c'] = get_valid_video_path(item['Name'])
|
151 |
item['Section__c'] = item.get('Section__c', "Customized dish")
|
152 |
item['Description__c'] = item.get('Description__c', "No description available")
|
153 |
-
item['is_menu_item'] = False
|
154 |
|
155 |
# Merge all items
|
156 |
all_items = food_items + custom_dishes
|
@@ -185,6 +122,32 @@ def menu():
|
|
185 |
|
186 |
# Remove empty sections
|
187 |
ordered_menu = {section: items for section, items in ordered_menu.items() if items}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
categories = ["All", "Veg", "Non veg"]
|
189 |
|
190 |
return render_template(
|
@@ -196,181 +159,33 @@ def menu():
|
|
196 |
reward_points=reward_points,
|
197 |
user_name=user_name,
|
198 |
first_letter=first_letter,
|
199 |
-
cart_item_count=cart_item_count
|
200 |
-
user_image=user_image,
|
201 |
-
user_id=user_id,
|
202 |
-
highlight_item=highlight_item # Pass the item to highlight to the template
|
203 |
)
|
204 |
|
205 |
-
@menu_blueprint.route(
|
206 |
def search():
|
|
|
207 |
user_email = session.get('user_email')
|
|
|
|
|
208 |
if not user_email:
|
209 |
return redirect(url_for("login"))
|
210 |
-
|
211 |
user_name = session.get('user_name')
|
212 |
first_letter = user_name[0].upper() if user_name else "A"
|
213 |
-
user_image = session.get('user_image')
|
214 |
-
|
215 |
-
# Get cart item count
|
216 |
-
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
217 |
-
try:
|
218 |
-
cart_count_result = sf.query(cart_query)
|
219 |
-
cart_item_count = cart_count_result.get('totalSize', 0)
|
220 |
-
except Exception as e:
|
221 |
-
logger.error(f"Error fetching cart item count: {str(e)}")
|
222 |
-
cart_item_count = 0
|
223 |
-
|
224 |
-
# Fetch all Menu_Item__c records with required fields
|
225 |
-
menu_query = """
|
226 |
-
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
227 |
-
Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c,
|
228 |
-
IngredientsInfo__c, NutritionalInfo__c, Allergens__c
|
229 |
-
FROM Menu_Item__c
|
230 |
-
"""
|
231 |
-
try:
|
232 |
-
menu_result = sf.query_all(menu_query)
|
233 |
-
food_items = menu_result.get('records', [])
|
234 |
-
except Exception as e:
|
235 |
-
logger.error(f"Error fetching menu items: {str(e)}")
|
236 |
-
food_items = []
|
237 |
-
|
238 |
-
for item in food_items:
|
239 |
-
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
|
240 |
-
item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
|
241 |
-
item['Section__c'] = item.get('Section__c', "Others")
|
242 |
-
item['Description__c'] = item.get('Description__c', "No description available")
|
243 |
-
item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
|
244 |
-
item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
|
245 |
-
item['Allergens__c'] = item.get('Allergens__c', "None listed")
|
246 |
-
item['is_menu_item'] = True
|
247 |
|
248 |
-
# Fetch
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
FROM Custom_Dish__c
|
253 |
-
WHERE CreatedDate >= LAST_N_DAYS:7
|
254 |
-
"""
|
255 |
-
try:
|
256 |
-
custom_dish_result = sf.query_all(custom_dish_query)
|
257 |
-
custom_dishes = custom_dish_result.get('records', [])
|
258 |
-
except Exception as e:
|
259 |
-
logger.error(f"Error fetching custom dishes: {str(e)}")
|
260 |
-
custom_dishes = []
|
261 |
-
|
262 |
-
for item in custom_dishes:
|
263 |
-
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
|
264 |
-
item['Video1__c'] = get_valid_video_path(item['Name'])
|
265 |
-
item['Section__c'] = item.get('Section__c', "Customized dish")
|
266 |
-
item['Description__c'] = item.get('Description__c', "No description available")
|
267 |
-
item['is_menu_item'] = False
|
268 |
-
|
269 |
-
all_items = food_items + custom_dishes
|
270 |
|
271 |
return render_template(
|
272 |
"search.html",
|
273 |
-
|
274 |
user_name=user_name,
|
275 |
first_letter=first_letter,
|
276 |
-
cart_item_count=cart_item_count
|
277 |
-
user_image=user_image
|
278 |
)
|
279 |
|
280 |
-
@menu_blueprint.route('/upload_avatar', methods=['POST'])
|
281 |
-
def upload_avatar():
|
282 |
-
try:
|
283 |
-
data = request.get_json()
|
284 |
-
if not data or 'image' not in data:
|
285 |
-
logger.error("No image data provided in request")
|
286 |
-
return jsonify({'success': False, 'error': 'No image data provided'}), 400
|
287 |
-
|
288 |
-
image_data = data['image']
|
289 |
-
logger.debug(f"Received image data with length: {len(image_data)}")
|
290 |
-
|
291 |
-
# Validate base64 image
|
292 |
-
if not image_data.startswith('data:image/'):
|
293 |
-
logger.error("Invalid image format: does not start with 'data:image/'")
|
294 |
-
return jsonify({'success': False, 'error': 'Invalid image format'}), 400
|
295 |
-
|
296 |
-
# Check size limit (~1.5MB for base64, roughly 1MB actual image)
|
297 |
-
if len(image_data) > 2_000_000:
|
298 |
-
logger.error(f"Image too large: {len(image_data)} characters (max 2,000,000)")
|
299 |
-
return jsonify({'success': False, 'error': 'Image too large (max ~1.5MB)'}), 400
|
300 |
-
|
301 |
-
# Validate base64 decoding
|
302 |
-
try:
|
303 |
-
base64_string = image_data.split(',')[1]
|
304 |
-
base64.b64decode(base64_string)
|
305 |
-
except Exception as e:
|
306 |
-
logger.error(f"Invalid base64 data: {str(e)}")
|
307 |
-
return jsonify({'success': False, 'error': 'Invalid base64 data'}), 400
|
308 |
-
|
309 |
-
# Store in session
|
310 |
-
session['user_image'] = image_data
|
311 |
-
logger.info("Image stored in session successfully")
|
312 |
-
|
313 |
-
# Store in Salesforce (if Avatar__c field exists)
|
314 |
-
user_email = session.get('user_email')
|
315 |
-
if user_email:
|
316 |
-
try:
|
317 |
-
user_query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
318 |
-
user_result = sf.query(user_query)
|
319 |
-
if user_result.get('records'):
|
320 |
-
user_id = user_result['records'][0]['Id']
|
321 |
-
describe_result = sf.Customer_Login__c.describe()
|
322 |
-
fields = [field['name'] for field in describe_result['fields']]
|
323 |
-
if 'Avatar__c' in fields:
|
324 |
-
sf.Customer_Login__c.update(user_id, {'Avatar__c': image_data})
|
325 |
-
logger.info(f"Image stored in Salesforce for user {user_email}")
|
326 |
-
else:
|
327 |
-
logger.warning("Avatar__c field does not exist; skipping Salesforce update")
|
328 |
-
else:
|
329 |
-
logger.warning(f"User not found in Salesforce: {user_email}")
|
330 |
-
except Exception as e:
|
331 |
-
logger.error(f"Failed to store image in Salesforce: {str(e)}")
|
332 |
-
|
333 |
-
return jsonify({'success': True, 'image': image_data})
|
334 |
-
|
335 |
-
except Exception as e:
|
336 |
-
logger.error(f"Error in upload_avatar: {str(e)}", exc_info=True)
|
337 |
-
return jsonify({'success': False, 'error': f'Server error: {str(e)}'}), 500
|
338 |
-
|
339 |
-
@menu_blueprint.route('/delete_avatar', methods=['POST'])
|
340 |
-
def delete_avatar():
|
341 |
-
try:
|
342 |
-
user_email = session.get('user_email')
|
343 |
-
if not user_email:
|
344 |
-
logger.error("No user email in session")
|
345 |
-
return jsonify({'success': False, 'error': 'User not authenticated'}), 401
|
346 |
-
|
347 |
-
if 'user_image' in session:
|
348 |
-
session.pop('user_image', None)
|
349 |
-
logger.info("Image removed from session")
|
350 |
-
|
351 |
-
try:
|
352 |
-
user_query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
353 |
-
user_result = sf.query(user_query)
|
354 |
-
if user_result.get('records'):
|
355 |
-
user_id = user_result['records'][0]['Id']
|
356 |
-
describe_result = sf.Customer_Login__c.describe()
|
357 |
-
fields = [field['name'] for field in describe_result['fields']]
|
358 |
-
if 'Avatar__c' in fields:
|
359 |
-
sf.Customer_Login__c.update(user_id, {'Avatar__c': None})
|
360 |
-
logger.info(f"Image removed from Salesforce for user {user_email}")
|
361 |
-
else:
|
362 |
-
logger.warning("Avatar__c field does not exist; skipping Salesforce update")
|
363 |
-
else:
|
364 |
-
logger.warning(f"User not found in Salesforce: {user_email}")
|
365 |
-
except Exception as e:
|
366 |
-
logger.error(f"Failed to remove image from Salesforce: {str(e)}")
|
367 |
-
|
368 |
-
return jsonify({'success': True})
|
369 |
-
|
370 |
-
except Exception as e:
|
371 |
-
logger.error(f"Error in delete_avatar: {str(e)}", exc_info=True)
|
372 |
-
return jsonify({'success': False, 'error': f'Server error: {str(e)}'}), 500
|
373 |
-
|
374 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|
375 |
def get_addons():
|
376 |
item_name = request.args.get('item_name')
|
@@ -407,7 +222,7 @@ def get_addons():
|
|
407 |
return jsonify({"success": True, "addons": formatted_addons})
|
408 |
|
409 |
except Exception as e:
|
410 |
-
|
411 |
return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
|
412 |
|
413 |
@menu_blueprint.route('/cart/add', methods=['POST'])
|
@@ -425,13 +240,8 @@ def add_to_cart():
|
|
425 |
customer_email = session.get('user_email')
|
426 |
|
427 |
if not item_name or not item_price or not customer_email:
|
428 |
-
logger.error(f"Missing required fields: item_name={item_name}, item_price={item_price}, customer_email={customer_email}")
|
429 |
return jsonify({"success": False, "error": "Item name, price, and user email are required."}), 400
|
430 |
|
431 |
-
# Sanitize inputs to prevent SOQL injection
|
432 |
-
item_name = item_name.replace("'", "''")
|
433 |
-
customer_email = customer_email.replace("'", "''")
|
434 |
-
|
435 |
query = f"""
|
436 |
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c, Price__c
|
437 |
FROM Cart_Item__c
|
@@ -457,7 +267,7 @@ def add_to_cart():
|
|
457 |
|
458 |
combined_instructions = existing_instructions
|
459 |
if instructions:
|
460 |
-
combined_instructions = f"{
|
461 |
|
462 |
combined_addons_list = combined_addons.split("; ")
|
463 |
combined_addons_price = sum(
|
@@ -496,14 +306,10 @@ def add_to_cart():
|
|
496 |
cart_result = sf.query_all(cart_query)
|
497 |
cart = [{"itemName": item["Name"], "quantity": item["Quantity__c"]} for item in cart_result.get("records", [])]
|
498 |
|
499 |
-
logger.info(f"Item '{item_name}' added to cart for {customer_email}")
|
500 |
return jsonify({"success": True, "message": "Item added to cart successfully.", "cart": cart})
|
501 |
|
502 |
except ValueError as e:
|
503 |
-
logger.error(f"Invalid data format: {str(e)}")
|
504 |
return jsonify({"success": False, "error": f"Invalid data format: {str(e)}"}), 400
|
505 |
except Exception as e:
|
506 |
-
|
507 |
-
return jsonify({"success": False, "error":
|
508 |
-
|
509 |
-
# Note: Ensure 'login' route exists in your app or adjust redirect accordingly
|
|
|
1 |
from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
|
2 |
import os
|
|
|
|
|
3 |
from salesforce import get_salesforce_connection
|
4 |
|
|
|
|
|
|
|
|
|
5 |
menu_blueprint = Blueprint('menu', __name__)
|
6 |
|
7 |
# Initialize Salesforce connection
|
|
|
17 |
if not os.path.exists(PLACEHOLDER_PATH):
|
18 |
with open(PLACEHOLDER_PATH, 'wb') as f:
|
19 |
f.close()
|
20 |
+
print(f"Created placeholder video at {PLACEHOLDER_PATH}")
|
21 |
|
22 |
def get_valid_video_path(item_name, video_url=None):
|
23 |
"""Get valid video path for item with placeholder fallback."""
|
|
|
31 |
if not os.path.exists(PLACEHOLDER_PATH):
|
32 |
with open(PLACEHOLDER_PATH, 'wb') as f:
|
33 |
f.close()
|
34 |
+
print(f"Created missing placeholder video at {PLACEHOLDER_PATH}")
|
35 |
return f"/static/{PLACEHOLDER_VIDEO}"
|
36 |
|
37 |
+
def fetch_menu_data(selected_category, user_email):
|
38 |
+
"""Common function to fetch and process menu data."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
# Fetch user referral and reward points
|
40 |
+
user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
41 |
+
user_result = sf.query(user_query)
|
42 |
+
if not user_result.get('records'):
|
43 |
+
return None, None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
|
46 |
+
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
|
|
|
|
|
47 |
|
48 |
# Get cart item count
|
49 |
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
50 |
+
cart_count_result = sf.query(cart_query)
|
51 |
+
cart_item_count = cart_count_result.get('totalSize', 0)
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Fetch all Menu_Item__c records with required fields
|
54 |
menu_query = """
|
|
|
57 |
IngredientsInfo__c, NutritionalInfo__c, Allergens__c
|
58 |
FROM Menu_Item__c
|
59 |
"""
|
60 |
+
menu_result = sf.query_all(menu_query)
|
61 |
+
food_items = menu_result.get('records', [])
|
|
|
|
|
|
|
|
|
62 |
|
63 |
# Process menu items
|
64 |
for item in food_items:
|
|
|
69 |
item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
|
70 |
item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
|
71 |
item['Allergens__c'] = item.get('Allergens__c', "None listed")
|
72 |
+
item['is_menu_item'] = True # Flag to identify Menu_Item__c records
|
73 |
|
74 |
# Fetch all Custom_Dish__c records with only existing fields
|
75 |
custom_dish_query = """
|
|
|
78 |
FROM Custom_Dish__c
|
79 |
WHERE CreatedDate >= LAST_N_DAYS:7
|
80 |
"""
|
81 |
+
custom_dish_result = sf.query_all(custom_dish_query)
|
82 |
+
custom_dishes = custom_dish_result.get('records', [])
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# Process custom dishes
|
85 |
for item in custom_dishes:
|
|
|
87 |
item['Video1__c'] = get_valid_video_path(item['Name'])
|
88 |
item['Section__c'] = item.get('Section__c', "Customized dish")
|
89 |
item['Description__c'] = item.get('Description__c', "No description available")
|
90 |
+
item['is_menu_item'] = False # Flag to identify Custom_Dish__c records
|
91 |
|
92 |
# Merge all items
|
93 |
all_items = food_items + custom_dishes
|
|
|
122 |
|
123 |
# Remove empty sections
|
124 |
ordered_menu = {section: items for section, items in ordered_menu.items() if items}
|
125 |
+
return ordered_menu, referral_code, reward_points, cart_item_count
|
126 |
+
|
127 |
+
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
128 |
+
def menu():
|
129 |
+
selected_category = request.args.get("category", "All")
|
130 |
+
user_email = session.get('user_email')
|
131 |
+
|
132 |
+
# Handle user authentication
|
133 |
+
if not user_email:
|
134 |
+
user_email = request.args.get("email")
|
135 |
+
user_name = request.args.get("name")
|
136 |
+
if user_email and user_name:
|
137 |
+
session['user_email'] = user_email
|
138 |
+
session['user_name'] = user_name
|
139 |
+
else:
|
140 |
+
return redirect(url_for("login"))
|
141 |
+
else:
|
142 |
+
user_name = session.get('user_name')
|
143 |
+
|
144 |
+
first_letter = user_name[0].upper() if user_name else "A"
|
145 |
+
|
146 |
+
# Fetch menu data
|
147 |
+
ordered_menu, referral_code, reward_points, cart_item_count = fetch_menu_data(selected_category, user_email)
|
148 |
+
if ordered_menu is None:
|
149 |
+
return redirect(url_for('login'))
|
150 |
+
|
151 |
categories = ["All", "Veg", "Non veg"]
|
152 |
|
153 |
return render_template(
|
|
|
159 |
reward_points=reward_points,
|
160 |
user_name=user_name,
|
161 |
first_letter=first_letter,
|
162 |
+
cart_item_count=cart_item_count
|
|
|
|
|
|
|
163 |
)
|
164 |
|
165 |
+
@menu_blueprint.route("/search", methods=["GET"])
|
166 |
def search():
|
167 |
+
selected_category = request.args.get("category", "All")
|
168 |
user_email = session.get('user_email')
|
169 |
+
|
170 |
+
# Handle user authentication
|
171 |
if not user_email:
|
172 |
return redirect(url_for("login"))
|
|
|
173 |
user_name = session.get('user_name')
|
174 |
first_letter = user_name[0].upper() if user_name else "A"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
+
# Fetch menu data
|
177 |
+
ordered_menu, referral_code, reward_points, cart_item_count = fetch_menu_data(selected_category, user_email)
|
178 |
+
if ordered_menu is None:
|
179 |
+
return redirect(url_for('login'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
return render_template(
|
182 |
"search.html",
|
183 |
+
ordered_menu=ordered_menu,
|
184 |
user_name=user_name,
|
185 |
first_letter=first_letter,
|
186 |
+
cart_item_count=cart_item_count
|
|
|
187 |
)
|
188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|
190 |
def get_addons():
|
191 |
item_name = request.args.get('item_name')
|
|
|
222 |
return jsonify({"success": True, "addons": formatted_addons})
|
223 |
|
224 |
except Exception as e:
|
225 |
+
print(f"Error fetching addons: {str(e)}")
|
226 |
return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
|
227 |
|
228 |
@menu_blueprint.route('/cart/add', methods=['POST'])
|
|
|
240 |
customer_email = session.get('user_email')
|
241 |
|
242 |
if not item_name or not item_price or not customer_email:
|
|
|
243 |
return jsonify({"success": False, "error": "Item name, price, and user email are required."}), 400
|
244 |
|
|
|
|
|
|
|
|
|
245 |
query = f"""
|
246 |
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c, Price__c
|
247 |
FROM Cart_Item__c
|
|
|
267 |
|
268 |
combined_instructions = existing_instructions
|
269 |
if instructions:
|
270 |
+
combined_instructions = f"{existing_instructions} | {instructions}".strip(" | ")
|
271 |
|
272 |
combined_addons_list = combined_addons.split("; ")
|
273 |
combined_addons_price = sum(
|
|
|
306 |
cart_result = sf.query_all(cart_query)
|
307 |
cart = [{"itemName": item["Name"], "quantity": item["Quantity__c"]} for item in cart_result.get("records", [])]
|
308 |
|
|
|
309 |
return jsonify({"success": True, "message": "Item added to cart successfully.", "cart": cart})
|
310 |
|
311 |
except ValueError as e:
|
|
|
312 |
return jsonify({"success": False, "error": f"Invalid data format: {str(e)}"}), 400
|
313 |
except Exception as e:
|
314 |
+
print(f"Error adding item to cart: {str(e)}")
|
315 |
+
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|
|
|
|