| 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 orderhistory import orderhistory_blueprint |
| from user_details import user_details_blueprint |
| from customdish import customdish_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", "sSSjyhInIsUohKpG8sHzty2q") |
| 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.register_blueprint(orderhistory_blueprint, url_prefix='/orderhistory') |
| app.register_blueprint(customdish_blueprint, url_prefix='/customdish') |
|
|
|
|
|
|
| @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("/login", methods=["GET", "POST"]) |
| def login(): |
| if request.method == "POST": |
| email = request.form.get("email") |
| password = request.form.get("password") |
| print(f"Login attempt with email: {email}") |
|
|
| try: |
| |
| query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'" |
| result = sf.query(query) |
|
|
| if result["records"]: |
| user = result["records"][0] |
| session['user_id'] = user['Id'] |
|
|
| |
| if 'user_email' not in session or session['user_email'] != email: |
| session['user_email'] = email |
| session['user_name'] = user.get("Name", "") |
| print(f"✅ Session email updated: {session['user_email']}") |
|
|
| reward_points = user.get("Reward_Points__c") or 0 |
|
|
| |
| if reward_points >= 500: |
| new_coupon_code = generate_coupon_code() |
| coupon_query = sf.query(f"SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'") |
|
|
| if coupon_query["records"]: |
| coupon_record = coupon_query["records"][0] |
| referral_coupon_id = coupon_record["Id"] |
| existing_coupons = coupon_record.get("Coupon_Code__c", "") |
|
|
| updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip() |
| sf.Referral_Coupon__c.update(referral_coupon_id, {"Coupon_Code__c": updated_coupons}) |
| else: |
| sf.Referral_Coupon__c.create({ |
| "Referral_Email__c": email, |
| "Name": user.get("Name", ""), |
| "Coupon_Code__c": new_coupon_code |
| }) |
|
|
| new_reward_points = reward_points - 500 |
| sf.Customer_Login__c.update(user['Id'], {"Reward_Points__c": new_reward_points}) |
|
|
| return redirect(url_for("menu.menu")) |
|
|
| else: |
| print("Invalid credentials!") |
| return render_template("login.html", error="Invalid credentials!") |
|
|
| except Exception as e: |
| print(f"Error during login: {str(e)}") |
| return render_template("login.html", error=f"Error: {str(e)}") |
|
|
| return render_template("login.html") |
|
|
| |
|
|
| @app.route('/order_summary') |
| def order_summary(): |
| email = session.get('user_email') |
|
|
| if not email: |
| print("User not logged in. Redirecting to login.") |
| return "User not logged in", 400 |
|
|
| try: |
| print(f"Fetching order details for email: {email}") |
|
|
| query = f""" |
| SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c |
| FROM Order__c |
| WHERE Customer_Email__c = '{email}' |
| ORDER BY CreatedDate DESC |
| LIMIT 1 |
| """ |
| result = sf.query(query) |
|
|
| if not result.get("records"): |
| print("No order found for this user.") |
| return "No order found for this user", 400 |
|
|
| order = result["records"][0] |
| order_details = order.get("Order_Details__c", "") |
|
|
| order_items = [] |
|
|
| if order_details: |
| print("Processing order details...") |
| for line in order_details.split('\n'): |
| item_parts = line.split('|') |
| if len(item_parts) >= 5: |
| item_name = item_parts[0].strip() |
| item_name_cleaned = ' '.join(item_name.split(' ')[:-1]).strip() |
|
|
| print(f"Processing Item: {item_name_cleaned}") |
|
|
| menu_query = f""" |
| SELECT Name, Price__c, Image1__c, Ingredient_1__r.Ingredient_Name__c, |
| Ingredient_1__r.Ingredient_Image__c, Ingredient_1__r.Health_Benefits__c, |
| Ingredient_1__r.Fun_Facts__c, Ingredient_2__r.Ingredient_Name__c, |
| Ingredient_2__r.Ingredient_Image__c, Ingredient_2__r.Health_Benefits__c, |
| Ingredient_2__r.Fun_Facts__c |
| FROM Menu_Item__c |
| WHERE Name = '{item_name_cleaned}' |
| """ |
| print(f"Executing Query for {item_name_cleaned}: {menu_query}") |
|
|
| menu_result = sf.query(menu_query) |
| print(f"Menu Result for {item_name_cleaned}: {menu_result}") |
|
|
| if menu_result.get("records"): |
| menu_item = menu_result["records"][0] |
| ingredients = [] |
| |
| |
| if 'Ingredient_1__r' in menu_item: |
| print(f"Ingredient 1 Found for {item_name_cleaned}: {menu_item['Ingredient_1__r']}") |
| ingredients.append({ |
| "name": menu_item['Ingredient_1__r']['Ingredient_Name__c'], |
| "image": menu_item['Ingredient_1__r']['Ingredient_Image__c'], |
| "health_benefits": menu_item['Ingredient_1__r']['Health_Benefits__c'] or "No health benefits available", |
| "fun_facts": menu_item['Ingredient_1__r']['Fun_Facts__c'] or "No fun facts available" |
| }) |
| else: |
| print(f"Ingredient 1 missing for {item_name_cleaned}") |
| |
| |
| if 'Ingredient_2__r' in menu_item: |
| print(f"Ingredient 2 Found for {item_name_cleaned}: {menu_item['Ingredient_2__r']}") |
| ingredients.append({ |
| "name": menu_item['Ingredient_2__r']['Ingredient_Name__c'], |
| "image": menu_item['Ingredient_2__r']['Ingredient_Image__c'], |
| "health_benefits": menu_item['Ingredient_2__r']['Health_Benefits__c'] or "No health benefits available", |
| "fun_facts": menu_item['Ingredient_2__r']['Fun_Facts__c'] or "No fun facts available" |
| }) |
| else: |
| print(f"Ingredient 2 missing for {item_name_cleaned}") |
| |
| |
| if not ingredients: |
| print(f"No ingredients found for {item_name_cleaned}") |
|
|
| order_items.append({ |
| "name": item_name_cleaned, |
| "price": menu_item.get("Price__c"), |
| "image_url": menu_item.get("Image1__c"), |
| "ingredients": ingredients |
| }) |
|
|
| else: |
| print(f"Item not found in menu: {item_name_cleaned}") |
|
|
| if not order_items: |
| print("No items found in order details.") |
| else: |
| print(f"Total items extracted: {len(order_items)}") |
|
|
| |
| return render_template( |
| 'reward_status.html', |
| order_items=order_items |
| ) |
|
|
| except Exception as e: |
| print(f"Error querying Salesforce: {str(e)}") |
| return f"Error querying Salesforce: {str(e)}", 500 |
|
|
|
|
|
|
| @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) |