Spaces:
Runtime error
Runtime error
from flask import Flask, render_template, request, jsonify, redirect, url_for, session | |
from flask_session import Session # Import the Session class | |
from flask.sessions import SecureCookieSessionInterface # Import the class | |
from salesforce import get_salesforce_connection | |
from datetime import timedelta | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from menu import menu_blueprint # Make sure this import is correct | |
from cart import cart_blueprint # Same for other blueprints | |
from order import order_blueprint # Same for user blueprint | |
from orderhistory import orderhistory_blueprint | |
from user_details import user_details_blueprint | |
from datetime import datetime | |
from datetime import datetime | |
import pytz # Library to handle timezone conversions | |
import os | |
import smtplib | |
import random | |
import string | |
app = Flask(__name__) | |
# Add debug logs in Salesforce connection setup | |
sf = get_salesforce_connection() | |
# Set the secret key to handle sessions securely | |
app.secret_key = os.getenv("SECRET_KEY", "xEr0cwgsiatzrzaeFewYrVA1O") # Replace with a secure key | |
app.config["SESSION_TYPE"] = "filesystem" # Storing sessions in filesystem | |
app.config["SESSION_COOKIE_SECURE"] = True # Enabling secure cookies (ensure your app is served over HTTPS) | |
app.config["SESSION_COOKIE_SAMESITE"] = "None" # Cross-site cookies allowed | |
# Initialize the session | |
Session(app) # Correctly initialize the Session object | |
app.session_interface = SecureCookieSessionInterface() | |
app.register_blueprint(cart_blueprint, url_prefix='/cart') | |
app.register_blueprint(user_details_blueprint, url_prefix='/user') | |
app.register_blueprint(menu_blueprint) | |
app.register_blueprint(order_blueprint) | |
app.register_blueprint(orderhistory_blueprint, url_prefix='/orderhistory') | |
def home(): | |
# Fetch user details from URL parameters | |
user_email = request.args.get("email") | |
user_name = request.args.get("name") | |
table_number = request.args.get("table") # Capture table number | |
if user_email and user_name: | |
session["user_email"] = user_email | |
session["user_name"] = user_name | |
session["table_number"] = table_number # Store table number in session | |
print(f"User logged in: {user_email} - {user_name} - Table: {table_number}") | |
# Ensure session is saved before redirecting | |
session.modified = True | |
return redirect(url_for("menu.menu")) # Redirect to menu directly | |
return render_template("index.html") | |
def generate_custom_dish(): | |
try: | |
data = request.form | |
dish_name = data.get("name") | |
description = data.get("description") | |
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" | |
if not dish_name or not description: | |
return jsonify({"success": False, "error": "Both fields are required"}), 400 | |
# Generate a random price for the custom dish | |
price = random.randint(10, 30) # Example logic for price setting | |
# Determine Veg/Non-Veg | |
veg_keywords = ["paneer", "vegetable", "mushroom", "cheese"] | |
non_veg_keywords = ["chicken", "mutton", "fish", "egg"] | |
category = "Veg" if any(word in description.lower() for word in veg_keywords) else \ | |
"Non veg" if any(word in description.lower() for word in non_veg_keywords) else \ | |
"both" | |
# Query to check if the dish already exists in Salesforce (Custom_Dish__c object) | |
existing_dish_query = f"SELECT Id, Name, Price__c, Image1__c, Image2__c, Description__c, Veg_NonVeg__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, use the existing details | |
existing_dish = existing_dish_result['records'][0] | |
price = existing_dish['Price__c'] | |
item_image_url = existing_dish['Image1__c'] | |
item_image_url2 = existing_dish['Image2__c'] | |
category = existing_dish['Veg_NonVeg__c'] | |
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': category, | |
'Section__c': 'Customized dish', | |
'Total_Ordered__c': 0 | |
} | |
# Insert the custom dish into Salesforce (Custom_Dish__c object) | |
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 | |
# After ensuring the dish exists, check if it's already in the Cart_Item__c | |
email = session.get('user_email') # Assuming you have the user's email in session | |
# Query to check if the custom dish already exists in the cart for the logged-in user | |
cart_item_query = f"SELECT Id, Quantity__c, Price__c, Base_Price__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}' AND Name = '{dish_name}'" | |
cart_item_result = sf.query(cart_item_query) | |
if cart_item_result['totalSize'] > 0: | |
# If the custom dish is already in the cart, update the quantity and price | |
cart_item = cart_item_result['records'][0] | |
new_quantity = cart_item['Quantity__c'] + 1 # Increase quantity by 1 | |
new_price = price * new_quantity # Update price based on new quantity | |
# Update the cart item in Salesforce | |
updated_cart_item = { | |
'Quantity__c': new_quantity, | |
'Price__c': new_price | |
} | |
cart_item_update = sf.Cart_Item__c.update(cart_item['Id'], updated_cart_item) | |
else: | |
# If the custom 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, # Default quantity is 1 | |
'Add_Ons__c': '', # Set Add_ons__c to empty | |
'Add_Ons_Price__c': 0, # Set Add_ons_Price__c to 0 | |
'Customer_Email__c': email # Associate the custom dish with the logged-in user | |
} | |
# Insert the custom dish as a Cart_Item__c record in Salesforce | |
cart_result = sf.Cart_Item__c.create(cart_item) | |
# Redirect to the cart page after successfully adding or updating the cart item | |
return redirect(url_for("cart")) | |
except Exception as e: | |
return jsonify({"success": False, "error": str(e)}), 500 | |
app.permanent_session_lifetime = timedelta(minutes=5) | |
def check_session_timeout(): | |
if "last_activity" in session: | |
last_activity_time = session["last_activity"] | |
now = datetime.now().timestamp() | |
# Check if inactivity time has exceeded 5 minutes (300 seconds) | |
if now - last_activity_time > 300: | |
session.clear() # Clear session | |
return redirect(url_for("logout")) | |
# Update last activity timestamp on every request | |
session["last_activity"] = datetime.now().timestamp() | |
def dashboard(): | |
return render_template("dashboard.html") | |
def logout(): | |
# Retrieve table number before clearing session | |
table_number = session.get('table_number', '') | |
# Clear session variables | |
session.pop('name', None) | |
session.pop('email', None) | |
session.pop('rewardPoints', None) | |
session.pop('coupon', None) | |
# Pass table number to redirect page | |
return render_template("redirect_page.html", table_number=table_number) | |
if __name__ == "__main__": | |
app.run(debug=True, host="0.0.0.0", port=7860) |