lokesh341 commited on
Commit
a35e901
·
verified ·
1 Parent(s): add8be8

Update menu.py

Browse files
Files changed (1) hide show
  1. menu.py +97 -114
menu.py CHANGED
@@ -20,10 +20,7 @@ if not os.path.exists(PLACEHOLDER_PATH):
20
  print(f"Created placeholder video at {PLACEHOLDER_PATH}")
21
 
22
  def get_valid_video_path(item_name, video_url=None):
23
- """
24
- Get valid video path for item with placeholder fallback
25
- Priority: 1. Video1__c from Salesforce 2. placeholder.mp4
26
- """
27
  if video_url:
28
  if video_url.startswith(('http://', 'https://')):
29
  return video_url
@@ -31,12 +28,10 @@ def get_valid_video_path(item_name, video_url=None):
31
  return video_url
32
  elif video_url.startswith('069'):
33
  return f"https://biryanihub-dev-ed.develop.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
34
-
35
  if not os.path.exists(PLACEHOLDER_PATH):
36
  with open(PLACEHOLDER_PATH, 'wb') as f:
37
  f.close()
38
  print(f"Created missing placeholder video at {PLACEHOLDER_PATH}")
39
-
40
  return f"/static/{PLACEHOLDER_VIDEO}"
41
 
42
  @menu_blueprint.route("/menu", methods=["GET", "POST"])
@@ -58,112 +53,95 @@ def menu():
58
 
59
  first_letter = user_name[0].upper() if user_name else "A"
60
 
