Cart / app.py
dschandra's picture
Update app.py
48267cf verified
raw
history blame
4.93 kB
import bcrypt
import gradio as gr
from datetime import datetime
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'))
# Fetch menu items from Salesforce
def fetch_menu_items():
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:
print(f"Error fetching menu items: {e}")
return []
# Fetch add-ons from Salesforce
def fetch_add_ons():
try:
query = "SELECT Name, Price__c FROM Add_Ons__c"
result = sf.query(query)
return result['records']
except Exception as e:
print(f"Error fetching add-ons: {e}")
return []
# Save cart details to Salesforce
def save_cart(cart_items, total_cost):
try:
# Create order record
order_record = {
'Name': f"Order {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
'Total_Cost__c': total_cost,
'Order_Date__c': datetime.now().isoformat()
}
order_result = sf.Order__c.create(order_record)
order_id = order_result['id']
# Save cart items as order items
for item in cart_items:
extras = ", ".join(extra['name'] for extra in item.get('extras', []))
order_item_record = {
'Name': item['name'],
'Order__c': order_id,
'Quantity__c': item['quantity'],
'Price__c': item['price'],
'Extras__c': extras,
'Instructions__c': item.get('instructions', ''),
'Total_Cost__c': item['total_cost']
}
sf.Order_Item__c.create(order_item_record)
return f"Order {order_id} saved successfully!"
except Exception as e:
print(f"Error saving cart: {e}")
return f"Error saving cart: {str(e)}"
# Function to filter menu based on preference
def filter_menu(preference):
menu_items = fetch_menu_items()
filtered_items = [item for item in menu_items if preference == "All" or item['Veg_NonVeg__c'] == preference]
menu_output = {}
for item in filtered_items:
section = item.get('Section__c', 'Uncategorized')
if section not in menu_output:
menu_output[section] = []
menu_output[section].append(item)
return menu_output
# Gradio App
with gr.Blocks() as app:
with gr.Row():
gr.HTML("<h1>Welcome to Biryani Hub</h1>")
# Menu and Cart Interface
with gr.Row():
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], value="All", label="Filter Preference")
menu_display = gr.JSON(label="Menu Items")
cart_display = gr.JSON(label="Cart Items")
total_cost_display = gr.Number(label="Total Cost", value=0)
with gr.Row():
item_name = gr.Textbox(label="Item Name")
item_price = gr.Number(label="Price", value=0)
item_quantity = gr.Number(label="Quantity", value=1)
add_ons_display = gr.JSON(label="Add-Ons")
special_instructions = gr.Textbox(label="Special Instructions")
with gr.Row():
add_to_cart_button = gr.Button("Add to Cart")
save_order_button = gr.Button("Save Order")
# State
cart = gr.State([]) # Cart state to maintain items
def update_menu(preference):
menu_items = filter_menu(preference)
return menu_items, fetch_add_ons()
def add_to_cart(cart, name, price, quantity, add_ons, instructions):
if not name or quantity <= 0:
return cart, "Invalid item details!"
total_cost = price * quantity
cart.append({
"name": name,
"price": price,
"quantity": quantity,
"extras": add_ons,
"instructions": instructions,
"total_cost": total_cost
})
return cart, f"Added {name} to cart!"
def save_order(cart):
total_cost = sum(item['total_cost'] for item in cart)
response = save_cart(cart, total_cost)
return response
# Interactivity
preference.change(update_menu, inputs=[preference], outputs=[menu_display, add_ons_display])
add_to_cart_button.click(add_to_cart, inputs=[cart, item_name, item_price, item_quantity, add_ons_display, special_instructions], outputs=[cart_display, total_cost_display])
save_order_button.click(save_order, inputs=[cart], outputs=[total_cost_display])
app.launch()