Update menu.py
Browse files
menu.py
CHANGED
@@ -1,6 +1,6 @@
|
|
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 |
|
@@ -15,23 +15,31 @@ SECTION_ORDER = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "C
|
|
15 |
|
16 |
# Create placeholder video at startup if it doesn't exist
|
17 |
if not os.path.exists(PLACEHOLDER_PATH):
|
18 |
-
|
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 |
-
"""
|
|
|
|
|
|
|
|
|
24 |
if video_url:
|
|
|
25 |
if video_url.startswith(('http://', 'https://')):
|
26 |
return video_url
|
|
|
27 |
elif video_url.startswith('/'):
|
28 |
return video_url
|
|
|
29 |
elif video_url.startswith('069'):
|
30 |
-
return f"https://
|
|
|
|
|
31 |
if not os.path.exists(PLACEHOLDER_PATH):
|
32 |
-
|
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"])
|
@@ -39,11 +47,11 @@ def menu():
|
|
39 |
selected_category = request.args.get("category", "All")
|
40 |
user_email = session.get('user_email')
|
41 |
|
42 |
-
# Handle user authentication
|
43 |
if not user_email:
|
44 |
user_email = request.args.get("email")
|
45 |
user_name = request.args.get("name")
|
46 |
-
|
|
|
47 |
session['user_email'] = user_email
|
48 |
session['user_name'] = user_name
|
49 |
else:
|
@@ -53,95 +61,122 @@ def menu():
|
|
53 |
|
54 |
first_letter = user_name[0].upper() if user_name else "A"
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
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 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
return render_template(
|
147 |
"menu.html",
|
@@ -157,8 +192,8 @@ def menu():
|
|
157 |
|
158 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|
159 |
def get_addons():
|
160 |
-
item_name = request.args.get('item_name')
|
161 |
-
item_section = request.args.get('item_section')
|
162 |
|
163 |
if not item_name or not item_section:
|
164 |
return jsonify({"success": False, "error": "Item name and section are required."}), 400
|
@@ -169,7 +204,7 @@ def get_addons():
|
|
169 |
FROM Customization_Options__c
|
170 |
WHERE Section__c = '{item_section}'
|
171 |
"""
|
172 |
-
result = sf.
|
173 |
addons = result.get('records', [])
|
174 |
|
175 |
if not addons:
|
@@ -178,10 +213,14 @@ def get_addons():
|
|
178 |
formatted_addons = []
|
179 |
for addon in addons:
|
180 |
options = addon.get("Options__c", "")
|
181 |
-
|
|
|
|
|
|
|
|
|
182 |
formatted_addons.append({
|
183 |
-
"name": addon
|
184 |
-
"type": addon
|
185 |
"options": options,
|
186 |
"max_selections": addon.get("Max_Selections__c", 1),
|
187 |
"extra_charge": addon.get("Extra_Charge__c", False),
|
@@ -199,39 +238,41 @@ def add_to_cart():
|
|
199 |
try:
|
200 |
data = request.json
|
201 |
item_name = data.get('itemName', '').strip()
|
202 |
-
item_price =
|
203 |
-
item_image = data.get('itemImage'
|
204 |
addons = data.get('addons', [])
|
205 |
instructions = data.get('instructions', '')
|
206 |
-
category = data.get('category'
|
207 |
-
section = data.get('section'
|
208 |
-
quantity =
|
209 |
customer_email = session.get('user_email')
|
210 |
|
211 |
-
if not item_name or not item_price
|
212 |
-
return jsonify({"success": False, "error": "Item name
|
|
|
|
|
|
|
213 |
|
214 |
query = f"""
|
215 |
-
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
216 |
FROM Cart_Item__c
|
217 |
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
218 |
"""
|
219 |
result = sf.query(query)
|
220 |
cart_items = result.get("records", [])
|
221 |
|
222 |
-
addons_price = sum(
|
223 |
-
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
224 |
|
225 |
if cart_items:
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
existing_instructions = cart_item.get('Instructions__c', "")
|
232 |
|
233 |
combined_addons = existing_addons if existing_addons != "None" else ""
|
234 |
-
if new_addons
|
235 |
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
236 |
|
237 |
combined_instructions = existing_instructions
|
@@ -243,26 +284,29 @@ def add_to_cart():
|
|
243 |
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
244 |
)
|
245 |
|
246 |
-
total_price = (existing_quantity + quantity) * item_price + combined_addons_price
|
247 |
-
|
248 |
sf.Cart_Item__c.update(cart_item_id, {
|
249 |
"Quantity__c": existing_quantity + quantity,
|
250 |
"Add_Ons__c": combined_addons,
|
251 |
"Add_Ons_Price__c": combined_addons_price,
|
252 |
"Instructions__c": combined_instructions,
|
253 |
-
"Price__c":
|
254 |
"Category__c": category,
|
255 |
"Section__c": section
|
256 |
})
|
257 |
else:
|
|
|
|
|
|
|
|
|
258 |
total_price = item_price * quantity + addons_price
|
|
|
259 |
sf.Cart_Item__c.create({
|
260 |
"Name": item_name,
|
261 |
"Price__c": total_price,
|
262 |
"Base_Price__c": item_price,
|
263 |
"Quantity__c": quantity,
|
264 |
"Add_Ons_Price__c": addons_price,
|
265 |
-
"Add_Ons__c":
|
266 |
"Image1__c": item_image,
|
267 |
"Customer_Email__c": customer_email,
|
268 |
"Instructions__c": instructions,
|
@@ -270,15 +314,11 @@ def add_to_cart():
|
|
270 |
"Section__c": section
|
271 |
})
|
272 |
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
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
|
|
|
1 |
from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
|
|
|
2 |
from salesforce import get_salesforce_connection
|
3 |
+
import os
|
4 |
|
5 |
menu_blueprint = Blueprint('menu', __name__)
|
6 |
|
|
|
15 |
|
16 |
# Create placeholder video at startup if it doesn't exist
|
17 |
if not os.path.exists(PLACEHOLDER_PATH):
|
18 |
+
open(PLACEHOLDER_PATH, 'wb').close()
|
|
|
19 |
print(f"Created placeholder video at {PLACEHOLDER_PATH}")
|
20 |
|
21 |
def get_valid_video_path(item_name, video_url=None):
|
22 |
+
"""
|
23 |
+
Get valid video path for item with placeholder fallback
|
24 |
+
Priority: 1. Video1__c from Salesforce 2. placeholder.mp4
|
25 |
+
"""
|
26 |
+
# First try: Video1__c from Salesforce if provided
|
27 |
if video_url:
|
28 |
+
# If it's a complete URL (http/https)
|
29 |
if video_url.startswith(('http://', 'https://')):
|
30 |
return video_url
|
31 |
+
# If it's a relative path (/videos/xxx.mp4)
|
32 |
elif video_url.startswith('/'):
|
33 |
return video_url
|
34 |
+
# If it's a Salesforce File ID (starts with '069')
|
35 |
elif video_url.startswith('069'):
|
36 |
+
return f"https://yourdomain.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
|
37 |
+
|
38 |
+
# Final fallback: placeholder.mp4
|
39 |
if not os.path.exists(PLACEHOLDER_PATH):
|
40 |
+
open(PLACEHOLDER_PATH, 'wb').close()
|
|
|
41 |
print(f"Created missing placeholder video at {PLACEHOLDER_PATH}")
|
42 |
+
|
43 |
return f"/static/{PLACEHOLDER_VIDEO}"
|
44 |
|
45 |
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
|
|
47 |
selected_category = request.args.get("category", "All")
|
48 |
user_email = session.get('user_email')
|
49 |
|
|
|
50 |
if not user_email:
|
51 |
user_email = request.args.get("email")
|
52 |
user_name = request.args.get("name")
|
53 |
+
|
54 |
+
if user_email:
|
55 |
session['user_email'] = user_email
|
56 |
session['user_name'] = user_name
|
57 |
else:
|
|
|
61 |
|
62 |
first_letter = user_name[0].upper() if user_name else "A"
|
63 |
|
64 |
+
try:
|
65 |
+
# Fetch user referral and reward points
|
66 |
+
user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
67 |
+
user_result = sf.query(user_query)
|
68 |
+
|
69 |
+
if not user_result['records']:
|
70 |
+
return redirect(url_for('login'))
|
71 |
+
|
72 |
+
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
|
73 |
+
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
|
74 |
+
|
75 |
+
# Get cart item count
|
76 |
+
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
77 |
+
cart_count_result = sf.query(cart_query)
|
78 |
+
cart_item_count = cart_count_result['totalSize']
|
79 |
+
|
80 |
+
# First try basic query with essential fields
|
81 |
+
base_fields = [
|
82 |
+
"Name", "Price__c", "Description__c", "Image1__c", "Image2__c",
|
83 |
+
"Veg_NonVeg__c", "Section__c", "Total_Ordered__c", "Video1__c"
|
84 |
+
]
|
85 |
+
|
86 |
+
# Try to include nutrition fields if they exist
|
87 |
+
try:
|
88 |
+
describe_result = sf.Menu_Item__c.describe()
|
89 |
+
available_fields = [field['name'] for field in describe_result['fields']]
|
90 |
+
|
91 |
+
nutrition_fields = []
|
92 |
+
if 'IngredientsInfo__c' in available_fields:
|
93 |
+
nutrition_fields.append('IngredientsInfo__c')
|
94 |
+
if 'Nutritional_Info__c' in available_fields:
|
95 |
+
nutrition_fields.append('Nutritional_Info__c')
|
96 |
+
if 'Allergens__c' in available_fields:
|
97 |
+
nutrition_fields.append('Allergens__c')
|
98 |
+
|
99 |
+
query_fields = base_fields + nutrition_fields
|
100 |
+
except Exception as e:
|
101 |
+
print(f"Error checking for nutrition fields: {str(e)}")
|
102 |
+
query_fields = base_fields
|
103 |
+
|
104 |
+
# Build and execute the query
|
105 |
+
menu_query = f"SELECT {', '.join(query_fields)} FROM Menu_Item__c"
|
106 |
+
result = sf.query(menu_query)
|
107 |
+
food_items = result['records'] if 'records' in result else []
|
108 |
+
|
109 |
+
# Process items with fallback values
|
110 |
+
for item in food_items:
|
111 |
+
# Ensure required fields have values
|
112 |
+
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0)
|
113 |
+
item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
|
114 |
+
|
115 |
+
# Add fallback values for nutrition fields
|
116 |
+
item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', 'Ingredients information not available')
|
117 |
+
item['Nutritional_Info__c'] = item.get('Nutritional_Info__c', 'Nutritional information not available')
|
118 |
+
item['Allergens__c'] = item.get('Allergens__c', 'Allergen information not available')
|
119 |
+
|
120 |
+
# Process best sellers
|
121 |
+
best_sellers = sorted(food_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True)
|
122 |
+
|
123 |
+
# Apply category filter
|
124 |
+
if selected_category == "Veg":
|
125 |
+
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
|
126 |
+
elif selected_category == "Non veg":
|
127 |
+
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
|
128 |
+
|
129 |
+
best_sellers = best_sellers[:4]
|
130 |
+
ordered_menu = {section: [] for section in SECTION_ORDER}
|
131 |
+
|
132 |
+
if best_sellers:
|
133 |
+
ordered_menu["Best Sellers"] = best_sellers
|
134 |
+
|
135 |
+
# Organize items by section
|
136 |
+
added_item_names = set()
|
137 |
+
for item in food_items:
|
138 |
+
section = item.get("Section__c", "Others")
|
139 |
+
if section not in ordered_menu:
|
140 |
+
ordered_menu[section] = []
|
141 |
+
|
142 |
+
if item['Name'] in added_item_names:
|
143 |
+
continue
|
144 |
+
|
145 |
+
# Apply category filter to all items
|
146 |
+
if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]:
|
147 |
+
continue
|
148 |
+
if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]:
|
149 |
+
continue
|
150 |
+
|
151 |
+
ordered_menu[section].append(item)
|
152 |
+
added_item_names.add(item['Name'])
|
153 |
+
|
154 |
+
# Remove empty sections
|
155 |
+
ordered_menu = {section: items for section, items in ordered_menu.items() if items}
|
156 |
+
categories = ["All", "Veg", "Non veg"]
|
157 |
+
|
158 |
+
except Exception as e:
|
159 |
+
print(f"Error fetching menu data: {str(e)}")
|
160 |
+
# Fallback data
|
161 |
+
ordered_menu = {section: [] for section in SECTION_ORDER}
|
162 |
+
best_sellers = ["Chicken Biryani", "Paneer Butter Masala", "Veg Manchurian", "Prawn Fry"]
|
163 |
+
ordered_menu["Best Sellers"] = [{
|
164 |
+
"Name": name,
|
165 |
+
"Price__c": "12.99",
|
166 |
+
"Description__c": f"Popular {name}",
|
167 |
+
"Image1__c": "/static/placeholder.jpg",
|
168 |
+
"Video1__c": get_valid_video_path(name),
|
169 |
+
"Total_Ordered__c": 100,
|
170 |
+
"Veg_NonVeg__c": "Veg" if "Paneer" in name or "Veg" in name else "Non veg",
|
171 |
+
"IngredientsInfo__c": "Ingredients information not available",
|
172 |
+
"Nutritional_Info__c": "Nutritional information not available",
|
173 |
+
"Allergens__c": "Allergen information not available"
|
174 |
+
} for name in best_sellers]
|
175 |
+
|
176 |
+
categories = ["All", "Veg", "Non veg"]
|
177 |
+
referral_code = 'N/A'
|
178 |
+
reward_points = 0
|
179 |
+
cart_item_count = 0
|
180 |
|
181 |
return render_template(
|
182 |
"menu.html",
|
|
|
192 |
|
193 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|
194 |
def get_addons():
|
195 |
+
item_name = request.args.get('item_name')
|
196 |
+
item_section = request.args.get('item_section')
|
197 |
|
198 |
if not item_name or not item_section:
|
199 |
return jsonify({"success": False, "error": "Item name and section are required."}), 400
|
|
|
204 |
FROM Customization_Options__c
|
205 |
WHERE Section__c = '{item_section}'
|
206 |
"""
|
207 |
+
result = sf.query(query)
|
208 |
addons = result.get('records', [])
|
209 |
|
210 |
if not addons:
|
|
|
213 |
formatted_addons = []
|
214 |
for addon in addons:
|
215 |
options = addon.get("Options__c", "")
|
216 |
+
if options:
|
217 |
+
options = options.split(", ")
|
218 |
+
else:
|
219 |
+
options = []
|
220 |
+
|
221 |
formatted_addons.append({
|
222 |
+
"name": addon["Name"],
|
223 |
+
"type": addon["Customization_Type__c"],
|
224 |
"options": options,
|
225 |
"max_selections": addon.get("Max_Selections__c", 1),
|
226 |
"extra_charge": addon.get("Extra_Charge__c", False),
|
|
|
238 |
try:
|
239 |
data = request.json
|
240 |
item_name = data.get('itemName', '').strip()
|
241 |
+
item_price = data.get('itemPrice')
|
242 |
+
item_image = data.get('itemImage')
|
243 |
addons = data.get('addons', [])
|
244 |
instructions = data.get('instructions', '')
|
245 |
+
category = data.get('category')
|
246 |
+
section = data.get('section')
|
247 |
+
quantity = data.get('quantity', 1)
|
248 |
customer_email = session.get('user_email')
|
249 |
|
250 |
+
if not item_name or not item_price:
|
251 |
+
return jsonify({"success": False, "error": "Item name and price are required."}), 400
|
252 |
+
|
253 |
+
if not customer_email:
|
254 |
+
return jsonify({"success": False, "error": "User email is required."}), 400
|
255 |
|
256 |
query = f"""
|
257 |
+
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
258 |
FROM Cart_Item__c
|
259 |
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
260 |
"""
|
261 |
result = sf.query(query)
|
262 |
cart_items = result.get("records", [])
|
263 |
|
264 |
+
addons_price = sum(addon['price'] for addon in addons)
|
265 |
+
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
266 |
|
267 |
if cart_items:
|
268 |
+
cart_item_id = cart_items[0]['Id']
|
269 |
+
existing_quantity = cart_items[0]['Quantity__c']
|
270 |
+
existing_addons = cart_items[0].get('Add_Ons__c', "None")
|
271 |
+
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
272 |
+
existing_instructions = cart_items[0].get('Instructions__c', "")
|
|
|
273 |
|
274 |
combined_addons = existing_addons if existing_addons != "None" else ""
|
275 |
+
if new_addons:
|
276 |
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
277 |
|
278 |
combined_instructions = existing_instructions
|
|
|
284 |
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
285 |
)
|
286 |
|
|
|
|
|
287 |
sf.Cart_Item__c.update(cart_item_id, {
|
288 |
"Quantity__c": existing_quantity + quantity,
|
289 |
"Add_Ons__c": combined_addons,
|
290 |
"Add_Ons_Price__c": combined_addons_price,
|
291 |
"Instructions__c": combined_instructions,
|
292 |
+
"Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
|
293 |
"Category__c": category,
|
294 |
"Section__c": section
|
295 |
})
|
296 |
else:
|
297 |
+
addons_string = "None"
|
298 |
+
if addons:
|
299 |
+
addons_string = new_addons
|
300 |
+
|
301 |
total_price = item_price * quantity + addons_price
|
302 |
+
|
303 |
sf.Cart_Item__c.create({
|
304 |
"Name": item_name,
|
305 |
"Price__c": total_price,
|
306 |
"Base_Price__c": item_price,
|
307 |
"Quantity__c": quantity,
|
308 |
"Add_Ons_Price__c": addons_price,
|
309 |
+
"Add_Ons__c": addons_string,
|
310 |
"Image1__c": item_image,
|
311 |
"Customer_Email__c": customer_email,
|
312 |
"Instructions__c": instructions,
|
|
|
314 |
"Section__c": section
|
315 |
})
|
316 |
|
317 |
+
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
318 |
+
|
319 |
+
except KeyError as e:
|
320 |
+
return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
|
|
|
|
|
321 |
|
|
|
|
|
322 |
except Exception as e:
|
323 |
print(f"Error adding item to cart: {str(e)}")
|
324 |
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|