Spaces:
Sleeping
Sleeping
Update menu.py
Browse files
menu.py
CHANGED
|
@@ -1,21 +1,11 @@
|
|
| 1 |
from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
|
| 2 |
-
from salesforce import get_salesforce_connection
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
# Configure logging for debugging
|
| 7 |
-
logging.basicConfig(level=logging.INFO)
|
| 8 |
-
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
menu_blueprint = Blueprint('menu', __name__)
|
| 11 |
|
| 12 |
# Initialize Salesforce connection
|
| 13 |
-
|
| 14 |
-
sf = get_salesforce_connection()
|
| 15 |
-
logger.info("Salesforce connection established successfully")
|
| 16 |
-
except Exception as e:
|
| 17 |
-
logger.error(f"Failed to connect to Salesforce: {str(e)}")
|
| 18 |
-
sf = None
|
| 19 |
|
| 20 |
# Constants for video handling
|
| 21 |
STATIC_DIR = os.path.join(os.path.dirname(__file__), 'static')
|
|
@@ -25,50 +15,31 @@ SECTION_ORDER = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "C
|
|
| 25 |
|
| 26 |
# Create placeholder video at startup if it doesn't exist
|
| 27 |
if not os.path.exists(PLACEHOLDER_PATH):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
logger.info(f"Created placeholder video at {PLACEHOLDER_PATH}")
|
| 32 |
-
except Exception as e:
|
| 33 |
-
logger.error(f"Failed to create placeholder video: {str(e)}")
|
| 34 |
|
| 35 |
def get_valid_video_path(item_name, video_url=None):
|
| 36 |
-
"""
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
elif video_url.startswith('069'):
|
| 50 |
-
return f"https://yourdomain.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
|
| 51 |
-
|
| 52 |
-
# Fallback to placeholder.mp4
|
| 53 |
-
if not os.path.exists(PLACEHOLDER_PATH):
|
| 54 |
-
open(PLACEHOLDER_PATH, 'wb').close()
|
| 55 |
-
logger.info(f"Created missing placeholder video at {PLACEHOLDER_PATH}")
|
| 56 |
-
|
| 57 |
-
return f"/static/{PLACEHOLDER_VIDEO}"
|
| 58 |
-
except Exception as e:
|
| 59 |
-
logger.error(f"Error processing video path for {item_name}: {str(e)}")
|
| 60 |
-
return f"/static/{PLACEHOLDER_VIDEO}"
|
| 61 |
|
| 62 |
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
| 63 |
def menu():
|
| 64 |
-
"""
|
| 65 |
-
Renders the menu page with items from Salesforce, organized by section.
|
| 66 |
-
Supports category filtering (All, Veg, Non veg) and user session management.
|
| 67 |
-
"""
|
| 68 |
selected_category = request.args.get("category", "All")
|
| 69 |
user_email = session.get('user_email')
|
| 70 |
|
| 71 |
-
#
|
| 72 |
if not user_email:
|
| 73 |
user_email = request.args.get("email")
|
| 74 |
user_name = request.args.get("name")
|
|
@@ -76,134 +47,99 @@ def menu():
|
|
| 76 |
session['user_email'] = user_email
|
| 77 |
session['user_name'] = user_name
|
| 78 |
else:
|
| 79 |
-
logger.warning("No user email found, redirecting to login")
|
| 80 |
return redirect(url_for("login"))
|
| 81 |
else:
|
| 82 |
user_name = session.get('user_name')
|
| 83 |
|
| 84 |
first_letter = user_name[0].upper() if user_name else "A"
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
logger.warning(f"No user found for email {user_email}, redirecting to login")
|
| 92 |
-
return redirect(url_for('login'))
|
| 93 |
-
|
| 94 |
-
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
|
| 95 |
-
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
|
| 96 |
-
|
| 97 |
-
# Get cart item count
|
| 98 |
-
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
| 99 |
-
cart_count_result = sf.query(cart_query)
|
| 100 |
-
cart_item_count = cart_count_result['totalSize']
|
| 101 |
-
|
| 102 |
-
# Fetch Menu_Item__c records with all required fields
|
| 103 |
-
menu_query = """
|
| 104 |
-
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
| 105 |
-
Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c,
|
| 106 |
-
Ingredientsinfo__c, NutritionalInfo__c, Allergens__c
|
| 107 |
-
FROM Menu_Item__c
|
| 108 |
-
"""
|
| 109 |
-
result = sf.query(menu_query)
|
| 110 |
-
food_items = result.get('records', [])
|
| 111 |
-
logger.info(f"Fetched {len(food_items)} menu items")
|
| 112 |
-
|
| 113 |
-
# Process menu items
|
| 114 |
-
for item in food_items:
|
| 115 |
-
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0)
|
| 116 |
-
item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
|
| 117 |
-
# Ensure defaults for item details
|
| 118 |
-
item['Description__c'] = item.get('Description__c', 'No description available')
|
| 119 |
-
item['Ingredientsinfo__c'] = item.get('Ingredientsinfo__c', 'Not specified')
|
| 120 |
-
item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', 'Not available')
|
| 121 |
-
item['Allergens__c'] = item.get('Allergens__c', 'None listed')
|
| 122 |
-
|
| 123 |
-
# Fetch Custom_Dish__c records
|
| 124 |
-
custom_dish_query = """
|
| 125 |
-
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
| 126 |
-
Veg_NonVeg__c, Section__c, Total_Ordered__c,
|
| 127 |
-
Ingredientsinfo__c, NutritionalInfo__c, Allergens__c
|
| 128 |
-
FROM Custom_Dish__c
|
| 129 |
-
WHERE CreatedDate >= LAST_N_DAYS:7
|
| 130 |
-
"""
|
| 131 |
-
custom_dish_result = sf.query(custom_dish_query)
|
| 132 |
-
custom_dishes = custom_dish_result.get('records', [])
|
| 133 |
-
logger.info(f"Fetched {len(custom_dishes)} custom dishes")
|
| 134 |
-
|
| 135 |
-
# Process custom dishes
|
| 136 |
-
for item in custom_dishes:
|
| 137 |
-
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0)
|
| 138 |
-
item['Video1__c'] = get_valid_video_path(item['Name'])
|
| 139 |
-
# Ensure defaults for item details
|
| 140 |
-
item['Description__c'] = item.get('Description__c', 'No description available')
|
| 141 |
-
item['Ingredientsinfo__c'] = item.get('Ingredientsinfo__c', 'Not specified')
|
| 142 |
-
item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', 'Not available')
|
| 143 |
-
item['Allergens__c'] = item.get('Allergens__c', 'None listed')
|
| 144 |
-
|
| 145 |
-
# Merge menu items and custom dishes
|
| 146 |
-
all_items = food_items + custom_dishes
|
| 147 |
-
ordered_menu = {section: [] for section in SECTION_ORDER}
|
| 148 |
-
|
| 149 |
-
# Populate Best Sellers (top 4 by order count)
|
| 150 |
-
best_sellers = sorted(all_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True)
|
| 151 |
-
if selected_category == "Veg":
|
| 152 |
-
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
|
| 153 |
-
elif selected_category == "Non veg":
|
| 154 |
-
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
|
| 155 |
-
ordered_menu["Best Sellers"] = best_sellers[:4]
|
| 156 |
-
|
| 157 |
-
# Organize items by section
|
| 158 |
-
added_item_names = set()
|
| 159 |
-
for item in all_items:
|
| 160 |
-
section = item.get("Section__c", "Others")
|
| 161 |
-
if section not in ordered_menu:
|
| 162 |
-
ordered_menu[section] = []
|
| 163 |
-
|
| 164 |
-
if item['Name'] in added_item_names:
|
| 165 |
-
continue
|
| 166 |
-
|
| 167 |
-
if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]:
|
| 168 |
-
continue
|
| 169 |
-
if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]:
|
| 170 |
-
continue
|
| 171 |
-
|
| 172 |
-
ordered_menu[section].append(item)
|
| 173 |
-
added_item_names.add(item['Name'])
|
| 174 |
-
|
| 175 |
-
# Remove empty sections
|
| 176 |
-
ordered_menu = {section: items for section, items in ordered_menu.items() if items}
|
| 177 |
-
categories = ["All", "Veg", "Non veg"]
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
]
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
return render_template(
|
| 209 |
"menu.html",
|
|
@@ -219,14 +155,10 @@ def menu():
|
|
| 219 |
|
| 220 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|
| 221 |
def get_addons():
|
| 222 |
-
"""
|
| 223 |
-
Fetches customization options for a menu item based on its section.
|
| 224 |
-
"""
|
| 225 |
item_name = request.args.get('item_name')
|
| 226 |
item_section = request.args.get('item_section')
|
| 227 |
|
| 228 |
if not item_name or not item_section:
|
| 229 |
-
logger.warning("Missing item_name or item_section in addons request")
|
| 230 |
return jsonify({"success": False, "error": "Item name and section are required."}), 400
|
| 231 |
|
| 232 |
try:
|
|
@@ -235,25 +167,116 @@ def get_addons():
|
|
| 235 |
FROM Customization_Options__c
|
| 236 |
WHERE Section__c = '{item_section}'
|
| 237 |
"""
|
| 238 |
-
result = sf.
|
| 239 |
addons = result.get('records', [])
|
| 240 |
|
| 241 |
if not addons:
|
| 242 |
-
return jsonify({"success": False, "error": "No customization options found."}), 404
|
| 243 |
|
| 244 |
-
formatted_addons = [
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
| 249 |
"max_selections": addon.get("Max_Selections__c", 1),
|
| 250 |
"extra_charge": addon.get("Extra_Charge__c", False),
|
| 251 |
"extra_charge_amount": addon.get("Extra_Charge_Amount__c", 0)
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
]
|
| 255 |
return jsonify({"success": True, "addons": formatted_addons})
|
| 256 |
|
| 257 |
except Exception as e:
|
| 258 |
-
|
| 259 |
-
return jsonify({"success": False, "error": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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__)
|
| 6 |
|
| 7 |
# Initialize Salesforce connection
|
| 8 |
+
sf = get_salesforce_connection()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Constants for video handling
|
| 11 |
STATIC_DIR = os.path.join(os.path.dirname(__file__), 'static')
|
|
|
|
| 15 |
|
| 16 |
# Create placeholder video at startup if it doesn't exist
|
| 17 |
if not os.path.exists(PLACEHOLDER_PATH):
|
| 18 |
+
with open(PLACEHOLDER_PATH, 'wb') as f:
|
| 19 |
+
f.close()
|
| 20 |
+
print(f"Created placeholder video at {PLACEHOLDER_PATH}")
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def get_valid_video_path(item_name, video_url=None):
|
| 23 |
+
"""Get valid video path for item with placeholder fallback."""
|
| 24 |
+
if video_url:
|
| 25 |
+
if video_url.startswith(('http://', 'https://')):
|
| 26 |
+
return video_url
|
| 27 |
+
elif video_url.startswith('/'):
|
| 28 |
+
return video_url
|
| 29 |
+
elif video_url.startswith('069'):
|
| 30 |
+
return f"https://biryanihub-dev-ed.develop.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
|
| 31 |
+
if not os.path.exists(PLACEHOLDER_PATH):
|
| 32 |
+
with open(PLACEHOLDER_PATH, 'wb') as f:
|
| 33 |
+
f.close()
|
| 34 |
+
print(f"Created missing placeholder video at {PLACEHOLDER_PATH}")
|
| 35 |
+
return f"/static/{PLACEHOLDER_VIDEO}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
@menu_blueprint.route("/menu", methods=["GET", "POST"])
|
| 38 |
def menu():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
selected_category = request.args.get("category", "All")
|
| 40 |
user_email = session.get('user_email')
|
| 41 |
|
| 42 |
+
# Handle user authentication
|
| 43 |
if not user_email:
|
| 44 |
user_email = request.args.get("email")
|
| 45 |
user_name = request.args.get("name")
|
|
|
|
| 47 |
session['user_email'] = user_email
|
| 48 |
session['user_name'] = user_name
|
| 49 |
else:
|
|
|
|
| 50 |
return redirect(url_for("login"))
|
| 51 |
else:
|
| 52 |
user_name = session.get('user_name')
|
| 53 |
|
| 54 |
first_letter = user_name[0].upper() if user_name else "A"
|
| 55 |
|
| 56 |
+
# Fetch user referral and reward points
|
| 57 |
+
user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
| 58 |
+
user_result = sf.query(user_query)
|
| 59 |
+
if not user_result.get('records'):
|
| 60 |
+
return redirect(url_for('login'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
|
| 63 |
+
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
|
| 64 |
+
|
| 65 |
+
# Get cart item count
|
| 66 |
+
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
|
| 67 |
+
cart_count_result = sf.query(cart_query)
|
| 68 |
+
cart_item_count = cart_count_result.get('totalSize', 0)
|
| 69 |
+
|
| 70 |
+
# Fetch all Menu_Item__c records with required fields
|
| 71 |
+
menu_query = """
|
| 72 |
+
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
| 73 |
+
Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c,
|
| 74 |
+
IngredientsInfo__c, NutritionalInfo__c, Allergens__c
|
| 75 |
+
FROM Menu_Item__c
|
| 76 |
+
"""
|
| 77 |
+
menu_result = sf.query_all(menu_query)
|
| 78 |
+
food_items = menu_result.get('records', [])
|
| 79 |
+
|
| 80 |
+
# Process menu items
|
| 81 |
+
for item in food_items:
|
| 82 |
+
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
|
| 83 |
+
item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
|
| 84 |
+
item['Section__c'] = item.get('Section__c', "Others")
|
| 85 |
+
item['Description__c'] = item.get('Description__c', "No description available")
|
| 86 |
+
item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified")
|
| 87 |
+
item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available")
|
| 88 |
+
item['Allergens__c'] = item.get('Allergens__c', "None listed")
|
| 89 |
+
item['is_menu_item'] = True # Flag to identify Menu_Item__c records
|
| 90 |
+
|
| 91 |
+
# Fetch all Custom_Dish__c records with only existing fields
|
| 92 |
+
custom_dish_query = """
|
| 93 |
+
SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
|
| 94 |
+
Veg_NonVeg__c, Section__c, Total_Ordered__c
|
| 95 |
+
FROM Custom_Dish__c
|
| 96 |
+
WHERE CreatedDate >= LAST_N_DAYS:7
|
| 97 |
+
"""
|
| 98 |
+
custom_dish_result = sf.query_all(custom_dish_query)
|
| 99 |
+
custom_dishes = custom_dish_result.get('records', [])
|
| 100 |
+
|
| 101 |
+
# Process custom dishes
|
| 102 |
+
for item in custom_dishes:
|
| 103 |
+
item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
|
| 104 |
+
item['Video1__c'] = get_valid_video_path(item['Name'])
|
| 105 |
+
item['Section__c'] = item.get('Section__c', "Customized dish")
|
| 106 |
+
item['Description__c'] = item.get('Description__c', "No description available")
|
| 107 |
+
item['is_menu_item'] = False # Flag to identify Custom_Dish__c records
|
| 108 |
+
|
| 109 |
+
# Merge all items
|
| 110 |
+
all_items = food_items + custom_dishes
|
| 111 |
+
ordered_menu = {section: [] for section in SECTION_ORDER}
|
| 112 |
+
|
| 113 |
+
# Process best sellers
|
| 114 |
+
best_sellers = sorted(all_items, key=lambda x: x['Total_Ordered__c'], reverse=True)
|
| 115 |
+
if selected_category == "Veg":
|
| 116 |
+
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
|
| 117 |
+
elif selected_category == "Non veg":
|
| 118 |
+
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
|
| 119 |
+
ordered_menu["Best Sellers"] = best_sellers[:4]
|
| 120 |
+
|
| 121 |
+
# Organize items into sections
|
| 122 |
+
added_item_names = set()
|
| 123 |
+
for item in all_items:
|
| 124 |
+
section = item['Section__c']
|
| 125 |
+
if section not in ordered_menu:
|
| 126 |
+
ordered_menu[section] = []
|
| 127 |
+
|
| 128 |
+
if item['Name'] in added_item_names:
|
| 129 |
+
continue
|
| 130 |
+
|
| 131 |
+
veg_nonveg = item.get("Veg_NonVeg__c", "both")
|
| 132 |
+
if selected_category == "Veg" and veg_nonveg not in ["Veg", "both"]:
|
| 133 |
+
continue
|
| 134 |
+
if selected_category == "Non veg" and veg_nonveg not in ["Non veg", "both"]:
|
| 135 |
+
continue
|
| 136 |
+
|
| 137 |
+
ordered_menu[section].append(item)
|
| 138 |
+
added_item_names.add(item['Name'])
|
| 139 |
+
|
| 140 |
+
# Remove empty sections
|
| 141 |
+
ordered_menu = {section: items for section, items in ordered_menu.items() if items}
|
| 142 |
+
categories = ["All", "Veg", "Non veg"]
|
| 143 |
|
| 144 |
return render_template(
|
| 145 |
"menu.html",
|
|
|
|
| 155 |
|
| 156 |
@menu_blueprint.route('/api/addons', methods=['GET'])
|
| 157 |
def get_addons():
|
|
|
|
|
|
|
|
|
|
| 158 |
item_name = request.args.get('item_name')
|
| 159 |
item_section = request.args.get('item_section')
|
| 160 |
|
| 161 |
if not item_name or not item_section:
|
|
|
|
| 162 |
return jsonify({"success": False, "error": "Item name and section are required."}), 400
|
| 163 |
|
| 164 |
try:
|
|
|
|
| 167 |
FROM Customization_Options__c
|
| 168 |
WHERE Section__c = '{item_section}'
|
| 169 |
"""
|
| 170 |
+
result = sf.query_all(query)
|
| 171 |
addons = result.get('records', [])
|
| 172 |
|
| 173 |
if not addons:
|
| 174 |
+
return jsonify({"success": False, "error": "No customization options found for the given section."}), 404
|
| 175 |
|
| 176 |
+
formatted_addons = []
|
| 177 |
+
for addon in addons:
|
| 178 |
+
options = addon.get("Options__c", "")
|
| 179 |
+
options = options.split(", ") if options else []
|
| 180 |
+
formatted_addons.append({
|
| 181 |
+
"name": addon.get("Name", ""),
|
| 182 |
+
"type": addon.get("Customization_Type__c", ""),
|
| 183 |
+
"options": options,
|
| 184 |
"max_selections": addon.get("Max_Selections__c", 1),
|
| 185 |
"extra_charge": addon.get("Extra_Charge__c", False),
|
| 186 |
"extra_charge_amount": addon.get("Extra_Charge_Amount__c", 0)
|
| 187 |
+
})
|
| 188 |
+
|
|
|
|
| 189 |
return jsonify({"success": True, "addons": formatted_addons})
|
| 190 |
|
| 191 |
except Exception as e:
|
| 192 |
+
print(f"Error fetching addons: {str(e)}")
|
| 193 |
+
return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
|
| 194 |
+
|
| 195 |
+
@menu_blueprint.route('/cart/add', methods=['POST'])
|
| 196 |
+
def add_to_cart():
|
| 197 |
+
try:
|
| 198 |
+
data = request.json
|
| 199 |
+
item_name = data.get('itemName', '').strip()
|
| 200 |
+
item_price = float(data.get('itemPrice', 0))
|
| 201 |
+
item_image = data.get('itemImage', '')
|
| 202 |
+
addons = data.get('addons', [])
|
| 203 |
+
instructions = data.get('instructions', '')
|
| 204 |
+
category = data.get('category', '')
|
| 205 |
+
section = data.get('section', '')
|
| 206 |
+
quantity = int(data.get('quantity', 1))
|
| 207 |
+
customer_email = session.get('user_email')
|
| 208 |
+
|
| 209 |
+
if not item_name or not item_price or not customer_email:
|
| 210 |
+
return jsonify({"success": False, "error": "Item name, price, and user email are required."}), 400
|
| 211 |
+
|
| 212 |
+
query = f"""
|
| 213 |
+
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c, Price__c
|
| 214 |
+
FROM Cart_Item__c
|
| 215 |
+
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
| 216 |
+
"""
|
| 217 |
+
result = sf.query(query)
|
| 218 |
+
cart_items = result.get("records", [])
|
| 219 |
+
|
| 220 |
+
addons_price = sum(float(addon.get('price', 0)) for addon in addons)
|
| 221 |
+
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) if addons else "None"
|
| 222 |
+
|
| 223 |
+
if cart_items:
|
| 224 |
+
cart_item = cart_items[0]
|
| 225 |
+
cart_item_id = cart_item['Id']
|
| 226 |
+
existing_quantity = int(cart_item.get('Quantity__c', 0))
|
| 227 |
+
existing_addons = cart_item.get('Add_Ons__c', "None")
|
| 228 |
+
existing_addons_price = float(cart_item.get('Add_Ons_Price__c', 0))
|
| 229 |
+
existing_instructions = cart_item.get('Instructions__c', "")
|
| 230 |
+
|
| 231 |
+
combined_addons = existing_addons if existing_addons != "None" else ""
|
| 232 |
+
if new_addons != "None":
|
| 233 |
+
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
| 234 |
+
|
| 235 |
+
combined_instructions = existing_instructions
|
| 236 |
+
if instructions:
|
| 237 |
+
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
| 238 |
+
|
| 239 |
+
combined_addons_list = combined_addons.split("; ")
|
| 240 |
+
combined_addons_price = sum(
|
| 241 |
+
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
total_price = (existing_quantity + quantity) * item_price + combined_addons_price
|
| 245 |
+
|
| 246 |
+
sf.Cart_Item__c.update(cart_item_id, {
|
| 247 |
+
"Quantity__c": existing_quantity + quantity,
|
| 248 |
+
"Add_Ons__c": combined_addons,
|
| 249 |
+
"Add_Ons_Price__c": combined_addons_price,
|
| 250 |
+
"Instructions__c": combined_instructions,
|
| 251 |
+
"Price__c": total_price,
|
| 252 |
+
"Category__c": category,
|
| 253 |
+
"Section__c": section
|
| 254 |
+
})
|
| 255 |
+
else:
|
| 256 |
+
total_price = item_price * quantity + addons_price
|
| 257 |
+
sf.Cart_Item__c.create({
|
| 258 |
+
"Name": item_name,
|
| 259 |
+
"Price__c": total_price,
|
| 260 |
+
"Base_Price__c": item_price,
|
| 261 |
+
"Quantity__c": quantity,
|
| 262 |
+
"Add_Ons_Price__c": addons_price,
|
| 263 |
+
"Add_Ons__c": new_addons,
|
| 264 |
+
"Image1__c": item_image,
|
| 265 |
+
"Customer_Email__c": customer_email,
|
| 266 |
+
"Instructions__c": instructions,
|
| 267 |
+
"Category__c": category,
|
| 268 |
+
"Section__c": section
|
| 269 |
+
})
|
| 270 |
+
|
| 271 |
+
# Fetch updated cart for UI update
|
| 272 |
+
cart_query = f"SELECT Name, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{customer_email}'"
|
| 273 |
+
cart_result = sf.query_all(cart_query)
|
| 274 |
+
cart = [{"itemName": item["Name"], "quantity": item["Quantity__c"]} for item in cart_result.get("records", [])]
|
| 275 |
+
|
| 276 |
+
return jsonify({"success": True, "message": "Item added to cart successfully.", "cart": cart})
|
| 277 |
+
|
| 278 |
+
except ValueError as e:
|
| 279 |
+
return jsonify({"success": False, "error": f"Invalid data format: {str(e)}"}), 400
|
| 280 |
+
except Exception as e:
|
| 281 |
+
print(f"Error adding item to cart: {str(e)}")
|
| 282 |
+
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|