Update app.py
Browse files
app.py
CHANGED
@@ -6,58 +6,48 @@ from simple_salesforce import Salesforce
|
|
6 |
# Salesforce Connection
|
7 |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
{
|
20 |
-
"name": "Chicken Biryani",
|
21 |
-
"price": 15.0,
|
22 |
-
"description": "Flavorful rice dish cooked with chicken and spices.",
|
23 |
-
"image": "https://via.placeholder.com/200x200",
|
24 |
-
"veg_nonveg": "Non-Veg",
|
25 |
-
"section": "Biryani",
|
26 |
-
},
|
27 |
-
{
|
28 |
-
"name": "Paneer Butter Masala",
|
29 |
-
"price": 12.0,
|
30 |
-
"description": "Paneer cubes cooked in a creamy tomato-based gravy.",
|
31 |
-
"image": "https://via.placeholder.com/200x200",
|
32 |
-
"veg_nonveg": "Veg",
|
33 |
-
"section": "Curries",
|
34 |
-
}
|
35 |
-
]
|
36 |
-
|
37 |
-
# Sample Add-Ons
|
38 |
-
sample_add_ons = [
|
39 |
-
{"name": "Extra Cheese", "price": 2.0},
|
40 |
-
{"name": "Extra Spicy", "price": 1.5},
|
41 |
-
{"name": "No Onions", "price": 0.5},
|
42 |
-
]
|
43 |
-
|
44 |
-
# Function to Save Cart Summary to Salesforce
|
45 |
-
def save_cart_to_salesforce(cart, total_cost):
|
46 |
try:
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
order_record = {
|
52 |
'Name': f"Order {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
|
53 |
'Total_Cost__c': total_cost,
|
54 |
-
'Order_Date__c': datetime.now().isoformat()
|
55 |
}
|
56 |
order_result = sf.Order__c.create(order_record)
|
57 |
order_id = order_result['id']
|
58 |
|
59 |
-
#
|
60 |
-
for item in
|
61 |
extras = ", ".join(extra['name'] for extra in item.get('extras', []))
|
62 |
order_item_record = {
|
63 |
'Name': item['name'],
|
@@ -66,92 +56,80 @@ def save_cart_to_salesforce(cart, total_cost):
|
|
66 |
'Price__c': item['price'],
|
67 |
'Extras__c': extras,
|
68 |
'Instructions__c': item.get('instructions', ''),
|
69 |
-
'Total_Cost__c': item['
|
70 |
}
|
71 |
sf.Order_Item__c.create(order_item_record)
|
72 |
|
73 |
-
return "Order saved successfully!"
|
74 |
except Exception as e:
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
<img src="{item['image']}" style="width:100px; height:100px;">
|
93 |
-
<h3>{item['name']}</h3>
|
94 |
-
<p>{item['description']}</p>
|
95 |
-
<p>Price: ${item['price']}</p>
|
96 |
-
</div>
|
97 |
-
"""
|
98 |
-
return html
|
99 |
-
|
100 |
-
# Function to Add Items to Cart
|
101 |
-
def add_to_cart(name, price, quantity, extras, instructions):
|
102 |
-
total_cost = (price * quantity) + sum(extra["price"] for extra in extras)
|
103 |
-
cart_item = {
|
104 |
-
"name": name,
|
105 |
-
"price": price,
|
106 |
-
"quantity": quantity,
|
107 |
-
"extras": extras,
|
108 |
-
"instructions": instructions,
|
109 |
-
"totalCost": total_cost,
|
110 |
-
}
|
111 |
-
return cart_item, total_cost
|
112 |
-
|
113 |
-
# Gradio Interface
|
114 |
with gr.Blocks() as app:
|
115 |
with gr.Row():
|
116 |
gr.HTML("<h1>Welcome to Biryani Hub</h1>")
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
with gr.Row():
|
121 |
-
cart_items = gr.JSON(label="Cart Items")
|
122 |
-
total_cost = gr.Textbox(label="Total Cost")
|
123 |
item_name = gr.Textbox(label="Item Name")
|
124 |
-
item_price = gr.Number(label="Price")
|
125 |
item_quantity = gr.Number(label="Quantity", value=1)
|
126 |
-
|
127 |
-
|
128 |
-
add_item_button = gr.Button("Add to Cart")
|
129 |
|
130 |
with gr.Row():
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
#
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
app.launch()
|
|
|
6 |
# Salesforce Connection
|
7 |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
8 |
|
9 |
+
# Function to Hash Password
|
10 |
+
def hash_password(password):
|
11 |
+
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
12 |
+
|
13 |
+
# Function to Verify Password
|
14 |
+
def verify_password(plain_password, hashed_password):
|
15 |
+
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
|
16 |
+
|
17 |
+
# Fetch menu items from Salesforce
|
18 |
+
def fetch_menu_items():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
try:
|
20 |
+
query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
|
21 |
+
result = sf.query(query)
|
22 |
+
return result['records']
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Error fetching menu items: {e}")
|
25 |
+
return []
|
26 |
|
27 |
+
# Fetch add-ons from Salesforce
|
28 |
+
def fetch_add_ons():
|
29 |
+
try:
|
30 |
+
query = "SELECT Name, Price__c FROM Add_Ons__c"
|
31 |
+
result = sf.query(query)
|
32 |
+
return result['records']
|
33 |
+
except Exception as e:
|
34 |
+
print(f"Error fetching add-ons: {e}")
|
35 |
+
return []
|
36 |
+
|
37 |
+
# Save cart details to Salesforce
|
38 |
+
def save_cart(cart_items, total_cost):
|
39 |
+
try:
|
40 |
+
# Create order record
|
41 |
order_record = {
|
42 |
'Name': f"Order {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
|
43 |
'Total_Cost__c': total_cost,
|
44 |
+
'Order_Date__c': datetime.now().isoformat()
|
45 |
}
|
46 |
order_result = sf.Order__c.create(order_record)
|
47 |
order_id = order_result['id']
|
48 |
|
49 |
+
# Save cart items as order items
|
50 |
+
for item in cart_items:
|
51 |
extras = ", ".join(extra['name'] for extra in item.get('extras', []))
|
52 |
order_item_record = {
|
53 |
'Name': item['name'],
|
|
|
56 |
'Price__c': item['price'],
|
57 |
'Extras__c': extras,
|
58 |
'Instructions__c': item.get('instructions', ''),
|
59 |
+
'Total_Cost__c': item['total_cost']
|
60 |
}
|
61 |
sf.Order_Item__c.create(order_item_record)
|
62 |
|
63 |
+
return f"Order {order_id} saved successfully!"
|
64 |
except Exception as e:
|
65 |
+
print(f"Error saving cart: {e}")
|
66 |
+
return f"Error saving cart: {str(e)}"
|
67 |
+
|
68 |
+
# Function to filter menu based on preference
|
69 |
+
def filter_menu(preference):
|
70 |
+
menu_items = fetch_menu_items()
|
71 |
+
filtered_items = [item for item in menu_items if preference == "All" or item['Veg_NonVeg__c'] == preference]
|
72 |
+
|
73 |
+
menu_output = {}
|
74 |
+
for item in filtered_items:
|
75 |
+
section = item.get('Section__c', 'Uncategorized')
|
76 |
+
if section not in menu_output:
|
77 |
+
menu_output[section] = []
|
78 |
+
menu_output[section].append(item)
|
79 |
+
return menu_output
|
80 |
+
|
81 |
+
# Gradio App
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
with gr.Blocks() as app:
|
83 |
with gr.Row():
|
84 |
gr.HTML("<h1>Welcome to Biryani Hub</h1>")
|
85 |
+
|
86 |
+
# Menu and Cart Interface
|
87 |
+
with gr.Row():
|
88 |
+
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], value="All", label="Filter Preference")
|
89 |
+
menu_display = gr.JSON(label="Menu Items")
|
90 |
+
cart_display = gr.JSON(label="Cart Items")
|
91 |
+
total_cost_display = gr.Number(label="Total Cost", value=0)
|
92 |
|
93 |
with gr.Row():
|
|
|
|
|
94 |
item_name = gr.Textbox(label="Item Name")
|
95 |
+
item_price = gr.Number(label="Price", value=0)
|
96 |
item_quantity = gr.Number(label="Quantity", value=1)
|
97 |
+
add_ons_display = gr.JSON(label="Add-Ons")
|
98 |
+
special_instructions = gr.Textbox(label="Special Instructions")
|
|
|
99 |
|
100 |
with gr.Row():
|
101 |
+
add_to_cart_button = gr.Button("Add to Cart")
|
102 |
+
save_order_button = gr.Button("Save Order")
|
103 |
+
|
104 |
+
# State
|
105 |
+
cart = gr.State([]) # Cart state to maintain items
|
106 |
+
|
107 |
+
def update_menu(preference):
|
108 |
+
menu_items = filter_menu(preference)
|
109 |
+
return menu_items, fetch_add_ons()
|
110 |
+
|
111 |
+
def add_to_cart(cart, name, price, quantity, add_ons, instructions):
|
112 |
+
if not name or quantity <= 0:
|
113 |
+
return cart, "Invalid item details!"
|
114 |
+
total_cost = price * quantity
|
115 |
+
cart.append({
|
116 |
+
"name": name,
|
117 |
+
"price": price,
|
118 |
+
"quantity": quantity,
|
119 |
+
"extras": add_ons,
|
120 |
+
"instructions": instructions,
|
121 |
+
"total_cost": total_cost
|
122 |
+
})
|
123 |
+
return cart, f"Added {name} to cart!"
|
124 |
+
|
125 |
+
def save_order(cart):
|
126 |
+
total_cost = sum(item['total_cost'] for item in cart)
|
127 |
+
response = save_cart(cart, total_cost)
|
128 |
+
return response
|
129 |
+
|
130 |
+
# Interactivity
|
131 |
+
preference.change(update_menu, inputs=[preference], outputs=[menu_display, add_ons_display])
|
132 |
+
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])
|
133 |
+
save_order_button.click(save_order, inputs=[cart], outputs=[total_cost_display])
|
134 |
|
135 |
app.launch()
|