Update menu.py
Browse files
menu.py
CHANGED
@@ -1,5 +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__)
|
@@ -36,97 +37,150 @@ def get_valid_video_path(item_name, video_url=None):
|
|
36 |
|
37 |
def fetch_menu_data(selected_category, user_email):
|
38 |
"""Common function to fetch and process menu data."""
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
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 |
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
128 |
def menu():
|
|
|
129 |
selected_category = request.args.get("category", "All")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
user_email = session.get('user_email')
|
131 |
|
132 |
# Handle user authentication
|
@@ -144,11 +198,11 @@ def menu():
|
|
144 |
first_letter = user_name[0].upper() if user_name else "A"
|
145 |
|
146 |
# Fetch menu data
|
147 |
-
ordered_menu, referral_code, reward_points, cart_item_count = fetch_menu_data(selected_category, user_email)
|
148 |
if ordered_menu is None:
|
149 |
return redirect(url_for('login'))
|
150 |
|
151 |
-
categories = ["All", "Veg", "Non veg"]
|
152 |
|
153 |
return render_template(
|
154 |
"menu.html",
|
@@ -159,7 +213,9 @@ def menu():
|
|
159 |
reward_points=reward_points,
|
160 |
user_name=user_name,
|
161 |
first_letter=first_letter,
|
162 |
-
cart_item_count=cart_item_count
|
|
|
|
|
163 |
)
|
164 |
|
165 |
@menu_blueprint.route("/search", methods=["GET"])
|
@@ -174,7 +230,7 @@ def search():
|
|
174 |
first_letter = user_name[0].upper() if user_name else "A"
|
175 |
|
176 |
# Fetch menu data
|
177 |
-
ordered_menu, referral_code, reward_points, cart_item_count = fetch_menu_data(selected_category, user_email)
|
178 |
if ordered_menu is None:
|
179 |
return redirect(url_for('login'))
|
180 |
|
@@ -183,7 +239,9 @@ def search():
|
|
183 |
ordered_menu=ordered_menu,
|
184 |
user_name=user_name,
|
185 |
first_letter=first_letter,
|
186 |
-
cart_item_count=cart_item_count
|
|
|
|
|
187 |
)
|
188 |
|
189 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|
|
|
1 |
from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
|
2 |
import os
|
3 |
+
import re
|
4 |
from salesforce import get_salesforce_connection
|
5 |
|
6 |
menu_blueprint = Blueprint('menu', __name__)
|
|
|
37 |
|
38 |
def fetch_menu_data(selected_category, user_email):
|
39 |
"""Common function to fetch and process menu data."""
|
40 |
+
try:
|
41 |
+
# Fetch user referral and reward points
|
42 |
+
user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
43 |
+
user_result = sf.query(user_query)
|
44 |
+
if not user_result.get('records'):
|
45 |
+
return None, None, None, None, None, None
|
46 |
+
|
47 |
+
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
|
48 |
+
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
|
49 |
+
|
50 |
+
# Get cart item count
|
51 |
+
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
52 |
+
cart_count_result = sf.query(cart_query)
|
53 |
+
cart_item_count = cart_count_result.get('totalSize', 0)
|
54 |
+
|
55 |
+
# Fetch all Menu_Item__c records with required fields
|
56 |
+
menu_query = """
|
57 |
+
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
58 |
+
Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c,
|
59 |
+
IngredientsInfo__c, NutritionalInfo__c, Allergens__c
|
60 |
+
FROM Menu_Item__c
|
61 |
+
"""
|
62 |
+
menu_result = sf.query_all(menu_query)
|
63 |
+
food_items = menu_result.get('records', [])
|
64 |
+
|
65 |
+
# Process menu items
|
66 |
+
for item in food_items:
|
67 |
+
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
|
68 |
+
item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
|
69 |
+
item['Section__c'] = item.get('Section__c', "Others")
|
70 |
+
item['Description__c'] = item.get('Description__c', "No description available")
|
71 |
+
item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
|
72 |
+
item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
|
73 |
+
item['Allergens__c'] = item.get('Allergens__c', "None listed")
|
74 |
+
item['is_menu_item'] = True # Flag to identify Menu_Item__c records
|
75 |
+
|
76 |
+
# Fetch all Custom_Dish__c records with only existing fields
|
77 |
+
custom_dish_query = """
|
78 |
+
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
79 |
+
Veg_NonVeg__c, Section__c, Total_Ordered__c
|
80 |
+
FROM Custom_Dish__c
|
81 |
+
WHERE CreatedDate >= LAST_N_DAYS:7
|
82 |
+
"""
|
83 |
+
custom_dish_result = sf.query_all(custom_dish_query)
|
84 |
+
custom_dishes = custom_dish_result.get('records', [])
|
85 |
+
|
86 |
+
# Process custom dishes
|
87 |
+
for item in custom_dishes:
|
88 |
+
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
|
89 |
+
item['Video1__c'] = get_valid_video_path(item['Name'])
|
90 |
+
item['Section__c'] = item.get('Section__c', "Customized dish")
|
91 |
+
item['Description__c'] = item.get('Description__c', "No description available")
|
92 |
+
item['is_menu_item'] = False # Flag to identify Custom_Dish__c records
|
93 |
+
|
94 |
+
# Merge all items
|
95 |
+
all_items = food_items + custom_dishes
|
96 |
+
ordered_menu = {section: [] for section in SECTION_ORDER}
|
97 |
+
|
98 |
+
# Process best sellers
|
99 |
+
best_sellers = sorted(all_items, key=lambda x: x['Total_Ordered__c'], reverse=True)
|
100 |
+
if selected_category == "Veg":
|
101 |
+
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
|
102 |
+
elif selected_category == "Non veg":
|
103 |
+
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
|
104 |
+
elif selected_category == "Customized Dish":
|
105 |
+
best_sellers = [item for item in best_sellers if item.get("Section__c") == "Customized dish"]
|
106 |
+
ordered_menu["Best Sellers"] = best_sellers[:4]
|
107 |
+
|
108 |
+
# Organize items into sections
|
109 |
+
added_item_names = set()
|
110 |
+
for item in all_items:
|
111 |
+
section = item['Section__c']
|
112 |
+
if section not in ordered_menu:
|
113 |
+
ordered_menu[section] = []
|
114 |
+
|
115 |
+
if item['Name'] in added_item_names:
|
116 |
+
continue
|
117 |
+
|
118 |
+
veg_nonveg = item.get("Veg_NonVeg__c", "both")
|
119 |
+
if selected_category == "Veg" and veg_nonveg not in ["Veg", "both"]:
|
120 |
+
continue
|
121 |
+
if selected_category == "Non veg" and veg_nonveg not in ["Non veg", "both"]:
|
122 |
+
continue
|
123 |
+
if selected_category == "Customized Dish" and section != "Customized dish":
|
124 |
+
continue
|
125 |
+
|
126 |
+
ordered_menu[section].append(item)
|
127 |
+
added_item_names.add(item['Name'])
|
128 |
+
|
129 |
+
# Remove empty sections
|
130 |
+
ordered_menu = {section: items for section, items in ordered_menu.items() if items}
|
131 |
+
|
132 |
+
# Fetch past orders for add-on and instruction analysis
|
133 |
+
past_orders_query = f"""
|
134 |
+
SELECT Order_Details__c FROM Order__c
|
135 |
+
WHERE Customer_Email__c = '{user_email}'
|
136 |
+
"""
|
137 |
+
past_orders_result = sf.query(past_orders_query)
|
138 |
+
past_orders = past_orders_result.get('records', [])
|
139 |
+
|
140 |
+
# Frequency analysis for add-ons and instructions
|
141 |
+
addon_counts = {}
|
142 |
+
instruction_counts = {}
|
143 |
+
for order in past_orders:
|
144 |
+
order_details = order.get('Order_Details__c', '').split('\n')
|
145 |
+
for line in order_details:
|
146 |
+
item_parts = line.split('|')
|
147 |
+
if len(item_parts) >= 5:
|
148 |
+
addons = item_parts[1].strip().replace('Add-Ons:', '').split(', ')
|
149 |
+
instructions = item_parts[2].strip().replace('Instructions:', '').split(', ')
|
150 |
+
cleaned_addons = [re.sub(r"\s?\(\$\d+(\.\d{2})?\)", "", addon).strip() for addon in addons]
|
151 |
+
for addon in cleaned_addons:
|
152 |
+
individual_addons = addon.split(';')
|
153 |
+
for individual_addon in individual_addons:
|
154 |
+
individual_addon = individual_addon.strip()
|
155 |
+
if individual_addon:
|
156 |
+
addon_counts[individual_addon] = addon_counts.get(individual_addon, 0) + 1
|
157 |
+
for instruction in instructions:
|
158 |
+
instruction = instruction.strip()
|
159 |
+
if instruction:
|
160 |
+
instruction_counts[instruction] = instruction_counts.get(instruction, 0) + 1
|
161 |
+
|
162 |
+
# Get most common add-ons and instructions (top 3)
|
163 |
+
most_common_addons = sorted(addon_counts, key=addon_counts.get, reverse=True)[:3]
|
164 |
+
most_common_instructions = sorted(instruction_counts, key=instruction_counts.get, reverse=True)[:3]
|
165 |
+
|
166 |
+
return ordered_menu, referral_code, reward_points, cart_item_count, most_common_addons, most_common_instructions
|
167 |
+
|
168 |
+
except Exception as e:
|
169 |
+
print(f"Error fetching menu data: {str(e)}")
|
170 |
+
return None, None, None, None, [], []
|
171 |
|
172 |
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
173 |
def menu():
|
174 |
+
# Handle filter parameters
|
175 |
selected_category = request.args.get("category", "All")
|
176 |
+
is_veg_only = request.args.get("veg") == 'on'
|
177 |
+
|
178 |
+
# Prioritize Customized Dish, then Veg, then All
|
179 |
+
if request.args.get("category") == "Customized Dish":
|
180 |
+
selected_category = "Customized Dish"
|
181 |
+
else:
|
182 |
+
selected_category = "Veg" if is_veg_only else "All"
|
183 |
+
|
184 |
user_email = session.get('user_email')
|
185 |
|
186 |
# Handle user authentication
|
|
|
198 |
first_letter = user_name[0].upper() if user_name else "A"
|
199 |
|
200 |
# Fetch menu data
|
201 |
+
ordered_menu, referral_code, reward_points, cart_item_count, most_common_addons, most_common_instructions = fetch_menu_data(selected_category, user_email)
|
202 |
if ordered_menu is None:
|
203 |
return redirect(url_for('login'))
|
204 |
|
205 |
+
categories = ["All", "Veg", "Non veg", "Customized Dish"]
|
206 |
|
207 |
return render_template(
|
208 |
"menu.html",
|
|
|
213 |
reward_points=reward_points,
|
214 |
user_name=user_name,
|
215 |
first_letter=first_letter,
|
216 |
+
cart_item_count=cart_item_count,
|
217 |
+
most_common_addons=[addon.strip() for addon in most_common_addons],
|
218 |
+
most_common_instructions=most_common_instructions
|
219 |
)
|
220 |
|
221 |
@menu_blueprint.route("/search", methods=["GET"])
|
|
|
230 |
first_letter = user_name[0].upper() if user_name else "A"
|
231 |
|
232 |
# Fetch menu data
|
233 |
+
ordered_menu, referral_code, reward_points, cart_item_count, most_common_addons, most_common_instructions = fetch_menu_data(selected_category, user_email)
|
234 |
if ordered_menu is None:
|
235 |
return redirect(url_for('login'))
|
236 |
|
|
|
239 |
ordered_menu=ordered_menu,
|
240 |
user_name=user_name,
|
241 |
first_letter=first_letter,
|
242 |
+
cart_item_count=cart_item_count,
|
243 |
+
most_common_addons=[addon.strip() for addon in most_common_addons],
|
244 |
+
most_common_instructions=most_common_instructions
|
245 |
)
|
246 |
|
247 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|