DSatishchandra commited on
Commit
444fe60
·
verified ·
1 Parent(s): 958d191

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify, redirect, url_for
2
+ from simple_salesforce import Salesforce
3
+
4
+ # Initialize Flask App
5
+ app = Flask(__name__)
6
+
7
+ # Salesforce Connection
8
+ sf = Salesforce(username='diggavalli98@gmail.com', 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").strip()
18
+ password = request.form.get("password").strip()
19
+
20
+ query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
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
+ else:
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").strip()
40
+ email = request.form.get("email").strip()
41
+ phone = request.form.get("phone").strip()
42
+ password = request.form.get("password").strip()
43
+
44
+ query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'"
45
+ result = sf.query(query)
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
+ query = "SELECT Name, Price__c, Description__c, Image1__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
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 add_to_cart():
70
+ global cart
71
+ data = request.json
72
+ item_name = data.get("name")
73
+ item_price = data.get("price")
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 view_cart():
82
+ global cart
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
+ global cart
90
+ email = request.form.get("email").strip()
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
+ sf.Order__c.create({
100
+ 'Customer_Email__c': email,
101
+ 'Order_Items__c': order_details,
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 placing order: {str(e)}", cart=cart, total=total)
108
+
109
+ if __name__ == "__main__":
110
+ app.run(debug=True)