Spaces:
Runtime error
Runtime error
| from flask import Flask, render_template, request, jsonify, redirect, url_for | |
| from simple_salesforce import Salesforce | |
| # Initialize Flask app and Salesforce connection | |
| app = Flask(__name__) | |
| # Function to connect to Salesforce | |
| def get_salesforce_connection(): | |
| sf = Salesforce( | |
| username="diggavalli98@gmail.com", | |
| password="Sati@1020", | |
| security_token="sSSjyhInIsUohKpG8sHzty2q" | |
| ) | |
| return sf | |
| sf = get_salesforce_connection() | |
| # Home route | |
| def home(): | |
| return render_template("index.html") | |
| # Signup route | |
| def signup(): | |
| if request.method == "POST": | |
| name = request.form.get("name") | |
| phone = request.form.get("phone") | |
| email = request.form.get("email") | |
| password = request.form.get("password") | |
| try: | |
| sf.Customer_Login__c.create({ | |
| "Name": name, | |
| "Phone__c": phone, | |
| "Email__c": email, | |
| "Password__c": password | |
| }) | |
| return redirect(url_for("login")) | |
| except Exception as e: | |
| return render_template("signup.html", error=f"Error: {str(e)}") | |
| return render_template("signup.html") | |
| # Login route | |
| def login(): | |
| if request.method == "POST": | |
| email = request.form.get("email") | |
| password = request.form.get("password") | |
| try: | |
| query = f"SELECT Name FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'" | |
| result = sf.query(query) | |
| if result["records"]: | |
| return redirect(url_for("menu")) | |
| return render_template("login.html", error="Invalid credentials!") | |
| except Exception as e: | |
| return render_template("login.html", error=f"Error: {str(e)}") | |
| return render_template("login.html") | |
| # Menu route | |
| def menu(): | |
| selected_category = request.args.get("category", "All") | |
| try: | |
| query = "SELECT Name, Price__c, Image1__c, Cateogry__c, Description__c FROM Menu_Item__c" | |
| result = sf.query(query) | |
| food_items = result['records'] | |
| categories = {item['Cateogry__c'] for item in food_items if 'Cateogry__c' in item} | |
| if selected_category != "All": | |
| food_items = [item for item in food_items if item.get("Cateogry__c") == selected_category] | |
| except Exception as e: | |
| food_items = [] | |
| categories = [] | |
| print(f"Error fetching data: {e}") | |
| return render_template("menu.html", food_items=food_items, categories=categories, selected_category=selected_category) | |
| # Cart route | |
| def cart(): | |
| try: | |
| result = sf.query( | |
| """ | |
| SELECT Name, Price__c, Quantity__c, Image1__c | |
| FROM Cart_Item__c | |
| WHERE User__c = 'Current_User_Id' -- Replace this with logic to fetch current user ID | |
| """ | |
| ) | |
| cart_items = result["records"] | |
| subtotal = sum(item['Quantity__c'] * item['Price__c'] for item in cart_items) | |
| except Exception as e: | |
| print(f"Error fetching cart items: {e}") | |
| cart_items = [] | |
| subtotal = 0 | |
| return render_template("cart.html", cart_items=cart_items, subtotal=subtotal) | |
| # Add to cart API route | |
| def add_to_cart(): | |
| data = request.json | |
| try: | |
| sf.Cart_Item__c.create({ | |
| "Menu_Item__c": data["menu_item_id"], | |
| "Quantity__c": 1, | |
| "In_Cart__c": True | |
| }) | |
| return jsonify({"success": True}) | |
| except Exception as e: | |
| print(f"Error adding to cart: {e}") | |
| return jsonify({"success": False, "error": str(e)}) | |
| # Addons API route | |
| def get_addons(): | |
| menu_item_id = request.args.get("menu_item_id") | |
| try: | |
| query = f""" | |
| SELECT Name, Price__c | |
| FROM Add_On__c | |
| WHERE Menu_Item__c = '{menu_item_id}' | |
| AND Available_In_Menu__c = true | |
| """ | |
| result = sf.query(query) | |
| addons = [{"id": addon["Id"], "name": addon["Name"], "price": addon["Price__c"]} for addon in result["records"]] | |
| return jsonify(addons) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}) | |
| # HTML files and JavaScript included in the templates | |
| def signup_html(): | |
| return ''' | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Signup</title> | |
| <style> | |
| body { | |
| font-family: Arial, sans-serif; | |
| background: linear-gradient(to right, #6a11cb, #2575fc); | |
| margin: 0; | |
| display: flex; | |
| justify-content: center; | |
| align-items: center; | |
| height: 100vh; | |
| color: #fff; | |
| } | |
| .form-container { | |
| background: #ffffff; | |
| padding: 20px 30px; | |
| border-radius: 10px; | |
| width: 100%; | |
| max-width: 400px; | |
| text-align: center; | |
| box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); | |
| color: #333; | |
| } | |
| .form-container h2 { | |
| margin-bottom: 20px; | |
| color: #333; | |
| } | |
| .form-container label { | |
| display: block; | |
| text-align: left; | |
| margin: 10px 0 5px; | |
| } | |
| .form-container input { | |
| width: 100%; | |
| padding: 10px; | |
| margin-bottom: 15px; | |
| border: 1px solid #ccc; | |
| border-radius: 5px; | |
| } | |
| .form-container button { | |
| width: 100%; | |
| padding: 10px; | |
| background-color: #6a11cb; | |
| color: #fff; | |
| border: none; | |
| border-radius: 5px; | |
| font-size: 16px; | |
| cursor: pointer; | |
| } | |
| .form-container button:hover { | |
| background-color: #2575fc; | |
| } | |
| .form-container p { | |
| margin-top: 10px; | |
| } | |
| .form-container a { | |
| color: #6a11cb; | |
| text-decoration: none; | |
| } | |
| .form-container a:hover { | |
| text-decoration: underline; | |
| } | |
| .error-message { | |
| color: red; | |
| margin-top: 10px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="form-container"> | |
| <h2>Signup</h2> | |
| <form action="/signup" method="POST"> | |
| <label for="name">Name:</label> | |
| <input type="text" id="name" name="name" placeholder="Enter your name" required> | |
| <label for="phone">Phone:</label> | |
| <input type="text" id="phone" name="phone" placeholder="Enter your phone number" required> | |
| <label for="email">Email:</label> | |
| <input type="email" id="email" name="email" placeholder="Enter your email" required> | |
| <label for="password">Password:</label> | |
| <input type="password" id="password" name="password" placeholder="Enter your password" required> | |
| <button type="submit">Sign Up</button> | |
| </form> | |
| <p>Already have an account? <a href="/login">Login</a></p> | |
| </div> | |
| </body> | |
| </html> | |
| ''' | |
| # Add more HTML for other pages similarly | |
| if __name__ == "__main__": | |
| app.run(debug=True, port=8080) | |