Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,101 @@
|
|
| 1 |
-
|
| 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 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
def
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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 |
-
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return render_template("signup.html")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
def
|
| 40 |
-
|
| 41 |
-
return
|
| 42 |
|
| 43 |
-
|
| 44 |
-
def
|
| 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
|
| 53 |
|
| 54 |
-
|
| 55 |
-
def
|
| 56 |
-
email =
|
|
|
|
|
|
|
| 57 |
cart, total = view_cart()
|
| 58 |
if not cart:
|
| 59 |
-
return
|
| 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
|
| 65 |
except Exception as e:
|
| 66 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 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()
|
|
|
|
|
|