DSatishchandra commited on
Commit
c053032
·
verified ·
1 Parent(s): 8f55c48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -51
app.py CHANGED
@@ -1,70 +1,101 @@
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
- import os
6
- from flask import Flask
7
 
8
- app = Flask(__name__)
 
9
 
10
- @app.route("/")
11
- def home():
12
- return "Hello, Hugging Face Spaces with Flask!"
 
 
 
 
 
13
 
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
- success, message = login_user(email, password)
20
- if success:
21
- return redirect(url_for("menu"))
22
- return render_template("login.html", error=message)
23
- return render_template("login.html")
24
 
25
- @app.route("/signup", methods=["GET", "POST"])
26
- def signup():
27
- if request.method == "POST":
28
- name = request.form.get("name")
29
- email = request.form.get("email")
30
- phone = request.form.get("phone")
31
- password = request.form.get("password")
32
- success, message = signup_user(name, email, phone, password)
33
- if success:
34
- return redirect(url_for("login"))
35
- return render_template("signup.html", error=message)
36
- return render_template("signup.html")
37
 
38
- @app.route("/menu")
39
- def menu():
40
- menu_items = fetch_menu_items()
41
- return render_template("menu.html", menu_items=menu_items)
42
 
43
- @app.route("/add_to_cart", methods=["POST"])
44
- def add_to_cart_route():
45
- data = request.json
46
- cart = add_to_cart(data["name"], data["price"])
47
- return jsonify({"message": "Item added to cart!", "cart": cart})
48
-
49
- @app.route("/cart")
50
- def cart_page():
51
  cart, total = view_cart()
52
- return render_template("cart.html", cart=cart, total=total)
53
 
54
- @app.route("/place_order", methods=["POST"])
55
- def place_order():
56
- email = request.form.get("email")
 
 
57
  cart, total = view_cart()
58
  if not cart:
59
- return render_template("cart.html", error="Cart is empty!", cart=cart, total=0)
60
  order_details = "\n".join([f"{item['Name']} - ${item['Price']} x {item['Quantity']}" for item in cart])
61
  try:
62
  place_order_in_salesforce(email, order_details, total)
63
  clear_cart()
64
- return render_template("cart.html", success="Order placed successfully!", cart=[], total=0)
65
  except Exception as e:
66
- return render_template("cart.html", error=f"Error: {str(e)}", cart=cart, total=total)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- if __name__ == "__main__":
69
- port = int(os.environ.get("PORT", 5000)) # Get PORT from environment or default to 5000
70
- app.run(host="0.0.0.0", port=port)
 
1
+ import gradio as gr
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
+ # Global user state
7
+ current_user = {"email": None, "name": None}
8
 
9
+ # Login Interface
10
+ def login(email, password):
11
+ success, message = login_user(email, password)
12
+ if success:
13
+ current_user["email"] = email
14
+ current_user["name"] = message
15
+ return "Login successful! Redirecting to menu..."
16
+ return f"Login failed: {message}"
17
 
18
+ # Signup Interface
19
+ def signup(name, email, phone, password):
20
+ success, message = signup_user(name, email, phone, password)
21
+ if success:
22
+ return "Signup successful! Please login now."
23
+ return f"Signup failed: {message}"
 
 
 
 
24
 
25
+ # Load Menu Items
26
+ def load_menu(preference):
27
+ items = fetch_menu_items()
28
+ filtered_items = [
29
+ item
30
+ for item in items
31
+ if preference == "All"
32
+ or (preference == "Veg" and item["Veg_NonVeg__c"] == "Veg")
33
+ or (preference == "Non-Veg" and item["Veg_NonVeg__c"] == "Non-Veg")
34
+ ]
35
+ return filtered_items
 
36
 
37
+ # Add to Cart
38
+ def add_to_cart_interface(item_name, price):
39
+ add_to_cart(item_name, price)
40
+ return f"Added {item_name} to cart!"
41
 
42
+ # View Cart
43
+ def view_cart_interface():
 
 
 
 
 
 
44
  cart, total = view_cart()
45
+ return cart, total
46
 
47
+ # Place Order
48
+ def place_order_interface():
49
+ email = current_user["email"]
50
+ if not email:
51
+ return "Error: Please login to place an order."
52
  cart, total = view_cart()
53
  if not cart:
54
+ return "Error: Cart is empty!"
55
  order_details = "\n".join([f"{item['Name']} - ${item['Price']} x {item['Quantity']}" for item in cart])
56
  try:
57
  place_order_in_salesforce(email, order_details, total)
58
  clear_cart()
59
+ return f"Order placed successfully! Total: ${total}"
60
  except Exception as e:
61
+ return f"Error placing order: {str(e)}"
62
+
63
+ # Gradio Interfaces
64
+ with gr.Blocks() as app:
65
+ with gr.Tab("Login"):
66
+ email = gr.Textbox(label="Email", placeholder="Enter your email")
67
+ password = gr.Textbox(label="Password", type="password", placeholder="Enter your password")
68
+ login_button = gr.Button("Login")
69
+ login_output = gr.Textbox(label="Login Status")
70
+ login_button.click(login, inputs=[email, password], outputs=login_output)
71
+
72
+ with gr.Tab("Signup"):
73
+ name = gr.Textbox(label="Name", placeholder="Enter your name")
74
+ signup_email = gr.Textbox(label="Email", placeholder="Enter your email")
75
+ phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
76
+ signup_password = gr.Textbox(label="Password", type="password", placeholder="Create a password")
77
+ signup_button = gr.Button("Signup")
78
+ signup_output = gr.Textbox(label="Signup Status")
79
+ signup_button.click(signup, inputs=[name, signup_email, phone, signup_password], outputs=signup_output)
80
+
81
+ with gr.Tab("Menu"):
82
+ preference = gr.Radio(["All", "Veg", "Non-Veg"], label="Filter Menu", value="All")
83
+ menu_output = gr.Dataframe(headers=["Name", "Description", "Price"])
84
+ preference.change(lambda p: load_menu(p), inputs=[preference], outputs=menu_output)
85
+ item_name = gr.Textbox(label="Item Name")
86
+ item_price = gr.Textbox(label="Price")
87
+ add_button = gr.Button("Add to Cart")
88
+ cart_status = gr.Textbox(label="Cart Status")
89
+ add_button.click(add_to_cart_interface, inputs=[item_name, item_price], outputs=cart_status)
90
+
91
+ with gr.Tab("Cart"):
92
+ view_button = gr.Button("View Cart")
93
+ cart_output = gr.Dataframe(headers=["Name", "Price", "Quantity"])
94
+ total_output = gr.Textbox(label="Total Amount")
95
+ view_button.click(view_cart_interface, outputs=[cart_output, total_output])
96
+
97
+ place_order_button = gr.Button("Place Order")
98
+ order_status = gr.Textbox(label="Order Status")
99
+ place_order_button.click(place_order_interface, outputs=order_status)
100
 
101
+ app.launch()