lokesh341 commited on
Commit
29a2fcc
·
verified ·
1 Parent(s): 4a9aef9

Update menu.py

Browse files
Files changed (1) hide show
  1. menu.py +29 -26
menu.py CHANGED
@@ -24,19 +24,14 @@ def get_valid_video_path(item_name, video_url=None):
24
  Get valid video path for item with placeholder fallback
25
  Priority: 1. Video1__c from Salesforce 2. placeholder.mp4
26
  """
27
- # First try: Video1__c from Salesforce if provided
28
  if video_url:
29
- # If it's a complete URL (http/https)
30
  if video_url.startswith(('http://', 'https://')):
31
  return video_url
32
- # If it's a relative path (/videos/xxx.mp4)
33
  elif video_url.startswith('/'):
34
  return video_url
35
- # If it's a Salesforce File ID (starts with '069')
36
  elif video_url.startswith('069'):
37
  return f"https://biryanihub-dev-ed.develop.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
38
 
39
- # Final fallback: placeholder.mp4
40
  if not os.path.exists(PLACEHOLDER_PATH):
41
  with open(PLACEHOLDER_PATH, 'wb') as f:
42
  f.close()
@@ -78,42 +73,54 @@ def menu():
78
  cart_count_result = sf.query(cart_query)
79
  cart_item_count = cart_count_result.get('totalSize', 0)
80
 
81
- # Fetch all Menu_Item__c records
82
  menu_query = """
83
  SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
84
- Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c
 
85
  FROM Menu_Item__c
86
  """
87
- menu_result = sf.query_all(menu_query) # Use query_all to fetch all records
88
  food_items = menu_result.get('records', [])
89
 
90
- # Process menu items and add video paths
91
  for item in food_items:
92
  item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
93
  item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
94
- item['Section__c'] = item.get('Section__c') or "Others" # Default section
95
-
96
- # Fetch all Custom_Dish__c records from the last 7 days
 
 
 
 
 
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 and add video paths
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') or "Customized dish" # Default section
 
 
 
 
 
111
 
112
  # Merge all items
113
  all_items = food_items + custom_dishes
114
  ordered_menu = {section: [] for section in SECTION_ORDER}
115
 
116
- # Process best sellers (top 4 by Total_Ordered__c)
117
  best_sellers = sorted(all_items, key=lambda x: x['Total_Ordered__c'], reverse=True)
118
  if selected_category == "Veg":
119
  best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
@@ -122,7 +129,7 @@ def menu():
122
  ordered_menu["Best Sellers"] = best_sellers[:4]
123
 
124
  # Organize items into sections
125
- added_item_names = set() # To avoid duplicates
126
  for item in all_items:
127
  section = item['Section__c']
128
  if section not in ordered_menu:
@@ -131,7 +138,6 @@ def menu():
131
  if item['Name'] in added_item_names:
132
  continue
133
 
134
- # Apply category filter
135
  veg_nonveg = item.get("Veg_NonVeg__c", "both")
136
  if selected_category == "Veg" and veg_nonveg not in ["Veg", "both"]:
137
  continue
@@ -147,13 +153,13 @@ def menu():
147
 
148
  except Exception as e:
149
  print(f"Error fetching menu data: {str(e)}")
150
- # Fallback data
151
  ordered_menu = {section: [] for section in SECTION_ORDER}
152
  best_sellers = [
153
- {"Name": "Chicken Biryani", "Price__c": 12.99, "Description__c": "Popular Chicken Biryani", "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"},
154
- {"Name": "Paneer Butter Masala", "Price__c": 11.99, "Description__c": "Popular Paneer Butter Masala", "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"},
155
- {"Name": "Veg Manchurian", "Price__c": 9.99, "Description__c": "Popular Veg Manchurian", "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"},
156
- {"Name": "Prawn Fry", "Price__c": 14.99, "Description__c": "Popular Prawn Fry", "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"}
157
  ]
158
  ordered_menu["Best Sellers"] = best_sellers
159
  categories = ["All", "Veg", "Non veg"]
@@ -229,7 +235,6 @@ def add_to_cart():
229
  if not item_name or not item_price or not customer_email:
230
  return jsonify({"success": False, "error": "Item name, price, and user email are required."}), 400
231
 
232
- # Check if item already exists in cart
233
  query = f"""
234
  SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c, Price__c
235
  FROM Cart_Item__c
