Update menu.py
Browse files
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 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
if
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|