Update app.py
Browse files
app.py
CHANGED
@@ -120,186 +120,116 @@ def login():
|
|
120 |
return render_template("login.html")
|
121 |
|
122 |
# Assuming you're fetching the order details and processing them like this
|
123 |
-
|
124 |
-
def
|
125 |
-
# Get the user email from session (assuming the session is already set during checkout)
|
126 |
email = session.get('user_email')
|
127 |
-
|
128 |
if not email:
|
129 |
return "User not logged in", 400
|
130 |
|
131 |
-
# Query Salesforce to fetch the user's reward points
|
132 |
try:
|
133 |
-
#
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
#
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
end_point = 100 # Default end point for other tiers
|
168 |
-
|
169 |
-
# Calculate progress as a percentage
|
170 |
-
if user_points >= start_point:
|
171 |
-
progress_percentage = ((user_points - start_point) / (end_point - start_point)) * 100
|
172 |
-
else:
|
173 |
-
progress_percentage = 0
|
174 |
-
|
175 |
-
# Calculate points to next tier
|
176 |
-
points_needed_for_next_tier = end_point - user_points
|
177 |
-
next_tier = "Gold" # Next tier name (you can dynamically calculate this)
|
178 |
-
# Round the points before passing to the template
|
179 |
-
user_points = round(user_points)
|
180 |
-
points_needed_for_next_tier = round(points_needed_for_next_tier)
|
181 |
-
|
182 |
-
# Pass these values to your template
|
183 |
-
return render_template(
|
184 |
-
'reward_status.html',
|
185 |
-
user_points=user_points,
|
186 |
-
current_tier=current_tier,
|
187 |
-
progress_percentage=progress_percentage,
|
188 |
-
points_needed_for_next_tier=points_needed_for_next_tier,
|
189 |
-
start_point=start_point,
|
190 |
-
end_point=end_point,
|
191 |
-
next_tier=next_tier,
|
192 |
-
tiers=tiers
|
193 |
-
)
|
194 |
-
@app.route('/order_summary')
|
195 |
-
def order_summary():
|
196 |
-
email = session.get('user_email')
|
197 |
-
|
198 |
-
if not email:
|
199 |
-
print("User not logged in. Redirecting to login.")
|
200 |
-
return "User not logged in", 400
|
201 |
-
|
202 |
-
try:
|
203 |
-
print(f"Fetching order details for email: {email}")
|
204 |
-
|
205 |
-
query = f"""
|
206 |
-
SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c
|
207 |
FROM Order__c
|
208 |
WHERE Customer_Email__c = '{email}'
|
209 |
ORDER BY CreatedDate DESC
|
210 |
LIMIT 1
|
211 |
"""
|
212 |
-
|
|
|
|
|
213 |
|
214 |
-
|
215 |
-
print("No order found for this user.")
|
216 |
-
return "No order found for this user", 400
|
217 |
-
|
218 |
-
order = result["records"][0]
|
219 |
order_details = order.get("Order_Details__c", "")
|
220 |
-
|
221 |
order_items = []
|
222 |
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
"image": menu_item['Ingredient_1__r']['Ingredient_Image__c'],
|
257 |
-
"health_benefits": menu_item['Ingredient_1__r']['Health_Benefits__c'] or "No health benefits available",
|
258 |
-
"fun_facts": menu_item['Ingredient_1__r']['Fun_Facts__c'] or "No fun facts available"
|
259 |
-
})
|
260 |
-
else:
|
261 |
-
print(f"Ingredient 1 missing for {item_name_cleaned}")
|
262 |
-
|
263 |
-
# Check and add Ingredient 2
|
264 |
-
if 'Ingredient_2__r' in menu_item:
|
265 |
-
print(f"Ingredient 2 Found for {item_name_cleaned}: {menu_item['Ingredient_2__r']}")
|
266 |
-
ingredients.append({
|
267 |
-
"name": menu_item['Ingredient_2__r']['Ingredient_Name__c'],
|
268 |
-
"image": menu_item['Ingredient_2__r']['Ingredient_Image__c'],
|
269 |
-
"health_benefits": menu_item['Ingredient_2__r']['Health_Benefits__c'] or "No health benefits available",
|
270 |
-
"fun_facts": menu_item['Ingredient_2__r']['Fun_Facts__c'] or "No fun facts available"
|
271 |
-
})
|
272 |
-
else:
|
273 |
-
print(f"Ingredient 2 missing for {item_name_cleaned}")
|
274 |
-
|
275 |
-
# If ingredients are missing, log the missing item
|
276 |
-
if not ingredients:
|
277 |
-
print(f"No ingredients found for {item_name_cleaned}")
|
278 |
-
|
279 |
-
order_items.append({
|
280 |
-
"name": item_name_cleaned,
|
281 |
-
"price": menu_item.get("Price__c"),
|
282 |
-
"image_url": menu_item.get("Image1__c"),
|
283 |
-
"ingredients": ingredients
|
284 |
})
|
285 |
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
print(f"Total items extracted: {len(order_items)}")
|
293 |
|
294 |
-
# Pass the order_items to the template
|
295 |
return render_template(
|
296 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
order_items=order_items
|
298 |
)
|
299 |
|
300 |
except Exception as e:
|
301 |
-
|
302 |
-
return f"Error querying Salesforce: {str(e)}", 500
|
303 |
|
304 |
|
305 |
|
|
|
120 |
return render_template("login.html")
|
121 |
|
122 |
# Assuming you're fetching the order details and processing them like this
|
123 |
+
@@app.route('/combined_summary')
|
124 |
+
def combined_summary():
|
|
|
125 |
email = session.get('user_email')
|
|
|
126 |
if not email:
|
127 |
return "User not logged in", 400
|
128 |
|
|
|
129 |
try:
|
130 |
+
# ====== FETCH REWARDS ======
|
131 |
+
reward_query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}'"
|
132 |
+
reward_data = sf.query(reward_query)
|
133 |
+
if not reward_data.get("records"):
|
134 |
+
return "Reward info not found", 400
|
135 |
+
|
136 |
+
user_points = reward_data["records"][0].get("Reward_Points__c", 0)
|
137 |
+
|
138 |
+
# Determine tier
|
139 |
+
tiers = {
|
140 |
+
"Bronze": 100,
|
141 |
+
"Silver": 200,
|
142 |
+
"Gold": 300,
|
143 |
+
"Platinum": 500
|
144 |
+
}
|
145 |
+
current_tier, next_tier = "Bronze", "Silver"
|
146 |
+
start_point, end_point = 0, 100
|
147 |
+
if user_points >= 100 and user_points < 200:
|
148 |
+
current_tier, next_tier = "Silver", "Gold"
|
149 |
+
start_point, end_point = 100, 200
|
150 |
+
elif user_points >= 200 and user_points < 300:
|
151 |
+
current_tier, next_tier = "Gold", "Platinum"
|
152 |
+
start_point, end_point = 200, 300
|
153 |
+
elif user_points >= 300:
|
154 |
+
current_tier, next_tier = "Platinum", "N/A"
|
155 |
+
start_point, end_point = 300, 500
|
156 |
+
|
157 |
+
progress_percentage = ((user_points - start_point) / (end_point - start_point)) * 100 if end_point != start_point else 100
|
158 |
+
points_needed_for_next_tier = max(0, end_point - user_points)
|
159 |
+
|
160 |
+
# ====== FETCH ORDER SUMMARY ======
|
161 |
+
order_query = f"""
|
162 |
+
SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c,
|
163 |
+
Order_Status__c, Discount__c, Total_Bill__c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
FROM Order__c
|
165 |
WHERE Customer_Email__c = '{email}'
|
166 |
ORDER BY CreatedDate DESC
|
167 |
LIMIT 1
|
168 |
"""
|
169 |
+
order_result = sf.query(order_query)
|
170 |
+
if not order_result.get("records"):
|
171 |
+
return "No order found", 400
|
172 |
|
173 |
+
order = order_result["records"][0]
|
|
|
|
|
|
|
|
|
174 |
order_details = order.get("Order_Details__c", "")
|
|
|
175 |
order_items = []
|
176 |
|
177 |
+
for line in order_details.split('\n'):
|
178 |
+
item_parts = line.split('|')
|
179 |
+
if len(item_parts) >= 5:
|
180 |
+
item_name_raw = item_parts[0].strip()
|
181 |
+
item_name = ' '.join(item_name_raw.split(' ')[:-1]).strip()
|
182 |
+
|
183 |
+
menu_query = f"""
|
184 |
+
SELECT Name, Price__c, Image1__c,
|
185 |
+
Ingredient_1__r.Ingredient_Name__c, Ingredient_1__r.Ingredient_Image__c,
|
186 |
+
Ingredient_1__r.Health_Benefits__c, Ingredient_1__r.Fun_Facts__c,
|
187 |
+
Ingredient_2__r.Ingredient_Name__c, Ingredient_2__r.Ingredient_Image__c,
|
188 |
+
Ingredient_2__r.Health_Benefits__c, Ingredient_2__r.Fun_Facts__c
|
189 |
+
FROM Menu_Item__c
|
190 |
+
WHERE Name = '{item_name}'
|
191 |
+
"""
|
192 |
+
menu_result = sf.query(menu_query)
|
193 |
+
ingredients = []
|
194 |
+
|
195 |
+
if menu_result.get("records"):
|
196 |
+
menu_item = menu_result["records"][0]
|
197 |
+
if 'Ingredient_1__r' in menu_item:
|
198 |
+
ingredients.append({
|
199 |
+
"name": menu_item['Ingredient_1__r']['Ingredient_Name__c'],
|
200 |
+
"image": menu_item['Ingredient_1__r']['Ingredient_Image__c'],
|
201 |
+
"health_benefits": menu_item['Ingredient_1__r']['Health_Benefits__c'] or "",
|
202 |
+
"fun_facts": menu_item['Ingredient_1__r']['Fun_Facts__c'] or ""
|
203 |
+
})
|
204 |
+
if 'Ingredient_2__r' in menu_item:
|
205 |
+
ingredients.append({
|
206 |
+
"name": menu_item['Ingredient_2__r']['Ingredient_Name__c'],
|
207 |
+
"image": menu_item['Ingredient_2__r']['Ingredient_Image__c'],
|
208 |
+
"health_benefits": menu_item['Ingredient_2__r']['Health_Benefits__c'] or "",
|
209 |
+
"fun_facts": menu_item['Ingredient_2__r']['Fun_Facts__c'] or ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
})
|
211 |
|
212 |
+
order_items.append({
|
213 |
+
"name": item_name,
|
214 |
+
"price": menu_item.get("Price__c"),
|
215 |
+
"image_url": menu_item.get("Image1__c"),
|
216 |
+
"ingredients": ingredients
|
217 |
+
})
|
|
|
218 |
|
|
|
219 |
return render_template(
|
220 |
+
'combined_summary.html',
|
221 |
+
user_points=round(user_points),
|
222 |
+
current_tier=current_tier,
|
223 |
+
next_tier=next_tier,
|
224 |
+
start_point=start_point,
|
225 |
+
end_point=end_point,
|
226 |
+
progress_percentage=round(progress_percentage),
|
227 |
+
points_needed_for_next_tier=round(points_needed_for_next_tier),
|
228 |
order_items=order_items
|
229 |
)
|
230 |
|
231 |
except Exception as e:
|
232 |
+
return f"Error: {str(e)}", 500
|
|
|
233 |
|
234 |
|
235 |
|