Spaces:
Sleeping
Sleeping
Create combined_summary.py
Browse files- combined_summary.py +166 -0
combined_summary.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Blueprint, render_template, session, redirect, url_for
|
2 |
+
from salesforce import get_salesforce_connection
|
3 |
+
from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
from salesforce import get_salesforce_connection
|
7 |
+
|
8 |
+
|
9 |
+
combined_summary_blueprint = Blueprint('combined_summary', __name__)
|
10 |
+
|
11 |
+
# Initialize Salesforce connection
|
12 |
+
sf = get_salesforce_connection()
|
13 |
+
|
14 |
+
def escape_soql(value):
|
15 |
+
"""Escape single quotes in SOQL query values to prevent injection."""
|
16 |
+
if value:
|
17 |
+
return value.replace("'", "\\'")
|
18 |
+
return value
|
19 |
+
|
20 |
+
@combined_summary_blueprint.route('/combined_summary')
|
21 |
+
def combined_summary():
|
22 |
+
email = session.get('user_email')
|
23 |
+
if not email:
|
24 |
+
print("No user email in session, redirecting to login")
|
25 |
+
return redirect(url_for('login'))
|
26 |
+
|
27 |
+
try:
|
28 |
+
# Sanitize email for SOQL query
|
29 |
+
safe_email = escape_soql(email)
|
30 |
+
|
31 |
+
# ====== FETCH REWARDS ======
|
32 |
+
reward_query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{safe_email}'"
|
33 |
+
reward_data = sf.query_all(reward_query)
|
34 |
+
if not reward_data.get("records"):
|
35 |
+
print(f"No reward info found for email: {email}")
|
36 |
+
return "Reward info not found", 404
|
37 |
+
|
38 |
+
user_points = reward_data["records"][0].get("Reward_Points__c", 0)
|
39 |
+
|
40 |
+
# Determine tier
|
41 |
+
tiers = {
|
42 |
+
"Bronze": 100,
|
43 |
+
"Silver": 200,
|
44 |
+
"Gold": 300,
|
45 |
+
"Platinum": 500
|
46 |
+
}
|
47 |
+
current_tier, next_tier = "Bronze", "Silver"
|
48 |
+
start_point, end_point = 0, 100
|
49 |
+
if user_points >= 100 and user_points < 200:
|
50 |
+
current_tier, next_tier = "Silver", "Gold"
|
51 |
+
start_point, end_point = 100, 200
|
52 |
+
elif user_points >= 200 and user_points < 300:
|
53 |
+
current_tier, next_tier = "Gold", "Platinum"
|
54 |
+
start_point, end_point = 200, 300
|
55 |
+
elif user_points >= 300:
|
56 |
+
current_tier, next_tier = "Platinum", "N/A"
|
57 |
+
start_point, end_point = 300, 500
|
58 |
+
|
59 |
+
progress_percentage = ((user_points - start_point) / (end_point - start_point)) * 100 if end_point != start_point else 100
|
60 |
+
points_needed_for_next_tier = max(0, end_point - user_points)
|
61 |
+
|
62 |
+
# ====== FETCH ORDER SUMMARY ======
|
63 |
+
order_query = f"""
|
64 |
+
SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c,
|
65 |
+
Order_Status__c, Discount__c, Total_Bill__c
|
66 |
+
FROM Order__c
|
67 |
+
WHERE Customer_Email__c = '{safe_email}'
|
68 |
+
ORDER BY CreatedDate DESC
|
69 |
+
LIMIT 1
|
70 |
+
"""
|
71 |
+
order_result = sf.query_all(order_query)
|
72 |
+
if not order_result.get("records"):
|
73 |
+
print(f"No order found for email: {email}")
|
74 |
+
return "No order found", 404
|
75 |
+
|
76 |
+
order = order_result["records"][0]
|
77 |
+
order_details = order.get("Order_Details__c", "")
|
78 |
+
order_items = []
|
79 |
+
sector_names = set() # Use a set to ensure sector names are unique
|
80 |
+
|
81 |
+
for line in order_details.split('\n'):
|
82 |
+
item_parts = line.split('|')
|
83 |
+
if len(item_parts) >= 5:
|
84 |
+
item_name_raw = item_parts[0].strip()
|
85 |
+
item_name = ' '.join(item_name_raw.split(' ')[:-1]).strip()
|
86 |
+
safe_item_name = escape_soql(item_name)
|
87 |
+
|
88 |
+
menu_query = f"""
|
89 |
+
SELECT Name, Price__c, Image1__c,
|
90 |
+
Ingredient_1__r.Ingredient_Name__c, Ingredient_1__r.Ingredient_Image__c,
|
91 |
+
Ingredient_1__r.Health_Benefits__c, Ingredient_1__r.Fun_Facts__c,
|
92 |
+
Ingredient_2__r.Ingredient_Name__c, Ingredient_2__r.Ingredient_Image__c,
|
93 |
+
Ingredient_2__r.Health_Benefits__c, Ingredient_2__r.Fun_Facts__c,
|
94 |
+
Sector__c
|
95 |
+
FROM Menu_Item__c
|
96 |
+
WHERE Name = '{safe_item_name}'
|
97 |
+
"""
|
98 |
+
menu_result = sf.query_all(menu_query)
|
99 |
+
ingredients = []
|
100 |
+
|
101 |
+
if menu_result.get("records"):
|
102 |
+
menu_item = menu_result["records"][0]
|
103 |
+
|
104 |
+
# Process Ingredient 1 if it exists
|
105 |
+
if menu_item.get('Ingredient_1__r') is not None:
|
106 |
+
ingredients.append({
|
107 |
+
"name": menu_item['Ingredient_1__r'].get('Ingredient_Name__c', ''),
|
108 |
+
"image": menu_item['Ingredient_1__r'].get('Ingredient_Image__c', ''),
|
109 |
+
"health_benefits": menu_item['Ingredient_1__r'].get('Health_Benefits__c', ''),
|
110 |
+
"fun_facts": menu_item['Ingredient_1__r'].get('Fun_Facts__c', '')
|
111 |
+
})
|
112 |
+
|
113 |
+
# Process Ingredient 2 if it exists
|
114 |
+
if menu_item.get('Ingredient_2__r') is not None:
|
115 |
+
ingredients.append({
|
116 |
+
"name": menu_item['Ingredient_2__r'].get('Ingredient_Name__c', ''),
|
117 |
+
"image": menu_item['Ingredient_2__r'].get('Ingredient_Image__c', ''),
|
118 |
+
"health_benefits": menu_item['Ingredient_2__r'].get('Health_Benefits__c', ''),
|
119 |
+
"fun_facts": menu_item['Ingredient_2__r'].get('Fun_Facts__c', '')
|
120 |
+
})
|
121 |
+
|
122 |
+
# Process the Sector__c field from Menu_Item__c
|
123 |
+
if menu_item.get('Sector__c'):
|
124 |
+
sector_names.update(menu_item['Sector__c'].split(',')) # Add sectors to the set
|
125 |
+
|
126 |
+
# Only add the item if ingredients are present
|
127 |
+
order_items.append({
|
128 |
+
"name": item_name,
|
129 |
+
"price": menu_item.get("Price__c", 0),
|
130 |
+
"image_url": menu_item.get("Image1__c", ''),
|
131 |
+
"ingredients": ingredients
|
132 |
+
})
|
133 |
+
|
134 |
+
# Fetch the sector details from the Sector_Detail__c object
|
135 |
+
sector_details = {}
|
136 |
+
for sector_name in sector_names:
|
137 |
+
safe_sector_name = escape_soql(sector_name.strip())
|
138 |
+
sector_query = f"""
|
139 |
+
SELECT Name, Image_URL__c, Description__c
|
140 |
+
FROM Sector_Detail__c
|
141 |
+
WHERE Name = '{safe_sector_name}'
|
142 |
+
"""
|
143 |
+
sector_result = sf.query_all(sector_query)
|
144 |
+
if sector_result.get("records"):
|
145 |
+
sector_record = sector_result["records"][0]
|
146 |
+
sector_details[sector_name] = {
|
147 |
+
"image_url": sector_record.get('Image_URL__c', ''),
|
148 |
+
"description": sector_record.get('Description__c', '')
|
149 |
+
}
|
150 |
+
|
151 |
+
return render_template(
|
152 |
+
'combined_summary.html',
|
153 |
+
user_points=round(user_points),
|
154 |
+
current_tier=current_tier,
|
155 |
+
next_tier=next_tier,
|
156 |
+
start_point=start_point,
|
157 |
+
end_point=end_point,
|
158 |
+
progress_percentage=round(progress_percentage),
|
159 |
+
points_needed_for_next_tier=round(points_needed_for_next_tier),
|
160 |
+
order_items=order_items,
|
161 |
+
sector_details=sector_details
|
162 |
+
)
|
163 |
+
|
164 |
+
except Exception as e:
|
165 |
+
print(f"Error in combined_summary: {str(e)}")
|
166 |
+
return f"Error: {str(e)}", 500
|