Update menu.py
Browse files
menu.py
CHANGED
@@ -1,27 +1,45 @@
|
|
1 |
-
from flask import Blueprint, render_template, request, session,
|
2 |
from salesforce import get_salesforce_connection
|
3 |
import os
|
4 |
|
|
|
|
|
|
|
|
|
5 |
menu_blueprint = Blueprint('menu', __name__)
|
6 |
|
7 |
# Initialize Salesforce connection
|
8 |
sf = get_salesforce_connection()
|
9 |
|
10 |
-
#
|
11 |
-
STATIC_DIR = os.path.join(
|
|
|
|
|
12 |
PLACEHOLDER_VIDEO = 'placeholder.mp4'
|
13 |
PLACEHOLDER_PATH = os.path.join(STATIC_DIR, PLACEHOLDER_VIDEO)
|
14 |
-
SECTION_ORDER = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "Customized dish", "Apetizer", "Desserts", "Soft Drinks"]
|
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:
|
@@ -35,6 +53,11 @@ def get_valid_video_path(item_name, video_url=None):
|
|
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()
|
@@ -42,113 +65,112 @@ def get_valid_video_path(item_name, video_url=None):
|
|
42 |
|
43 |
return f"/static/{PLACEHOLDER_VIDEO}"
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
46 |
def menu():
|
|
|
|
|
|
|
47 |
selected_category = request.args.get("category", "All")
|
48 |
-
|
49 |
-
|
50 |
if not user_email:
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
session['user_email'] = user_email
|
56 |
-
session['user_name'] = user_name
|
57 |
-
else:
|
58 |
-
return redirect(url_for("login"))
|
59 |
-
else:
|
60 |
-
user_name = session.get('user_name')
|
61 |
-
|
62 |
first_letter = user_name[0].upper() if user_name else "A"
|
63 |
|
64 |
try:
|
65 |
-
#
|
66 |
-
|
67 |
-
|
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 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
82 |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
83 |
Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c
|
84 |
FROM Menu_Item__c
|
85 |
-
"""
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
# Process items and add video paths
|
90 |
-
for item in food_items:
|
91 |
-
if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None:
|
92 |
-
item['Total_Ordered__c'] = 0
|
93 |
-
item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
|
94 |
-
|
95 |
-
# Query to fetch Custom_Dish__c records
|
96 |
-
custom_dish_query = """
|
97 |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
98 |
Veg_NonVeg__c, Section__c, Total_Ordered__c
|
99 |
FROM Custom_Dish__c
|
100 |
WHERE CreatedDate >= LAST_N_DAYS:7
|
101 |
-
"""
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
item
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
ordered_menu = {section: [] for section in SECTION_ORDER}
|
114 |
-
|
115 |
-
# Process best sellers
|
116 |
-
best_sellers = sorted(all_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True)
|
117 |
-
|
118 |
-
if selected_category == "Veg":
|
119 |
-
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
|
120 |
-
elif selected_category == "Non veg":
|
121 |
-
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
|
122 |
-
|
123 |
-
best_sellers = best_sellers[:4]
|
124 |
-
if best_sellers:
|
125 |
-
ordered_menu["Best Sellers"] = best_sellers
|
126 |
-
|
127 |
-
# Organize other sections
|
128 |
-
added_item_names = set()
|
129 |
-
for item in all_items:
|
130 |
-
section = item.get("Section__c", "Others")
|
131 |
-
if section not in ordered_menu:
|
132 |
-
ordered_menu[section] = []
|
133 |
-
|
134 |
-
if item['Name'] in added_item_names:
|
135 |
-
continue
|
136 |
-
|
137 |
-
if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]:
|
138 |
-
continue
|
139 |
-
if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]:
|
140 |
-
continue
|
141 |
-
|
142 |
-
ordered_menu[section].append(item)
|
143 |
-
added_item_names.add(item['Name'])
|
144 |
-
|
145 |
-
ordered_menu = {section: items for section, items in ordered_menu.items() if items}
|
146 |
-
categories = ["All", "Veg", "Non veg"]
|
147 |
-
|
148 |
except Exception as e:
|
149 |
-
print(f"Error
|
150 |
-
# Fallback data
|
151 |
-
ordered_menu = {section: [] for section in
|
|
|
|
|
|
|
|
|
|
|
152 |
best_sellers = ["Chicken Biryani", "Paneer Butter Masala", "Veg Manchurian", "Prawn Fry"]
|
153 |
ordered_menu["Best Sellers"] = [{
|
154 |
"Name": name,
|
@@ -159,22 +181,105 @@ def menu():
|
|
159 |
"Total_Ordered__c": 100,
|
160 |
"Veg_NonVeg__c": "Veg" if "Paneer" in name or "Veg" in name else "Non veg"
|
161 |
} for name in best_sellers]
|
162 |
-
|
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",
|
170 |
ordered_menu=ordered_menu,
|
171 |
-
categories=categories,
|
172 |
selected_category=selected_category,
|
173 |
referral_code=referral_code,
|
174 |
reward_points=reward_points,
|
175 |
user_name=user_name,
|
176 |
first_letter=first_letter,
|
177 |
-
cart_item_count=
|
178 |
)
|
179 |
|
180 |
-
# [Rest of your
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, Blueprint, render_template, request, session, redirect, url_for, jsonify
|
2 |
from salesforce import get_salesforce_connection
|
3 |
import os
|
4 |
|
5 |
+
app = Flask(__name__)
|
6 |
+
app.secret_key = 'your-secret-key-here' # Change for production
|
7 |
+
|
8 |
+
# Initialize Blueprint
|
9 |
menu_blueprint = Blueprint('menu', __name__)
|
10 |
|
11 |
# Initialize Salesforce connection
|
12 |
sf = get_salesforce_connection()
|
13 |
|
14 |
+
# Define static folder path
|
15 |
+
STATIC_DIR = os.path.join(app.root_path, 'static')
|
16 |
+
|
17 |
+
# Video configuration
|
18 |
PLACEHOLDER_VIDEO = 'placeholder.mp4'
|
19 |
PLACEHOLDER_PATH = os.path.join(STATIC_DIR, PLACEHOLDER_VIDEO)
|
|
|
20 |
|
21 |
# Create placeholder video at startup if it doesn't exist
|
22 |
if not os.path.exists(PLACEHOLDER_PATH):
|
23 |
open(PLACEHOLDER_PATH, 'wb').close()
|
24 |
print(f"Created placeholder video at {PLACEHOLDER_PATH}")
|
25 |
|
26 |
+
# Menu configuration
|
27 |
+
MENU_CONFIG = {
|
28 |
+
"section_order": ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads",
|
29 |
+
"Customized dish", "Apetizer", "Desserts", "Soft Drinks"],
|
30 |
+
"section_mapping": {
|
31 |
+
"Main Course": "Curries",
|
32 |
+
"Appetizers": "Starters",
|
33 |
+
"Breads": "Breads",
|
34 |
+
"Soft Drinks": "Soft Drinks"
|
35 |
+
},
|
36 |
+
"categories": ["All", "Veg", "Non veg"]
|
37 |
+
}
|
38 |
+
|
39 |
def get_valid_video_path(item_name, video_url=None):
|
40 |
"""
|
41 |
Get valid video path for item with placeholder fallback
|
42 |
+
Priority: 1. Video1__c from Salesforce 2. Generated filename 3. placeholder.mp4
|
43 |
"""
|
44 |
# First try: Video1__c from Salesforce if provided
|
45 |
if video_url:
|
|
|
53 |
elif video_url.startswith('069'):
|
54 |
return f"https://yourdomain.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
|
55 |
|
56 |
+
# Second try: Generated filename based on item name
|
57 |
+
video_file = f"{item_name.lower().replace(' ', '_')}.mp4"
|
58 |
+
if os.path.isfile(os.path.join(STATIC_DIR, video_file)):
|
59 |
+
return f"/static/{video_file}"
|
60 |
+
|
61 |
# Final fallback: placeholder.mp4
|
62 |
if not os.path.exists(PLACEHOLDER_PATH):
|
63 |
open(PLACEHOLDER_PATH, 'wb').close()
|
|
|
65 |
|
66 |
return f"/static/{PLACEHOLDER_VIDEO}"
|
67 |
|
68 |
+
def organize_menu_items(items, selected_category="All"):
|
69 |
+
"""Organize items into the requested section structure with category filtering"""
|
70 |
+
ordered_menu = {section: [] for section in MENU_CONFIG["section_order"]}
|
71 |
+
|
72 |
+
# Filter by category if needed
|
73 |
+
filtered_items = []
|
74 |
+
for item in items:
|
75 |
+
if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]:
|
76 |
+
continue
|
77 |
+
if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]:
|
78 |
+
continue
|
79 |
+
filtered_items.append(item)
|
80 |
+
|
81 |
+
# Get top 4 best sellers
|
82 |
+
best_sellers = sorted(filtered_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True)[:4]
|
83 |
+
if best_sellers:
|
84 |
+
ordered_menu["Best Sellers"] = best_sellers
|
85 |
+
|
86 |
+
# Organize remaining items
|
87 |
+
for item in filtered_items:
|
88 |
+
if item in best_sellers:
|
89 |
+
continue
|
90 |
+
|
91 |
+
section = MENU_CONFIG["section_mapping"].get(
|
92 |
+
item.get("Section__c", "Others"),
|
93 |
+
item.get("Section__c", "Others")
|
94 |
+
)
|
95 |
+
|
96 |
+
if section in ordered_menu:
|
97 |
+
ordered_menu[section].append(item)
|
98 |
+
else:
|
99 |
+
if "Others" not in ordered_menu:
|
100 |
+
ordered_menu["Others"] = []
|
101 |
+
ordered_menu["Others"].append(item)
|
102 |
+
|
103 |
+
# Remove empty sections
|
104 |
+
return {section: items for section, items in ordered_menu.items() if items}
|
105 |
+
|
106 |
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
107 |
def menu():
|
108 |
+
# User session handling
|
109 |
+
user_email = session.get('user_email') or request.args.get("email")
|
110 |
+
user_name = session.get('user_name') or request.args.get("name")
|
111 |
selected_category = request.args.get("category", "All")
|
112 |
+
|
|
|
113 |
if not user_email:
|
114 |
+
return redirect(url_for("login"))
|
115 |
+
|
116 |
+
session['user_email'] = user_email
|
117 |
+
session['user_name'] = user_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
first_letter = user_name[0].upper() if user_name else "A"
|
119 |
|
120 |
try:
|
121 |
+
# Get user data from Salesforce
|
122 |
+
user_result = sf.query(f"""
|
123 |
+
SELECT Referral__c, Reward_Points__c
|
124 |
+
FROM Customer_Login__c
|
125 |
+
WHERE Email__c = '{user_email}'
|
126 |
+
""")
|
127 |
if not user_result['records']:
|
128 |
return redirect(url_for('login'))
|
129 |
+
|
130 |
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
|
131 |
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
|
132 |
+
|
133 |
# Get cart item count
|
134 |
+
cart_count = sf.query(f"""
|
135 |
+
SELECT COUNT()
|
136 |
+
FROM Cart_Item__c
|
137 |
+
WHERE Customer_Email__c = '{user_email}'
|
138 |
+
""")['totalSize']
|
139 |
+
|
140 |
+
# Get menu items including video field
|
141 |
+
food_items = sf.query("""
|
142 |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
143 |
Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c
|
144 |
FROM Menu_Item__c
|
145 |
+
""").get('records', [])
|
146 |
+
|
147 |
+
# Get custom dishes
|
148 |
+
custom_dishes = sf.query("""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
150 |
Veg_NonVeg__c, Section__c, Total_Ordered__c
|
151 |
FROM Custom_Dish__c
|
152 |
WHERE CreatedDate >= LAST_N_DAYS:7
|
153 |
+
""").get('records', [])
|
154 |
+
|
155 |
+
# Process items and add video paths
|
156 |
+
for item in food_items + custom_dishes:
|
157 |
+
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0)
|
158 |
+
item['Video1__c'] = get_valid_video_path(
|
159 |
+
item['Name'],
|
160 |
+
item.get('Video1__c')
|
161 |
+
)
|
162 |
+
|
163 |
+
ordered_menu = organize_menu_items(food_items + custom_dishes, selected_category)
|
164 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
except Exception as e:
|
166 |
+
print(f"Error: {str(e)}")
|
167 |
+
# Fallback data
|
168 |
+
ordered_menu = {section: [] for section in MENU_CONFIG["section_order"]}
|
169 |
+
referral_code = 'N/A'
|
170 |
+
reward_points = 0
|
171 |
+
cart_count = 0
|
172 |
+
|
173 |
+
# Sample best sellers with video support
|
174 |
best_sellers = ["Chicken Biryani", "Paneer Butter Masala", "Veg Manchurian", "Prawn Fry"]
|
175 |
ordered_menu["Best Sellers"] = [{
|
176 |
"Name": name,
|
|
|
181 |
"Total_Ordered__c": 100,
|
182 |
"Veg_NonVeg__c": "Veg" if "Paneer" in name or "Veg" in name else "Non veg"
|
183 |
} for name in best_sellers]
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
return render_template(
|
186 |
"menu.html",
|
187 |
ordered_menu=ordered_menu,
|
188 |
+
categories=MENU_CONFIG["categories"],
|
189 |
selected_category=selected_category,
|
190 |
referral_code=referral_code,
|
191 |
reward_points=reward_points,
|
192 |
user_name=user_name,
|
193 |
first_letter=first_letter,
|
194 |
+
cart_item_count=cart_count
|
195 |
)
|
196 |
|
197 |
+
# [Rest of your routes remain unchanged...]
|
198 |
+
@menu_blueprint.route('/api/addons', methods=['GET'])
|
199 |
+
def get_addons():
|
200 |
+
try:
|
201 |
+
item_section = request.args.get('item_section')
|
202 |
+
if not item_section:
|
203 |
+
return jsonify({"success": False, "error": "Section required"}), 400
|
204 |
+
|
205 |
+
addons = sf.query(f"""
|
206 |
+
SELECT Name, Customization_Type__c, Options__c, Max_Selections__c,
|
207 |
+
Extra_Charge__c, Extra_Charge_Amount__c
|
208 |
+
FROM Customization_Options__c
|
209 |
+
WHERE Section__c = '{item_section}'
|
210 |
+
""").get('records', [])
|
211 |
+
|
212 |
+
return jsonify({
|
213 |
+
"success": True,
|
214 |
+
"addons": [{
|
215 |
+
"name": a["Name"],
|
216 |
+
"type": a["Customization_Type__c"],
|
217 |
+
"options": a.get("Options__c", "").split(", "),
|
218 |
+
"max_selections": a.get("Max_Selections__c", 1),
|
219 |
+
"extra_charge": a.get("Extra_Charge__c", False),
|
220 |
+
"extra_charge_amount": float(a.get("Extra_Charge_Amount__c", 0))
|
221 |
+
} for a in addons]
|
222 |
+
})
|
223 |
+
|
224 |
+
except Exception as e:
|
225 |
+
return jsonify({"success": False, "error": str(e)}), 500
|
226 |
+
|
227 |
+
@menu_blueprint.route('/cart/add', methods=['POST'])
|
228 |
+
def add_to_cart():
|
229 |
+
try:
|
230 |
+
data = request.json
|
231 |
+
required_fields = ['itemName', 'itemPrice', 'category', 'section']
|
232 |
+
if not all(field in data for field in required_fields):
|
233 |
+
return jsonify({"success": False, "error": "Missing required fields"}), 400
|
234 |
+
|
235 |
+
customer_email = session.get('user_email')
|
236 |
+
if not customer_email:
|
237 |
+
return jsonify({"success": False, "error": "Not authenticated"}), 401
|
238 |
+
|
239 |
+
# Prepare cart item data
|
240 |
+
item_data = {
|
241 |
+
"Name": data['itemName'],
|
242 |
+
"Price__c": float(data['itemPrice']),
|
243 |
+
"Base_Price__c": float(data['itemPrice']),
|
244 |
+
"Quantity__c": int(data.get('quantity', 1)),
|
245 |
+
"Image1__c": data.get('itemImage', ''),
|
246 |
+
"Customer_Email__c": customer_email,
|
247 |
+
"Instructions__c": data.get('instructions', ''),
|
248 |
+
"Category__c": data['category'],
|
249 |
+
"Section__c": data['section']
|
250 |
+
}
|
251 |
+
|
252 |
+
# Handle addons
|
253 |
+
addons = data.get('addons', [])
|
254 |
+
if addons:
|
255 |
+
item_data["Add_Ons__c"] = "; ".join(
|
256 |
+
f"{a['name']} (${a['price']})" for a in addons
|
257 |
+
)
|
258 |
+
item_data["Add_Ons_Price__c"] = sum(float(a.get('price', 0)) for a in addons)
|
259 |
+
item_data["Price__c"] += item_data["Add_Ons_Price__c"]
|
260 |
+
|
261 |
+
# Check if item already in cart
|
262 |
+
existing = sf.query(f"""
|
263 |
+
SELECT Id FROM Cart_Item__c
|
264 |
+
WHERE Customer_Email__c = '{customer_email}' AND Name = '{data['itemName']}'
|
265 |
+
""").get('records', [])
|
266 |
+
|
267 |
+
if existing:
|
268 |
+
sf.Cart_Item__c.update(existing[0]['Id'], item_data)
|
269 |
+
else:
|
270 |
+
sf.Cart_Item__c.create(item_data)
|
271 |
+
|
272 |
+
return jsonify({"success": True, "message": "Added to cart"})
|
273 |
+
|
274 |
+
except Exception as e:
|
275 |
+
return jsonify({"success": False, "error": str(e)}), 500
|
276 |
+
|
277 |
+
# Register blueprint and routes
|
278 |
+
app.register_blueprint(menu_blueprint)
|
279 |
+
|
280 |
+
@app.route('/')
|
281 |
+
def home():
|
282 |
+
return redirect(url_for('menu.menu'))
|
283 |
+
|
284 |
+
if __name__ == "__main__":
|
285 |
+
app.run(debug=True)
|