geethareddy's picture
Update menu.py
ac72660 verified
from flask import Blueprint, render_template, request, session, jsonify
from flask import redirect, url_for
from salesforce import get_salesforce_connection
menu_blueprint = Blueprint('menu', __name__)
# Initialize Salesforce connection
sf = get_salesforce_connection()
@menu_blueprint.route("/menu", methods=["GET", "POST"])
def menu():
selected_category = request.args.get("category", "All")
user_email = session.get('user_email')
if not user_email:
user_email = request.args.get("email")
user_name = request.args.get("name")
if user_email:
session['user_email'] = user_email
session['user_name'] = user_name # Store name in session
else:
return redirect(url_for("login"))
else:
user_name = session.get('user_name') # Get name from session if it's already stored
# Get the first letter of the user's name (make it uppercase for consistency)
first_letter = user_name[0].upper() if user_name else "A"
try:
# Fetch user referral and reward points
user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
user_result = sf.query(user_query)
if not user_result['records']:
return redirect(url_for('login'))
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
# Query to fetch Menu_Item__c records including Total_Ordered__c for best sellers
menu_query = """
SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c, Video_URL__c
FROM Menu_Item__c
"""
result = sf.query(menu_query)
food_items = result['records'] if 'records' in result else []
# Ensure Total_Ordered__c has a valid value
for item in food_items:
if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None:
item['Total_Ordered__c'] = 0 # Default value
# Process the food items and include them in the template
section_order = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "Customized dish", "Apetizer", "Desserts", "Soft Drinks"]
ordered_menu = {section: [] for section in section_order}
# Sorting and filtering logic (same as your existing code)
# Pass the ordered_menu and other data to the template
return render_template(
"menu.html", # Template path
ordered_menu=ordered_menu,
categories=["All", "Veg", "Non veg"],
selected_category=selected_category,
referral_code=referral_code,
reward_points=reward_points,
user_name=user_name,
first_letter=first_letter
)
except Exception as e:
# Log the error and return the error page with error details
print(f"Error fetching menu data: {str(e)}")
return render_template("error.html", error=str(e)) # Render error.html with the error message