61
- try:
62
- # Fetch user referral and reward points
63
- user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
64
- user_result = sf.query(user_query)
65
- if not user_result.get('records'):
66
- return redirect(url_for('login'))
67
-
68
- referral_code = user_result['records'][0].get('Referral__c', 'N/A')
69
- reward_points = user_result['records'][0].get('Reward_Points__c', 0)
70
-
71
- # Get cart item count
72
- cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
73
- cart_count_result = sf.query(cart_query)
74
- cart_item_count = cart_count_result.get('totalSize', 0)
75
-
76
- # Fetch all Menu_Item__c records
77
- menu_query = """
78
- SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
79
- Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c
80
- FROM Menu_Item__c
81
- """
82
- menu_result = sf.query_all(menu_query) # Fetch all records
83
- food_items = menu_result.get('records', [])
84
-
85
- # Process menu items
86
- for item in food_items:
87
- item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
88
- item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
89
- item['Section__c'] = item.get('Section__c', "Others")
90
- item['Description__c'] = item.get('Description__c', "No description available")
91
- # Placeholder for missing fields until created in Salesforce
92
- item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
93
- item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
94
- item['Allergens__c'] = item.get('Allergens__c', "None listed")
95
-
96
- # Fetch all Custom_Dish__c records
97
- custom_dish_query = """
98
- SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
99
- Veg_NonVeg__c, Section__c, Total_Ordered__c
100
- FROM Custom_Dish__c
101
- WHERE CreatedDate >= LAST_N_DAYS:7
102
- """
103
- custom_dish_result = sf.query_all(custom_dish_query)
104
- custom_dishes = custom_dish_result.get('records', [])
105
-
106
- # Process custom dishes
107
- for item in custom_dishes:
108
- item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
109
- item['Video1__c'] = get_valid_video_path(item['Name'])
110
- item['Section__c'] = item.get('Section__c', "Customized dish")
111
- item['Description__c'] = item.get('Description__c', "No description available")
112
- # Placeholder for missing fields until created in Salesforce
113
- item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
114
- item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
115
- item['Allergens__c'] = item.get('Allergens__c', "None listed")
116
-
117
- # Merge all items
118
- all_items = food_items + custom_dishes
119
- ordered_menu = {section: [] for section in SECTION_ORDER}
120
-
121
- # Process best sellers
122
- best_sellers = sorted(all_items, key=lambda x: x['Total_Ordered__c'], reverse=True)
123
- if selected_category == "Veg":
124
- best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
125
- elif selected_category == "Non veg":
126
- best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
127
- ordered_menu["Best Sellers"] = best_sellers[:4]
128
-
129
- # Organize items into sections
130
- added_item_names = set()
131
- for item in all_items:
132
- section = item['Section__c']
133
- if section not in ordered_menu:
134
- ordered_menu[section] = []
135
-
136
- if item['Name'] in added_item_names:
137
- continue
138
-
139
- veg_nonveg = item.get("Veg_NonVeg__c", "both")
140
- if selected_category == "Veg" and veg_nonveg not in ["Veg", "both"]:
141
- continue
142
- if selected_category == "Non veg" and veg_nonveg not in ["Non veg", "both"]:
143
- continue
144
-
145
- ordered_menu[section].append(item)
146
- added_item_names.add(item['Name'])
147
-
148
- # Remove empty sections
149
- ordered_menu = {section: items for section, items in ordered_menu.items() if items}
150
- categories = ["All", "Veg", "Non veg"]
151
-
152
- except Exception as e:
153
- print(f"Error fetching menu data: {str(e)}")
154
- # Fallback data with all required fields
155
- ordered_menu = {section: [] for section in SECTION_ORDER}
156
- best_sellers = [
157
- {"Name": "Chicken Biryani", "Price__c": 12.99, "Description__c": "Spicy chicken with rice", "Image1__c": "/static/placeholder.jpg", "Video1__c": get_valid_video_path("Chicken Biryani"), "Total_Ordered__c": 100, "Veg_NonVeg__c": "Non veg", "Section__c": "Best Sellers", "IngredientsInfo__c": "Chicken, rice, spices", "NutritionalInfo__c": "600 cal, 30g protein", "Allergens__c": "None"},
158
- {"Name": "Paneer Butter Masala", "Price__c": 11.99, "Description__c": "Creamy paneer curry", "Image1__c": "/static/placeholder.jpg", "Video1__c": get_valid_video_path("Paneer Butter Masala"), "Total_Ordered__c": 90, "Veg_NonVeg__c": "Veg", "Section__c": "Best Sellers", "IngredientsInfo__c": "Paneer, cream, tomatoes", "NutritionalInfo__c": "700 cal, 20g protein", "Allergens__c": "Dairy"},
159
- {"Name": "Veg Manchurian", "Price__c": 9.99, "Description__c": "Indo-Chinese veggie dish", "Image1__c": "/static/placeholder.jpg", "Video1__c": get_valid_video_path("Veg Manchurian"), "Total_Ordered__c": 80, "Veg_NonVeg__c": "Veg", "Section__c": "Best Sellers", "IngredientsInfo__c": "Vegetables, soy sauce", "NutritionalInfo__c": "400 cal, 10g protein", "Allergens__c": "Soy"},
160
- {"Name": "Prawn Fry", "Price__c": 14.99, "Description__c": "Crispy fried prawns", "Image1__c": "/static/placeholder.jpg", "Video1__c": get_valid_video_path("Prawn Fry"), "Total_Ordered__c": 70, "Veg_NonVeg__c": "Non veg", "Section__c": "Best Sellers", "IngredientsInfo__c": "Prawns, spices", "NutritionalInfo__c": "500 cal, 25g protein", "Allergens__c": "Shellfish"}
161
- ]
162
- ordered_menu["Best Sellers"] = best_sellers
163
- categories = ["All", "Veg", "Non veg"]
164
- referral_code = 'N/A'
165
- reward_points = 0
166
- cart_item_count = 0
167
 
168
  return render_template(
169
  "menu.html",
@@ -292,10 +270,15 @@ def add_to_cart():
292
  "Section__c": section
293
  })
294
 
295
- return jsonify({"success": True, "message": "Item added to cart successfully."})
 
 
 
 
 
296
 
297
  except ValueError as e:
298
  return jsonify({"success": False, "error": f"Invalid data format: {str(e)}"}), 400
299
  except Exception as e:
300
  print(f"Error adding item to cart: {str(e)}")
