|
from flask import Flask, render_template, request, jsonify, redirect, url_for |
|
from simple_salesforce import Salesforce |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') |
|
|
|
|
|
cart = [] |
|
|
|
|
|
@app.route("/", methods=["GET", "POST"]) |
|
def login(): |
|
if request.method == "POST": |
|
email = request.form.get("email").strip() |
|
password = request.form.get("password").strip() |
|
|
|
query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'" |
|
result = sf.query(query) |
|
|
|
if len(result['records']) == 0: |
|
return render_template("login.html", error="Invalid email or password.") |
|
|
|
user = result['records'][0] |
|
stored_password = user['Password__c'] |
|
|
|
if password == stored_password: |
|
return redirect(url_for("menu")) |
|
else: |
|
return render_template("login.html", error="Invalid email or password.") |
|
return render_template("login.html") |
|
|
|
|
|
@app.route("/signup", methods=["GET", "POST"]) |
|
def signup(): |
|
if request.method == "POST": |
|
name = request.form.get("name").strip() |
|
email = request.form.get("email").strip() |
|
phone = request.form.get("phone").strip() |
|
password = request.form.get("password").strip() |
|
|
|
query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'" |
|
result = sf.query(query) |
|
|
|
if len(result['records']) > 0: |
|
return render_template("signup.html", error="Email already exists!") |
|
|
|
sf.Customer_Login__c.create({ |
|
'Name': name, |
|
'Email__c': email, |
|
'Phone_Number__c': phone, |
|
'Password__c': password |
|
}) |
|
return redirect(url_for("login")) |
|
return render_template("signup.html") |
|
|
|
|
|
@app.route("/menu") |
|
def menu(): |
|
query = "SELECT Name, Price__c, Description__c, Image1__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c" |
|
result = sf.query(query) |
|
menu_items = result['records'] |
|
return render_template("menu.html", menu_items=menu_items) |
|
|
|
|
|
@app.route("/add_to_cart", methods=["POST"]) |
|
def add_to_cart(): |
|
global cart |
|
data = request.json |
|
item_name = data.get("name") |
|
item_price = data.get("price") |
|
|
|
|
|
cart.append({"Name": item_name, "Price": item_price, "Quantity": 1}) |
|
return jsonify({"message": f"{item_name} added to cart!"}) |
|
|
|
|
|
@app.route("/cart") |
|
def view_cart(): |
|
global cart |
|
total = sum(float(item["Price"]) * item["Quantity"] for item in cart) |
|
return render_template("cart.html", cart=cart, total=total) |
|
|
|
|
|
@app.route("/place_order", methods=["POST"]) |
|
def place_order(): |
|
global cart |
|
email = request.form.get("email").strip() |
|
|
|
if not cart: |
|
return render_template("cart.html", error="Cart is empty!", cart=cart, total=0) |
|
|
|
order_details = "\n".join([f"{item['Name']} - ${item['Price']} x {item['Quantity']}" for item in cart]) |
|
total = sum(float(item["Price"]) * item["Quantity"] for item in cart) |
|
|
|
try: |
|
sf.Order__c.create({ |
|
'Customer_Email__c': email, |
|
'Order_Items__c': order_details, |
|
'Total_Amount__c': total |
|
}) |
|
cart.clear() |
|
return render_template("cart.html", success=f"Order placed successfully! Total: ${total}", cart=cart, total=0) |
|
except Exception as e: |
|
return render_template("cart.html", error=f"Error placing order: {str(e)}", cart=cart, total=total) |
|
|
|
if __name__ == "__main__": |
|
app.run(debug=True) |
|
|