Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,19 +13,21 @@ def hash_password(password):
|
|
| 13 |
def verify_password(plain_password, hashed_password):
|
| 14 |
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
|
| 15 |
|
| 16 |
-
# Signup
|
| 17 |
def signup(name, email, phone, password):
|
| 18 |
try:
|
| 19 |
-
|
|
|
|
| 20 |
result = sf.query(query)
|
| 21 |
|
| 22 |
-
if result['records']:
|
| 23 |
return "Email already exists! Please use a different email."
|
| 24 |
|
| 25 |
hashed_password = hash_password(password)
|
|
|
|
| 26 |
sf.Customer_Login__c.create({
|
| 27 |
'Name': name.strip(),
|
| 28 |
-
'Email__c': email
|
| 29 |
'Phone_Number__c': phone.strip(),
|
| 30 |
'Password__c': hashed_password
|
| 31 |
})
|
|
@@ -33,24 +35,84 @@ def signup(name, email, phone, password):
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error during signup: {str(e)}"
|
| 35 |
|
| 36 |
-
# Login
|
| 37 |
def login(email, password):
|
| 38 |
try:
|
| 39 |
-
|
|
|
|
| 40 |
result = sf.query(query)
|
| 41 |
|
| 42 |
-
if
|
| 43 |
return "Invalid email or password.", None
|
| 44 |
|
| 45 |
user = result['records'][0]
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
return "Login successful!", user['Name']
|
| 48 |
else:
|
| 49 |
return "Invalid email or password.", None
|
| 50 |
except Exception as e:
|
| 51 |
return f"Error during login: {str(e)}", None
|
| 52 |
|
| 53 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def submit_order_to_salesforce(cart):
|
| 55 |
try:
|
| 56 |
for item in cart:
|
|
@@ -72,37 +134,63 @@ def submit_order_to_salesforce(cart):
|
|
| 72 |
|
| 73 |
# Gradio App
|
| 74 |
with gr.Blocks() as app:
|
| 75 |
-
# UI Elements
|
| 76 |
with gr.Row():
|
| 77 |
-
gr.HTML("<h1>Welcome to Biryani Hub</h1>")
|
|
|
|
| 78 |
with gr.Row(visible=True) as login_page:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
with gr.Row(visible=False) as menu_page:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
# Login Logic
|
| 90 |
login_button.click(
|
| 91 |
-
lambda email, password: (
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
-
|
|
|
|
| 103 |
submit_cart.click(
|
| 104 |
lambda cart: submit_order_to_salesforce(cart),
|
| 105 |
-
inputs=
|
| 106 |
outputs=cart_status,
|
| 107 |
)
|
| 108 |
|
|
|
|
| 13 |
def verify_password(plain_password, hashed_password):
|
| 14 |
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
|
| 15 |
|
| 16 |
+
# Signup function
|
| 17 |
def signup(name, email, phone, password):
|
| 18 |
try:
|
| 19 |
+
email = email.strip()
|
| 20 |
+
query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'"
|
| 21 |
result = sf.query(query)
|
| 22 |
|
| 23 |
+
if len(result['records']) > 0:
|
| 24 |
return "Email already exists! Please use a different email."
|
| 25 |
|
| 26 |
hashed_password = hash_password(password)
|
| 27 |
+
|
| 28 |
sf.Customer_Login__c.create({
|
| 29 |
'Name': name.strip(),
|
| 30 |
+
'Email__c': email,
|
| 31 |
'Phone_Number__c': phone.strip(),
|
| 32 |
'Password__c': hashed_password
|
| 33 |
})
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error during signup: {str(e)}"
|
| 37 |
|
| 38 |
+
# Login function
|
| 39 |
def login(email, password):
|
| 40 |
try:
|
| 41 |
+
email = email.strip()
|
| 42 |
+
query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
|
| 43 |
result = sf.query(query)
|
| 44 |
|
| 45 |
+
if len(result['records']) == 0:
|
| 46 |
return "Invalid email or password.", None
|
| 47 |
|
| 48 |
user = result['records'][0]
|
| 49 |
+
stored_password = user['Password__c']
|
| 50 |
+
|
| 51 |
+
if verify_password(password.strip(), stored_password):
|
| 52 |
return "Login successful!", user['Name']
|
| 53 |
else:
|
| 54 |
return "Invalid email or password.", None
|
| 55 |
except Exception as e:
|
| 56 |
return f"Error during login: {str(e)}", None
|
| 57 |
|
| 58 |
+
# Function to load menu data
|
| 59 |
+
def load_menu_from_salesforce():
|
| 60 |
+
try:
|
| 61 |
+
query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
|
| 62 |
+
result = sf.query(query)
|
| 63 |
+
return result['records']
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return []
|
| 66 |
+
|
| 67 |
+
# Function to load add-ons data
|
| 68 |
+
def load_add_ons_from_salesforce():
|
| 69 |
+
try:
|
| 70 |
+
query = "SELECT Name, Price__c FROM Add_Ons__c"
|
| 71 |
+
result = sf.query(query)
|
| 72 |
+
return result['records']
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return []
|
| 75 |
+
|
| 76 |
+
# Function to filter menu items
|
| 77 |
+
def filter_menu(preference):
|
| 78 |
+
menu_data = load_menu_from_salesforce()
|
| 79 |
+
|
| 80 |
+
filtered_data = {}
|
| 81 |
+
for item in menu_data:
|
| 82 |
+
if "Section__c" not in item or "Veg_NonVeg__c" not in item:
|
| 83 |
+
continue
|
| 84 |
+
|
| 85 |
+
if item["Section__c"] not in filtered_data:
|
| 86 |
+
filtered_data[item["Section__c"]] = []
|
| 87 |
+
|
| 88 |
+
if preference == "All" or (preference == "Veg" and item["Veg_NonVeg__c"] in ["Veg", "Both"]) or (preference == "Non-Veg" and item["Veg_NonVeg__c"] in ["Non veg", "Both"]):
|
| 89 |
+
filtered_data[item["Section__c"].strip()].append(item)
|
| 90 |
+
|
| 91 |
+
html_content = '<div style="padding: 0 10px; max-width: 1200px; margin: auto;">'
|
| 92 |
+
for section, items in filtered_data.items():
|
| 93 |
+
html_content += f"<h2 style='text-align: center; margin-top: 5px;'>{section}</h2>"
|
| 94 |
+
html_content += '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px; justify-content: center; margin-top: 10px;">'
|
| 95 |
+
for item in items:
|
| 96 |
+
html_content += f"""
|
| 97 |
+
<div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;">
|
| 98 |
+
<img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;"
|
| 99 |
+
onclick="openModal('{item['Name']}', '{item.get('Image2__c', '')}', '{item['Description__c']}', '${item['Price__c']}')">
|
| 100 |
+
<div style="padding: 10px;">
|
| 101 |
+
<h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3>
|
| 102 |
+
<p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p>
|
| 103 |
+
<p style='font-size: 0.9em; text-align: justify; margin: 5px;'>{item['Description__c']}</p>
|
| 104 |
+
</div>
|
| 105 |
+
</div>
|
| 106 |
+
"""
|
| 107 |
+
html_content += '</div>'
|
| 108 |
+
html_content += '</div>'
|
| 109 |
+
|
| 110 |
+
if not any(filtered_data.values()):
|
| 111 |
+
return "<p>No items match your filter.</p>"
|
| 112 |
+
|
| 113 |
+
return html_content
|
| 114 |
+
|
| 115 |
+
# Function to submit order to Salesforce
|
| 116 |
def submit_order_to_salesforce(cart):
|
| 117 |
try:
|
| 118 |
for item in cart:
|
|
|
|
| 134 |
|
| 135 |
# Gradio App
|
| 136 |
with gr.Blocks() as app:
|
|
|
|
| 137 |
with gr.Row():
|
| 138 |
+
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
| 139 |
+
|
| 140 |
with gr.Row(visible=True) as login_page:
|
| 141 |
+
with gr.Column():
|
| 142 |
+
login_email = gr.Textbox(label="Email")
|
| 143 |
+
login_password = gr.Textbox(label="Password", type="password")
|
| 144 |
+
login_button = gr.Button("Login")
|
| 145 |
+
signup_button = gr.Button("Go to Signup")
|
| 146 |
+
login_output = gr.Textbox(label="Status")
|
| 147 |
+
|
| 148 |
+
with gr.Row(visible=False) as signup_page:
|
| 149 |
+
with gr.Column():
|
| 150 |
+
signup_name = gr.Textbox(label="Name")
|
| 151 |
+
signup_email = gr.Textbox(label="Email")
|
| 152 |
+
signup_phone = gr.Textbox(label="Phone")
|
| 153 |
+
signup_password = gr.Textbox(label="Password", type="password")
|
| 154 |
+
submit_signup = gr.Button("Signup")
|
| 155 |
+
login_redirect = gr.Button("Go to Login")
|
| 156 |
+
signup_output = gr.Textbox(label="Status")
|
| 157 |
+
|
| 158 |
with gr.Row(visible=False) as menu_page:
|
| 159 |
+
with gr.Column():
|
| 160 |
+
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
| 161 |
+
menu_output = gr.HTML()
|
| 162 |
+
cart_input = gr.JSON(label="Cart Data")
|
| 163 |
+
submit_cart = gr.Button("Submit Order")
|
| 164 |
+
cart_status = gr.Textbox(label="Order Submission Status")
|
| 165 |
|
|
|
|
| 166 |
login_button.click(
|
| 167 |
+
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
| 168 |
+
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
|
| 169 |
+
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
|
| 170 |
+
)
|
| 171 |
+
submit_signup.click(
|
| 172 |
+
lambda name, email, phone, password: signup(name, email, phone, password),
|
| 173 |
+
inputs=[signup_name, signup_email, signup_phone, signup_password],
|
| 174 |
+
outputs=signup_output
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
signup_button.click(
|
| 178 |
+
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
| 179 |
+
inputs=[],
|
| 180 |
+
outputs=[login_page, signup_page]
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
login_redirect.click(
|
| 184 |
+
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
| 185 |
+
inputs=[],
|
| 186 |
+
outputs=[login_page, signup_page]
|
| 187 |
)
|
| 188 |
|
| 189 |
+
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
|
| 190 |
+
|
| 191 |
submit_cart.click(
|
| 192 |
lambda cart: submit_order_to_salesforce(cart),
|
| 193 |
+
inputs=cart_input,
|
| 194 |
outputs=cart_status,
|
| 195 |
)
|
| 196 |
|