301
- return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
 
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."""
 
 
 
24
  if video_url:
25
  if video_url.startswith(('http://', 'https://')):
26
  return video_url
 
28
  return video_url
29
  elif video_url.startswith('069'):
30
  return f"https://biryanihub-dev-ed.develop.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
 
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
  @menu_blueprint.route("/menu", methods=["GET", "POST"])
 
53
 
54
  first_letter = user_name[0].upper() if user_name else "A"
55
 
56
+ # Fetch user referral and reward points
57
+ user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
58
+ user_result = sf.query(user_query)
59
+ if not user_result.get('records'):
60
+ return redirect(url_for('login'))
61
+
62
+ referral_code = user_result['records'][0].get('Referral__c', 'N/A')
63
+ reward_points = user_result['records'][0].get('Reward_Points__c', 0)
64
+
65
+ # Get cart item count
66
+ cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
67
+ cart_count_result = sf.query(cart_query)
68
+ cart_item_count = cart_count_result.get('totalSize', 0)
69
+
70
+ # Fetch all Menu_Item__c records with all required fields
71
+ menu_query = """
72
+ SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
73
+ Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c,
74
+ IngredientsInfo__c, NutritionalInfo__c, Allergens__c
75
+ FROM Menu_Item__c
76
+ """
77
+ menu_result = sf.query_all(menu_query)
78
+ food_items = menu_result.get('records', [])
79
+
80
+ # Process menu items
81
+ for item in food_items:
82
+ item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
83
+ item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
84
+ item['Section__c'] = item.get('Section__c', "Others")
85
+ item['Description__c'] = item.get('Description__c', "No description available")
86
+ item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
87
+ item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
88
+ item['Allergens__c'] = item.get('Allergens__c', "None listed")
89
+
90
+ # Fetch all Custom_Dish__c records with all required fields
91
+ custom_dish_query = """
92
+ SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
93
+ Veg_NonVeg__c, Section__c, Total_Ordered__c,
94
+ IngredientsInfo__c, NutritionalInfo__c, Allergens__c
95
+ FROM Custom_Dish__c
96
+ WHERE CreatedDate >= LAST_N_DAYS:7
97
+ """
98
+ custom_dish_result = sf.query_all(custom_dish_query)
99
+ custom_dishes = custom_dish_result.get('records', [])
100
+
101
+ # Process custom dishes
102
+ for item in custom_dishes:
103
+ item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
104
+ item['Video1__c'] = get_valid_video_path(item['Name'])
105
+ item['Section__c'] = item.get('Section__c', "Customized dish")
106
+ item['Description__c'] = item.get('Description__c', "No description available")
107
+ item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
108
+ item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
109
+ item['Allergens__c'] = item.get('Allergens__c', "None listed")
110
+
111
+ # Merge all items
112
+ all_items = food_items + custom_dishes
113
+ ordered_menu = {section: [] for section in SECTION_ORDER}
114
+
115
+ # Process best sellers
116
+ best_sellers = sorted(all_items, key=lambda x: x['Total_Ordered__c'], reverse=True)
117
+ if selected_category == "Veg":
118
+ best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
119
+ elif selected_category == "Non veg":
120
+ best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
121
+ ordered_menu["Best Sellers"] = best_sellers[:4]
122
+
123
+ # Organize items into sections
124
+ added_item_names = set()
125
+ for item in all_items:
126
+ section = item['Section__c']
127
+ if section not in ordered_menu:
128
+ ordered_menu[section] = []
129
+
130
+ if item['Name'] in added_item_names:
131
+ continue
132
+
133
+ veg_nonveg = item.get("Veg_NonVeg__c", "both")
134
+ if selected_category == "Veg" and veg_nonveg not in ["Veg", "both"]:
135
+ continue
136
+ if selected_category == "Non veg" and veg_nonveg not in ["Non veg", "both"]:
137
+ continue
138
+
139
+ ordered_menu[section].append(item)
140
+ added_item_names.add(item['Name'])
141
+
142
+ # Remove empty sections
143
+ ordered_menu = {section: items for section, items in ordered_menu.items() if items}
144
+ categories = ["All", "Veg", "Non veg"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  return render_template(
147
  "menu.html",
 
270
  "Section__c": section
271
  })
272
 
273
+ # Fetch updated cart for UI update
274
+ cart_query = f"SELECT Name, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{customer_email}'"
275
+ cart_result = sf.query_all(cart_query)
276
+ cart = [{"itemName": item["Name"], "quantity": item["Quantity__c"]} for item in cart_result.get("records", [])]
277
+
278
+ return jsonify({"success": True, "message": "Item added to cart successfully.", "cart": cart})
279
 
280
  except ValueError as e:
281
  return jsonify({"success": False, "error": f"Invalid data format: {str(e)}"}), 400
282
  except Exception as e:
283
  print(f"Error adding item to cart: {str(e)}")
284
+ return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500