Update menu.py
Browse files
menu.py
CHANGED
@@ -60,26 +60,49 @@ def menu():
|
|
60 |
first_letter = user_name[0].upper() if user_name else "A"
|
61 |
user_image = session.get('user_image') # Add avatar image from session
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
# Fetch user referral and reward points
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
68 |
|
69 |
user_record = user_result['records'][0]
|
70 |
user_id = user_record['Id']
|
71 |
referral_code = user_record.get('Referral__c', 'N/A')
|
72 |
reward_points = user_record.get('Reward_Points__c', 0)
|
73 |
|
74 |
-
# If no session image, check Salesforce for stored avatar
|
75 |
-
if not user_image and user_record.get('Avatar__c'):
|
76 |
session['user_image'] = user_record['Avatar__c']
|
77 |
user_image = session['user_image']
|
78 |
|
79 |
# Get cart item count
|
80 |
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# Fetch all Menu_Item__c records with required fields
|
85 |
menu_query = """
|
@@ -88,8 +111,12 @@ def menu():
|
|
88 |
IngredientsInfo__c, NutritionalInfo__c, Allergens__c
|
89 |
FROM Menu_Item__c
|
90 |
"""
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
93 |
|
94 |
# Process menu items
|
95 |
for item in food_items:
|
@@ -109,8 +136,12 @@ def menu():
|
|
109 |
FROM Custom_Dish__c
|
110 |
WHERE CreatedDate >= LAST_N_DAYS:7
|
111 |
"""
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
114 |
|
115 |
# Process custom dishes
|
116 |
for item in custom_dishes:
|
@@ -203,7 +234,7 @@ def upload_avatar():
|
|
203 |
session['user_image'] = image_data
|
204 |
logger.info("Image stored in session successfully")
|
205 |
|
206 |
-
# Store in Salesforce (
|
207 |
user_email = session.get('user_email')
|
208 |
if user_email:
|
209 |
try:
|
@@ -211,8 +242,14 @@ def upload_avatar():
|
|
211 |
user_result = sf.query(user_query)
|
212 |
if user_result.get('records'):
|
213 |
user_id = user_result['records'][0]['Id']
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
else:
|
217 |
logger.warning(f"User not found in Salesforce: {user_email}")
|
218 |
except Exception as e:
|
@@ -238,14 +275,20 @@ def delete_avatar():
|
|
238 |
session.pop('user_image', None)
|
239 |
logger.info("Image removed from session")
|
240 |
|
241 |
-
# Remove from Salesforce
|
242 |
try:
|
243 |
user_query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
244 |
user_result = sf.query(user_query)
|
245 |
if user_result.get('records'):
|
246 |
user_id = user_result['records'][0]['Id']
|
247 |
-
|
248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
else:
|
250 |
logger.warning(f"User not found in Salesforce: {user_email}")
|
251 |
except Exception as e:
|
|
|
60 |
first_letter = user_name[0].upper() if user_name else "A"
|
61 |
user_image = session.get('user_image') # Add avatar image from session
|
62 |
|
63 |
+
# Check if Avatar__c field exists on Customer_Login__c
|
64 |
+
try:
|
65 |
+
describe_result = sf.Customer_Login__c.describe()
|
66 |
+
fields = [field['name'] for field in describe_result['fields']]
|
67 |
+
avatar_field_exists = 'Avatar__c' in fields
|
68 |
+
except Exception as e:
|
69 |
+
logger.error(f"Error describing Customer_Login__c object: {str(e)}")
|
70 |
+
avatar_field_exists = False
|
71 |
+
|
72 |
+
# Build the SOQL query dynamically based on field availability
|
73 |
+
query_fields = ["Id", "Referral__c", "Reward_Points__c"]
|
74 |
+
if avatar_field_exists:
|
75 |
+
query_fields.append("Avatar__c")
|
76 |
+
user_query = f"SELECT {', '.join(query_fields)} FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
77 |
+
|
78 |
# Fetch user referral and reward points
|
79 |
+
try:
|
80 |
+
user_result = sf.query(user_query)
|
81 |
+
if not user_result.get('records'):
|
82 |
+
logger.warning(f"No user found with email: {user_email}")
|
83 |
+
return redirect(url_for('login'))
|
84 |
+
except Exception as e:
|
85 |
+
logger.error(f"Error querying user data: {str(e)}")
|
86 |
+
return jsonify({"success": False, "error": "Failed to fetch user data from Salesforce"}), 500
|
87 |
|
88 |
user_record = user_result['records'][0]
|
89 |
user_id = user_record['Id']
|
90 |
referral_code = user_record.get('Referral__c', 'N/A')
|
91 |
reward_points = user_record.get('Reward_Points__c', 0)
|
92 |
|
93 |
+
# If no session image, check Salesforce for stored avatar (if field exists)
|
94 |
+
if not user_image and avatar_field_exists and user_record.get('Avatar__c'):
|
95 |
session['user_image'] = user_record['Avatar__c']
|
96 |
user_image = session['user_image']
|
97 |
|
98 |
# Get cart item count
|
99 |
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
100 |
+
try:
|
101 |
+
cart_count_result = sf.query(cart_query)
|
102 |
+
cart_item_count = cart_count_result.get('totalSize', 0)
|
103 |
+
except Exception as e:
|
104 |
+
logger.error(f"Error fetching cart item count: {str(e)}")
|
105 |
+
cart_item_count = 0
|
106 |
|
107 |
# Fetch all Menu_Item__c records with required fields
|
108 |
menu_query = """
|
|
|
111 |
IngredientsInfo__c, NutritionalInfo__c, Allergens__c
|
112 |
FROM Menu_Item__c
|
113 |
"""
|
114 |
+
try:
|
115 |
+
menu_result = sf.query_all(menu_query)
|
116 |
+
food_items = menu_result.get('records', [])
|
117 |
+
except Exception as e:
|
118 |
+
logger.error(f"Error fetching menu items: {str(e)}")
|
119 |
+
food_items = []
|
120 |
|
121 |
# Process menu items
|
122 |
for item in food_items:
|
|
|
136 |
FROM Custom_Dish__c
|
137 |
WHERE CreatedDate >= LAST_N_DAYS:7
|
138 |
"""
|
139 |
+
try:
|
140 |
+
custom_dish_result = sf.query_all(custom_dish_query)
|
141 |
+
custom_dishes = custom_dish_result.get('records', [])
|
142 |
+
except Exception as e:
|
143 |
+
logger.error(f"Error fetching custom dishes: {str(e)}")
|
144 |
+
custom_dishes = []
|
145 |
|
146 |
# Process custom dishes
|
147 |
for item in custom_dishes:
|
|
|
234 |
session['user_image'] = image_data
|
235 |
logger.info("Image stored in session successfully")
|
236 |
|
237 |
+
# Store in Salesforce (if Avatar__c field exists)
|
238 |
user_email = session.get('user_email')
|
239 |
if user_email:
|
240 |
try:
|
|
|
242 |
user_result = sf.query(user_query)
|
243 |
if user_result.get('records'):
|
244 |
user_id = user_result['records'][0]['Id']
|
245 |
+
# Check if Avatar__c field exists before updating
|
246 |
+
describe_result = sf.Customer_Login__c.describe()
|
247 |
+
fields = [field['name'] for field in describe_result['fields']]
|
248 |
+
if 'Avatar__c' in fields:
|
249 |
+
sf.Customer_Login__c.update(user_id, {'Avatar__c': image_data})
|
250 |
+
logger.info(f"Image stored in Salesforce for user {user_email}")
|
251 |
+
else:
|
252 |
+
logger.warning("Avatar__c field does not exist; skipping Salesforce update")
|
253 |
else:
|
254 |
logger.warning(f"User not found in Salesforce: {user_email}")
|
255 |
except Exception as e:
|
|
|
275 |
session.pop('user_image', None)
|
276 |
logger.info("Image removed from session")
|
277 |
|
278 |
+
# Remove from Salesforce (if Avatar__c field exists)
|
279 |
try:
|
280 |
user_query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
281 |
user_result = sf.query(user_query)
|
282 |
if user_result.get('records'):
|
283 |
user_id = user_result['records'][0]['Id']
|
284 |
+
# Check if Avatar__c field exists before updating
|
285 |
+
describe_result = sf.Customer_Login__c.describe()
|
286 |
+
fields = [field['name'] for field in describe_result['fields']]
|
287 |
+
if 'Avatar__c' in fields:
|
288 |
+
sf.Customer_Login__c.update(user_id, {'Avatar__c': None})
|
289 |
+
logger.info(f"Image removed from Salesforce for user {user_email}")
|
290 |
+
else:
|
291 |
+
logger.warning("Avatar__c field does not exist; skipping Salesforce update")
|
292 |
else:
|
293 |
logger.warning(f"User not found in Salesforce: {user_email}")
|
294 |
except Exception as e:
|