Spaces:
Sleeping
Sleeping
import bcrypt | |
import gradio as gr | |
from simple_salesforce import Salesforce | |
# Salesforce Connection | |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') | |
# Function to Hash Password | |
def hash_password(password): | |
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') | |
# Function to Verify Password | |
def verify_password(plain_password, hashed_password): | |
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8')) | |
# Signup function | |
def signup(name, email, phone, password): | |
try: | |
email = email.strip() | |
query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'" | |
result = sf.query(query) | |
if len(result['records']) > 0: | |
return "Email already exists! Please use a different email." | |
hashed_password = hash_password(password) | |
sf.Customer_Login__c.create({ | |
'Name': name.strip(), | |
'Email__c': email, | |
'Phone_Number__c': phone.strip(), | |
'Password__c': hashed_password | |
}) | |
return "Signup successful! You can now login." | |
except Exception as e: | |
return f"Error during signup: {str(e)}" | |
# Login function | |
def login(email, password): | |
try: | |
email = email.strip() | |
query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'" | |
result = sf.query(query) | |
if len(result['records']) == 0: | |
return "Invalid email or password.", None | |
user = result['records'][0] | |
stored_password = user['Password__c'] | |
if verify_password(password.strip(), stored_password): | |
return "Login successful!", user['Name'] | |
else: | |
return "Invalid email or password.", None | |
except Exception as e: | |
return f"Error during login: {str(e)}", None | |
# Function to load menu data | |
def load_menu_from_salesforce(): | |
try: | |
query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c" | |
result = sf.query(query) | |
return result['records'] | |
except Exception as e: | |
return [] | |
# Function to filter menu items | |
def filter_menu(preference): | |
menu_data = load_menu_from_salesforce() | |
filtered_data = {} | |
for item in menu_data: | |
if "Section__c" not in item or "Veg_NonVeg__c" not in item: | |
continue | |
if item["Section__c"] not in filtered_data: | |
filtered_data[item["Section__c"]] = [] | |
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"]): | |
filtered_data[item["Section__c"].strip()].append(item) | |
html_content = '<div style="padding: 0 10px; max-width: 1200px; margin: auto;">' | |
for section, items in filtered_data.items(): | |
html_content += f"<h2 style='text-align: center; margin-top: 5px;'>{section}</h2>" | |
html_content += '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px; justify-content: center; margin-top: 10px;">' | |
for item in items: | |
html_content += f""" | |
<div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;"> | |
<img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;" | |
onclick="openModal('{item['Name']}', '{item.get('Image2__c', '')}', '{item['Description__c']}', '${item['Price__c']}')"> | |
<div style="padding: 10px;"> | |
<h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3> | |
<p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p> | |
<p style='font-size: 0.9em; text-align: justify; margin: 5px;'>{item['Description__c']}</p> | |
</div> | |
</div> | |
""" | |
html_content += '</div>' | |
html_content += '</div>' | |
if not any(filtered_data.values()): | |
return "<p>No items match your filter.</p>" | |
return html_content | |
# JavaScript for Modal and Cart Functionality | |
def modal_js(): | |
modal_script = """ | |
<script> | |
let cart = []; | |
let totalCartCost = 0; | |
function openModal(name, image2, description, price) { | |
const modal = document.getElementById('modal'); | |
modal.style.display = 'block'; | |
modal.style.position = 'fixed'; | |
modal.style.width = window.innerWidth <= 768 ? '90%' : '30%'; | |
modal.style.top = `${event.clientY}px`; | |
modal.style.left = '50%'; | |
modal.style.transform = 'translate(-50%, -50%)'; | |
document.getElementById('modal-image').src = image2; | |
document.getElementById('modal-name').innerText = name; | |
document.getElementById('modal-description').innerText = description; | |
document.getElementById('modal-price').innerText = price; | |
document.getElementById('quantity').value = 1; | |
document.getElementById('special-instructions').value = ''; | |
} | |
function closeModal() { | |
document.getElementById('modal').style.display = 'none'; | |
} | |
function addToCart() { | |
const name = document.getElementById('modal-name').innerText; | |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', '')); | |
const quantity = parseInt(document.getElementById('quantity').value) || 1; | |
const instructions = document.getElementById('special-instructions').value; | |
const extrasCost = 0; // For simplicity, no extras are added here | |
const totalCost = price * quantity + extrasCost; | |
cart.push({ name, quantity, instructions, totalCost }); | |
totalCartCost += totalCost; | |
updateCartButton(); | |
closeModal(); | |
} | |
function updateCartButton() { | |
const cartButton = document.getElementById('cart-button'); | |
cartButton.innerText = `View Cart (${cart.length} items)`; | |
} | |
function submitCart() { | |
if (cart.length === 0) { | |
alert("Cart is empty. Please add items!"); | |
return; | |
} | |
let summary = "<h3>Order Summary</h3><ul>"; | |
cart.forEach(item => { | |
summary += `<li>${item.name} (x${item.quantity}) - $${item.totalCost.toFixed(2)}</li>`; | |
}); | |
summary += `</ul><h3>Total: $${totalCartCost.toFixed(2)}</h3>`; | |
document.getElementById('cart-summary').innerHTML = summary; | |
} | |
</script> | |
""" | |
return modal_script | |
# Gradio App | |
with gr.Blocks() as app: | |
with gr.Row(): | |
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>") | |
with gr.Row(visible=True) as login_page: | |
with gr.Column(): | |
login_email = gr.Textbox(label="Email") | |
login_password = gr.Textbox(label="Password", type="password") | |
login_button = gr.Button("Login") | |
signup_redirect = gr.Button("Go to Signup") | |
login_output = gr.Textbox(label="Status") | |
with gr.Row(visible=False) as signup_page: | |
with gr.Column(): | |
signup_name = gr.Textbox(label="Name") | |
signup_email = gr.Textbox(label="Email") | |
signup_phone = gr.Textbox(label="Phone") | |
signup_password = gr.Textbox(label="Password", type="password") | |
submit_signup = gr.Button("Signup") | |
login_redirect = gr.Button("Go to Login") | |
signup_output = gr.Textbox(label="Status") | |
with gr.Row(visible=False) as menu_page: | |
with gr.Column(): | |
preference = gr.Radio(["All", "Veg", "Non-Veg"], label="Filter Preference", value="All") | |
menu_output = gr.HTML() | |
submit_cart_button = gr.Button("Submit Cart", elem_id="cart-button") | |
cart_summary = gr.HTML(elem_id="cart-summary") | |
# Login Button Click | |
login_button.click( | |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), filter_menu("All"), "Login successful!") | |
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), "Invalid email or password."), | |
[login_email, login_password], | |
[login_page, menu_page, menu_output, login_output] | |
) | |
# Signup Button Click | |
submit_signup.click( | |
lambda name, email, phone, password: (signup(name, email, phone, password), gr.update(visible=False), gr.update(visible=True)), | |
[signup_name, signup_email, signup_phone, signup_password], | |
[signup_output, signup_page, login_page] | |
) | |
# Signup Redirect | |
signup_redirect.click( | |
lambda: (gr.update(visible=False), gr.update(visible=True)), | |
[], | |
[login_page, signup_page] | |
) | |
# Login Redirect | |
login_redirect.click( | |
lambda: (gr.update(visible=True), gr.update(visible=False)), | |
[], | |
[login_page, signup_page] | |
) | |
# Submit Cart Button | |
submit_cart_button.click( | |
None, | |
[], | |
[cart_summary], | |
_js="submitCart()" | |
) | |
app.launch() | |