@@ -242,7 +247,6 @@ def add_to_cart():
242
  new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) if addons else "None"
243
 
244
  if cart_items:
245
- # Update existing cart item
246
  cart_item = cart_items[0]
247
  cart_item_id = cart_item['Id']
248
  existing_quantity = int(cart_item.get('Quantity__c', 0))
@@ -275,7 +279,6 @@ def add_to_cart():
275
  "Section__c": section
276
  })
277
  else:
278
- # Create new cart item
279
  total_price = item_price * quantity + addons_price
280
  sf.Cart_Item__c.create({
281
  "Name": item_name,
 
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
 
30
  elif video_url.startswith('/'):
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()
 
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 with additional fields
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
+ IngredientsInfo__c, NutritionalInfo__c, Allergens__c
81
  FROM Menu_Item__c
82
  """
83
+ menu_result = sf.query_all(menu_query) # Fetch all records
84
  food_items = menu_result.get('records', [])
85
 
86
+ # Process menu items
87
  for item in food_items:
88
  item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
89
  item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
90
+ item['Section__c'] = item.get('Section__c', "Others")
91
+ # Default values for new fields if null
92
+ item['Description__c'] = item.get('Description__c', "No description available")
93
+ item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
94
+ item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
95
+ item['Allergens__c'] = item.get('Allergens__c', "None listed")
96
+
97
+ # Fetch all Custom_Dish__c records with additional fields
98
  custom_dish_query = """
99
  SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
100
+ Veg_NonVeg__c, Section__c, Total_Ordered__c,
101
+ IngredientsInfo__c, NutritionalInfo__c, Allergens__c
102
  FROM Custom_Dish__c
103
  WHERE CreatedDate >= LAST_N_DAYS:7
104
  """
105
  custom_dish_result = sf.query_all(custom_dish_query)
106
  custom_dishes = custom_dish_result.get('records', [])
107
 
108
+ # Process custom dishes
109
  for item in custom_dishes:
110
  item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
111
  item['Video1__c'] = get_valid_video_path(item['Name'])
112
+ item['Section__c'] = item.get('Section__c', "Customized dish")
113
+ # Default values for new fields if null
114
+ item['Description__c'] = item.get('Description__c', "No description available")
115
+ item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
116
+ item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
117
+ item['Allergens__c'] = item.get('Allergens__c', "None listed")
118
 
119
  # Merge all items
120
  all_items = food_items + custom_dishes
121
  ordered_menu = {section: [] for section in SECTION_ORDER}
122
 
123
+ # Process best sellers
124
  best_sellers = sorted(all_items, key=lambda x: x['Total_Ordered__c'], reverse=True)
125
  if selected_category == "Veg":
126
  best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
 
129
  ordered_menu["Best Sellers"] = best_sellers[:4]
130
 
131
  # Organize items into sections
132
+ added_item_names = set()
133
  for item in all_items:
134
  section = item['Section__c']
135
  if section not in ordered_menu:
 
138
  if item['Name'] in added_item_names:
139
  continue
140
 
 
141
  veg_nonveg = item.get("Veg_NonVeg__c", "both")
142
  if selected_category == "Veg" and veg_nonveg not in ["Veg", "both"]:
143
  continue
 
153
 
154
  except Exception as e:
155
  print(f"Error fetching menu data: {str(e)}")
156
+ # Fallback data with all required fields
157
  ordered_menu = {section: [] for section in SECTION_ORDER}
158
  best_sellers = [
159
+ {"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"},
160
+ {"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"},
161
+ {"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"},
162
+ {"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"}
163
  ]
164
  ordered_menu["Best Sellers"] = best_sellers
165
  categories = ["All", "Veg", "Non veg"]
 
235
  if not item_name or not item_price or not customer_email:
236
  return jsonify({"success": False, "error": "Item name, price, and user email are required."}), 400
237
 
 
238
  query = f"""
239
  SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c, Price__c
240
  FROM Cart_Item__c
 
247
  new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) if addons else "None"
248
 
249
  if cart_items:
 
250
  cart_item = cart_items[0]
251
  cart_item_id = cart_item['Id']
252
  existing_quantity = int(cart_item.get('Quantity__c', 0))
 
279
  "Section__c": section
280
  })
281
  else:
 
282
  total_price = item_price * quantity + addons_price
283
  sf.Cart_Item__c.create({
284
  "Name": item_name,