Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,110 +1,63 @@
|
|
| 1 |
-
from flask import Flask, render_template, request,
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Initialize Flask App
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
# Salesforce Connection
|
| 8 |
-
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 9 |
-
|
| 10 |
-
# Global Cart
|
| 11 |
-
cart = []
|
| 12 |
-
|
| 13 |
-
# Home Page (Login)
|
| 14 |
@app.route("/", methods=["GET", "POST"])
|
| 15 |
def login():
|
| 16 |
if request.method == "POST":
|
| 17 |
-
email = request.form.get("email")
|
| 18 |
-
password = request.form.get("password")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
result = sf.query(query)
|
| 22 |
-
|
| 23 |
-
if len(result['records']) == 0:
|
| 24 |
-
return render_template("login.html", error="Invalid email or password.")
|
| 25 |
-
|
| 26 |
-
user = result['records'][0]
|
| 27 |
-
stored_password = user['Password__c']
|
| 28 |
-
|
| 29 |
-
if password == stored_password: # Simplified password verification for demo
|
| 30 |
return redirect(url_for("menu"))
|
| 31 |
-
|
| 32 |
-
return render_template("login.html", error="Invalid email or password.")
|
| 33 |
return render_template("login.html")
|
| 34 |
|
| 35 |
-
# Signup Page
|
| 36 |
@app.route("/signup", methods=["GET", "POST"])
|
| 37 |
def signup():
|
| 38 |
if request.method == "POST":
|
| 39 |
-
name = request.form.get("name")
|
| 40 |
-
email = request.form.get("email")
|
| 41 |
-
phone = request.form.get("phone")
|
| 42 |
-
password = request.form.get("password")
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
if len(result['records']) > 0:
|
| 48 |
-
return render_template("signup.html", error="Email already exists!")
|
| 49 |
-
|
| 50 |
-
sf.Customer_Login__c.create({
|
| 51 |
-
'Name': name,
|
| 52 |
-
'Email__c': email,
|
| 53 |
-
'Phone_Number__c': phone,
|
| 54 |
-
'Password__c': password # Store securely in production
|
| 55 |
-
})
|
| 56 |
-
return redirect(url_for("login"))
|
| 57 |
return render_template("signup.html")
|
| 58 |
|
| 59 |
-
# Menu Page
|
| 60 |
@app.route("/menu")
|
| 61 |
def menu():
|
| 62 |
-
|
| 63 |
-
result = sf.query(query)
|
| 64 |
-
menu_items = result['records']
|
| 65 |
return render_template("menu.html", menu_items=menu_items)
|
| 66 |
|
| 67 |
-
# Add to Cart
|
| 68 |
@app.route("/add_to_cart", methods=["POST"])
|
| 69 |
-
def
|
| 70 |
-
global cart
|
| 71 |
data = request.json
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# Add item to cart
|
| 76 |
-
cart.append({"Name": item_name, "Price": item_price, "Quantity": 1})
|
| 77 |
-
return jsonify({"message": f"{item_name} added to cart!"})
|
| 78 |
|
| 79 |
-
# View Cart
|
| 80 |
@app.route("/cart")
|
| 81 |
-
def
|
| 82 |
-
|
| 83 |
-
total = sum(float(item["Price"]) * item["Quantity"] for item in cart)
|
| 84 |
return render_template("cart.html", cart=cart, total=total)
|
| 85 |
|
| 86 |
-
# Place Order
|
| 87 |
@app.route("/place_order", methods=["POST"])
|
| 88 |
def place_order():
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
if not cart:
|
| 93 |
return render_template("cart.html", error="Cart is empty!", cart=cart, total=0)
|
| 94 |
-
|
| 95 |
order_details = "\n".join([f"{item['Name']} - ${item['Price']} x {item['Quantity']}" for item in cart])
|
| 96 |
-
total = sum(float(item["Price"]) * item["Quantity"] for item in cart)
|
| 97 |
-
|
| 98 |
try:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
'Total_Amount__c': total
|
| 103 |
-
})
|
| 104 |
-
cart.clear()
|
| 105 |
-
return render_template("cart.html", success=f"Order placed successfully! Total: ${total}", cart=cart, total=0)
|
| 106 |
except Exception as e:
|
| 107 |
-
return render_template("cart.html", error=f"Error
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, jsonify
|
| 2 |
+
from models.salesforce import fetch_menu_items, place_order_in_salesforce
|
| 3 |
+
from models.cart import add_to_cart, view_cart, clear_cart
|
| 4 |
+
from models.user import login_user, signup_user
|
| 5 |
|
|
|
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@app.route("/", methods=["GET", "POST"])
|
| 9 |
def login():
|
| 10 |
if request.method == "POST":
|
| 11 |
+
email = request.form.get("email")
|
| 12 |
+
password = request.form.get("password")
|
| 13 |
+
success, message = login_user(email, password)
|
| 14 |
+
if success:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
return redirect(url_for("menu"))
|
| 16 |
+
return render_template("login.html", error=message)
|
|
|
|
| 17 |
return render_template("login.html")
|
| 18 |
|
|
|
|
| 19 |
@app.route("/signup", methods=["GET", "POST"])
|
| 20 |
def signup():
|
| 21 |
if request.method == "POST":
|
| 22 |
+
name = request.form.get("name")
|
| 23 |
+
email = request.form.get("email")
|
| 24 |
+
phone = request.form.get("phone")
|
| 25 |
+
password = request.form.get("password")
|
| 26 |
+
success, message = signup_user(name, email, phone, password)
|
| 27 |
+
if success:
|
| 28 |
+
return redirect(url_for("login"))
|
| 29 |
+
return render_template("signup.html", error=message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return render_template("signup.html")
|
| 31 |
|
|
|
|
| 32 |
@app.route("/menu")
|
| 33 |
def menu():
|
| 34 |
+
menu_items = fetch_menu_items()
|
|
|
|
|
|
|
| 35 |
return render_template("menu.html", menu_items=menu_items)
|
| 36 |
|
|
|
|
| 37 |
@app.route("/add_to_cart", methods=["POST"])
|
| 38 |
+
def add_to_cart_route():
|
|
|
|
| 39 |
data = request.json
|
| 40 |
+
cart = add_to_cart(data["name"], data["price"])
|
| 41 |
+
return jsonify({"message": "Item added to cart!", "cart": cart})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
| 43 |
@app.route("/cart")
|
| 44 |
+
def cart_page():
|
| 45 |
+
cart, total = view_cart()
|
|
|
|
| 46 |
return render_template("cart.html", cart=cart, total=total)
|
| 47 |
|
|
|
|
| 48 |
@app.route("/place_order", methods=["POST"])
|
| 49 |
def place_order():
|
| 50 |
+
email = request.form.get("email")
|
| 51 |
+
cart, total = view_cart()
|
|
|
|
| 52 |
if not cart:
|
| 53 |
return render_template("cart.html", error="Cart is empty!", cart=cart, total=0)
|
|
|
|
| 54 |
order_details = "\n".join([f"{item['Name']} - ${item['Price']} x {item['Quantity']}" for item in cart])
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
+
place_order_in_salesforce(email, order_details, total)
|
| 57 |
+
clear_cart()
|
| 58 |
+
return render_template("cart.html", success="Order placed successfully!", cart=[], total=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
+
return render_template("cart.html", error=f"Error: {str(e)}", cart=cart, total=total)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
app.run(debug=True)
|