Spaces:
Sleeping
Sleeping
from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for | |
from salesforce import get_salesforce_connection | |
import os | |
menu_blueprint = Blueprint('menu', __name__) | |
# Initialize Salesforce connection | |
sf = get_salesforce_connection() | |
# Constants for video handling | |
STATIC_DIR = os.path.join(os.path.dirname(__file__), 'static') | |
PLACEHOLDER_VIDEO = 'placeholder.mp4' | |
PLACEHOLDER_PATH = os.path.join(STATIC_DIR, PLACEHOLDER_VIDEO) | |
SECTION_ORDER = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "Customized dish", "Apetizer", "Desserts", "Soft Drinks"] | |
# Create placeholder video at startup if it doesn't exist | |
if not os.path.exists(PLACEHOLDER_PATH): | |
open(PLACEHOLDER_PATH, 'wb').close() | |
print(f"Created placeholder video at {PLACEHOLDER_PATH}") | |
def get_valid_video_path(item_name, video_url=None): | |
""" | |
Get valid video path for item with placeholder fallback | |
Priority: 1. Video1__c from Salesforce 2. placeholder.mp4 | |
""" | |
# First try: Video1__c from Salesforce if provided | |
if video_url: | |
# If it's a complete URL (http/https) | |
if video_url.startswith(('http://', 'https://')): | |
return video_url | |
# If it's a relative path (/videos/xxx.mp4) | |
elif video_url.startswith('/'): | |
return video_url | |
# If it's a Salesforce File ID (starts with '069') | |
elif video_url.startswith('069'): | |
return f"https://yourdomain.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}" | |
# Final fallback: placeholder.mp4 | |
if not os.path.exists(PLACEHOLDER_PATH): | |
open(PLACEHOLDER_PATH, 'wb').close() | |
print(f"Created missing placeholder video at {PLACEHOLDER_PATH}") | |
return f"/static/{PLACEHOLDER_VIDEO}" | |
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 | |
else: | |
return redirect(url_for("login")) | |
else: | |
user_name = session.get('user_name') | |
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) | |
# Get cart item count | |
cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'" | |
cart_count_result = sf.query(cart_query) | |
cart_item_count = cart_count_result['totalSize'] | |
# Query to fetch Menu_Item__c records including Video1__c | |
menu_query = """ | |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c, | |
Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c | |
FROM Menu_Item__c | |
""" | |
result = sf.query(menu_query) | |
food_items = result['records'] if 'records' in result else [] | |
# Process items and add video paths | |
for item in food_items: | |
if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None: | |
item['Total_Ordered__c'] = 0 | |
item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c')) | |
# Query to fetch Custom_Dish__c records | |
custom_dish_query = """ | |
SELECT Name, Price__c, Description__c, Image1__c, Image2__c, | |
Veg_NonVeg__c, Section__c, Total_Ordered__c | |
FROM Custom_Dish__c | |
WHERE CreatedDate >= LAST_N_DAYS:7 | |
""" | |
custom_dish_result = sf.query(custom_dish_query) | |
custom_dishes = custom_dish_result.get('records', []) | |
# Process custom dishes and add video paths | |
for item in custom_dishes: | |
if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None: | |
item['Total_Ordered__c'] = 0 | |
item['Video1__c'] = get_valid_video_path(item['Name']) | |
# Merge both Menu_Item__c and Custom_Dish__c records | |
all_items = food_items + custom_dishes | |
ordered_menu = {section: [] for section in SECTION_ORDER} | |
# Process best sellers | |
best_sellers = sorted(all_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True) | |
if selected_category == "Veg": | |
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]] | |
elif selected_category == "Non veg": | |
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]] | |
best_sellers = best_sellers[:4] | |
if best_sellers: | |
ordered_menu["Best Sellers"] = best_sellers | |
# Organize other sections | |
added_item_names = set() | |
for item in all_items: | |
section = item.get("Section__c", "Others") | |
if section not in ordered_menu: | |
ordered_menu[section] = [] | |
if item['Name'] in added_item_names: | |
continue | |
if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]: | |
continue | |
if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]: | |
continue | |
ordered_menu[section].append(item) | |
added_item_names.add(item['Name']) | |
ordered_menu = {section: items for section, items in ordered_menu.items() if items} | |
categories = ["All", "Veg", "Non veg"] | |
except Exception as e: | |
print(f"Error fetching menu data: {str(e)}") | |
# Fallback data with video support | |
ordered_menu = {section: [] for section in SECTION_ORDER} | |
best_sellers = ["Chicken Biryani", "Paneer Butter Masala", "Veg Manchurian", "Prawn Fry"] | |
ordered_menu["Best Sellers"] = [{ | |
"Name": name, | |
"Price__c": "12.99", | |
"Description__c": f"Popular {name}", | |
"Image1__c": "/static/placeholder.jpg", | |
"Video1__c": get_valid_video_path(name), | |
"Total_Ordered__c": 100, | |
"Veg_NonVeg__c": "Veg" if "Paneer" in name or "Veg" in name else "Non veg" | |
} for name in best_sellers] | |
categories = ["All", "Veg", "Non veg"] | |
referral_code = 'N/A' | |
reward_points = 0 | |
cart_item_count = 0 | |
return render_template( | |
"menu.html", | |
ordered_menu=ordered_menu, | |
categories=categories, | |
selected_category=selected_category, | |
referral_code=referral_code, | |
reward_points=reward_points, | |
user_name=user_name, | |
first_letter=first_letter, | |
cart_item_count=cart_item_count | |
) | |
# [Rest of your existing routes...] |