Spaces:
Runtime error
Runtime error
from flask import Blueprint, request, jsonify, redirect, url_for, session, render_template | |
import random | |
from salesforce import get_salesforce_connection | |
sf = get_salesforce_connection() | |
customdish_blueprint = Blueprint('customdish', __name__) | |
def custom_dish(): | |
user_email = session.get('user_email') | |
if not user_email: | |
return redirect(url_for("login")) | |
if request.method == "POST": | |
# Handle the form submission for veg/non-veg selection | |
veg_nonveg = request.form.get("veg_nonveg") | |
if not veg_nonveg: | |
return jsonify({"success": False, "error": "Please select Vegetarian or Non-Vegetarian"}), 400 | |
# Store the preference in session and redirect to ingredient selection | |
session['veg_nonveg'] = veg_nonveg | |
return redirect(url_for('customdish.select_ingredients')) | |
# Render the veg/non-veg selection page | |
return render_template("custom_dish.html") | |
def select_ingredients(): | |
user_email = session.get('user_email') | |
if not user_email: | |
return redirect(url_for("login")) | |
veg_nonveg = session.get('veg_nonveg') | |
if not veg_nonveg: | |
return redirect(url_for('customdish.custom_dish')) | |
if request.method == "POST": | |
# Handle the ingredient selection and dish creation | |
dish_name = request.form.get("name") | |
selected_ingredients = request.form.getlist("ingredients") # Multiple ingredients can be selected | |
description = f"Custom dish with {', '.join(selected_ingredients)}" | |
if not dish_name or not selected_ingredients: | |
return jsonify({"success": False, "error": "Dish name and ingredients are required"}), 400 | |
dish_name = dish_name.replace("'", "+") | |
price = random.randint(10, 30) # Example logic for price setting | |
item_image_url = "https://huggingface.co/spaces/nagasurendra/BiryaniHubflask30/resolve/main/static/customized.jpg" | |
item_image_url2 = "https://huggingface.co/spaces/nagasurendra/BiryaniHubflask30/resolve/main/static/customized1.jpg" | |
# Check if the dish already exists in Custom_Dish__c | |
existing_dish_query = f"SELECT Id, Total_Ordered__c FROM Custom_Dish__c WHERE Name = '{dish_name}'" | |
existing_dish_result = sf.query(existing_dish_query) | |
if existing_dish_result['totalSize'] > 0: | |
# If the dish exists, increment Total_Ordered__c | |
existing_dish = existing_dish_result['records'][0] | |
dish_id = existing_dish['Id'] | |
total_ordered = existing_dish.get('Total_Ordered__c', 0) or 0 | |
sf.Custom_Dish__c.update(dish_id, { | |
'Total_Ordered__c': total_ordered + 1 | |
}) | |
else: | |
# If the dish does not exist, create a new custom dish | |
custom_dish = { | |
'Name': dish_name, | |
'Price__c': price, | |
'Image1__c': item_image_url, | |
'Image2__c': item_image_url2, | |
'Description__c': description, | |
'Veg_NonVeg__c': veg_nonveg, | |
'Section__c': 'Customized dish', | |
'Total_Ordered__c': 1 # Initial order count | |
} | |
result = sf.Custom_Dish__c.create(custom_dish) | |
if not result.get('success'): | |
return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500 | |
# Add the custom dish to the cart | |
cart_item_query = f"SELECT Id, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}' AND Name = '{dish_name}'" | |
cart_item_result = sf.query(cart_item_query) | |
if cart_item_result['totalSize'] > 0: | |
# If the dish is already in the cart, increment the quantity | |
cart_item = cart_item_result['records'][0] | |
cart_item_id = cart_item['Id'] | |
existing_quantity = int(cart_item.get('Quantity__c', 0)) | |
total_price = (existing_quantity + 1) * price | |
sf.Cart_Item__c.update(cart_item_id, { | |
"Quantity__c": existing_quantity + 1, | |
"Price__c": total_price | |
}) | |
else: | |
# If the dish is not in the cart, create a new cart item | |
cart_item = { | |
'Name': dish_name, | |
'Price__c': price, | |
'Base_Price__c': price, | |
'Image1__c': item_image_url, | |
'Quantity__c': 1, | |
'Add_Ons__c': '', | |
'Add_Ons_Price__c': 0, | |
'Customer_Email__c': user_email, | |
'Category__c': veg_nonveg, | |
'Section__c': 'Customized dish' | |
} | |
cart_result = sf.Cart_Item__c.create(cart_item) | |
if not cart_result.get('success'): | |
return jsonify({"success": False, "error": "Failed to add custom dish to cart in Salesforce"}), 500 | |
return redirect(url_for('cart.cart')) | |
# Fetch ingredients based on veg/non-veg preference | |
try: | |
query = f""" | |
SELECT Name, Veg_NonVeg__c | |
FROM Ingredients__c | |
WHERE Veg_NonVeg__c = '{veg_nonveg}' OR Veg_NonVeg__c = 'both' | |
""" | |
result = sf.query_all(query) | |
ingredients = result.get('records', []) | |
except Exception as e: | |
print(f"Error fetching ingredients: {str(e)}") | |
ingredients = [] | |
return render_template("select_ingredients.html", ingredients=ingredients, veg_nonveg=veg_nonveg) |