| from flask import Flask, render_template, request, jsonify, redirect, url_for, session |
| from flask_session import Session |
| from flask.sessions import SecureCookieSessionInterface |
| 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 |
| from cart import cart_blueprint |
| from order import order_blueprint |
| from user_details import user_details_blueprint |
| from datetime import datetime |
| from datetime import datetime |
| import pytz |
| import os |
| import smtplib |
| import random |
| import string |
|
|
| app = Flask(__name__) |
|
|
|
|
| |
| sf = get_salesforce_connection() |
|
|
|
|
| |
| app.secret_key = os.getenv("SECRET_KEY", "xEr0cwgsiatzrzaeFewYrVA1O") |
| app.config["SESSION_TYPE"] = "filesystem" |
| app.config["SESSION_COOKIE_SECURE"] = True |
| app.config["SESSION_COOKIE_SAMESITE"] = "None" |
|
|
| |
| Session(app) |
| 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.route("/") |
| def home(): |
| |
| user_email = request.args.get("email") |
| user_name = request.args.get("name") |
| table_number = request.args.get("table") |
|
|
| if user_email and user_name: |
| session["user_email"] = user_email |
| session["user_name"] = user_name |
| session["table_number"] = table_number |
| print(f"User logged in: {user_email} - {user_name} - Table: {table_number}") |
|
|
| |
| session.modified = True |
| return redirect(url_for("menu.menu")) |
|
|
| return render_template("index.html") |
|
|
| @app.route("/generate_custom_dish", methods=["POST"]) |
| 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 |
|
|
| |
| price = random.randint(10, 30) |
|
|
| |
| 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" |
|
|
| |
| 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: |
| |
| 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: |
| |
| 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 |
| } |
|
|
| |
| 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 |
|
|
| |
| email = session.get('user_email') |
| |
| |
| 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: |
| |
| cart_item = cart_item_result['records'][0] |
| new_quantity = cart_item['Quantity__c'] + 1 |
| new_price = price * new_quantity |
| |
| |
| 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: |
| |
| 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': email |
| } |
|
|
| |
| cart_result = sf.Cart_Item__c.create(cart_item) |
|
|
| |
| return redirect(url_for("cart")) |
|
|
| except Exception as e: |
| return jsonify({"success": False, "error": str(e)}), 500 |
|
|
| @app.route("/order-history", methods=["GET"]) |
| def order_history(): |
| email = session.get('user_email') |
| if not email: |
| return redirect(url_for("login")) |
|
|
| try: |
| |
| result = sf.query(f""" |
| SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, |
| Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c, CreatedDate |
| FROM Order__c |
| WHERE Customer_Email__c = '{email}' |
| ORDER BY CreatedDate DESC |
| """) |
|
|
| print(f"Salesforce query result: {result}") |
|
|
| orders = result.get("records", []) |
|
|
| if not orders: |
| print("No orders found for this email.") |
|
|
| |
| for order in orders: |
| order_details = order.get("Order_Details__c", "") |
| items = order_details.split("\n") |
| formatted_items = [] |
|
|
| |
| for item in items: |
| item_details = item.split(" | ") |
| if len(item_details) > 1: |
| name = item_details[0].strip() |
| quantity = item_details[1].strip() |
| formatted_items.append(f"{name} * {quantity}") |
|
|
| |
| order['formatted_items'] = ", ".join(formatted_items) |
|
|
| |
| created_date = order.get("CreatedDate", "") |
| if created_date: |
| |
| utc_datetime = datetime.strptime(created_date, '%Y-%m-%dT%H:%M:%S.000+0000') |
| utc_datetime = utc_datetime.replace(tzinfo=pytz.UTC) |
| |
| |
| local_timezone = pytz.timezone('Asia/Kolkata') |
| local_datetime = utc_datetime.astimezone(local_timezone) |
|
|
| |
| order['formatted_date'] = local_datetime.strftime('%B %d, %I:%M %p') |
|
|
| order_status = order.get("Order_Status__c", "N/A") |
| order['order_status'] = order_status |
|
|
|
|
| return render_template("order_history.html", orders=orders) |
|
|
| except Exception as e: |
| print(f"Error fetching order history: {str(e)}") |
| return render_template("order_history.html", orders=[], error=str(e)) |
|
|
|
|
| app.permanent_session_lifetime = timedelta(minutes=5) |
| @app.before_request |
| def check_session_timeout(): |
| if "last_activity" in session: |
| last_activity_time = session["last_activity"] |
| now = datetime.now().timestamp() |
| |
| |
| if now - last_activity_time > 300: |
| session.clear() |
| return redirect(url_for("logout")) |
| |
| |
| session["last_activity"] = datetime.now().timestamp() |
|
|
| @app.route("/dashboard") |
| def dashboard(): |
| return render_template("dashboard.html") |
| @app.route("/logout") |
| def logout(): |
| |
| table_number = session.get('table_number', '') |
|
|
| |
| session.pop('name', None) |
| session.pop('email', None) |
| session.pop('rewardPoints', None) |
| session.pop('coupon', None) |
|
|
| |
| return render_template("redirect_page.html", table_number=table_number) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if __name__ == "__main__": |
| app.run(debug=True, host="0.0.0.0", port=7860) |