geethareddy commited on
Commit
c6b41d3
·
verified ·
1 Parent(s): 177e475

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -159
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from simple_salesforce import Salesforce
3
 
@@ -110,28 +111,6 @@ def filter_menu(preference):
110
  return "<p>No items match your filter.</p>"
111
 
112
  return html_content
113
- def save_order_to_salesforce(order_json):
114
- try:
115
- # Parse JSON data from the hidden input field
116
- order_data = json.loads(order_json)
117
- cart_summary = json.dumps(order_data['cartSummary']) # Convert to JSON string for Salesforce
118
- total_cost = float(order_data['totalCost'])
119
- login_details = order_data['loginDetails']
120
-
121
- # Save to Salesforce
122
- sf.Order__c.create({
123
- 'Items__c': cart_summary,
124
- 'Total_Cost__c': total_cost,
125
- 'Name2__c': login_details['name'],
126
- 'Email__c': login_details['email'],
127
- })
128
- return "Order saved to Salesforce successfully!"
129
- except Exception as e:
130
- return f"Error saving order to Salesforce: {str(e)}"
131
-
132
- # Link the hidden field to the backend function
133
- order_data.change(save_order_to_salesforce, inputs=order_data, outputs=None)
134
-
135
 
136
  # Create Modal Window HTML
137
  def create_modal_window():
@@ -164,22 +143,6 @@ def create_modal_window():
164
  <textarea id="special-instructions" placeholder="Add your special instructions here..." style="width: 100%; height: 60px;"></textarea>
165
  <button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
166
  </div>
167
- <div id="cart-modal" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;">
168
- <div style="padding: 20px;">
169
- <div style="text-align: right;">
170
- <button onclick="closeCartModal()" style="background: none; border: none; font-size: 24px; cursor: pointer;">&times;</button>
171
- </div>
172
- <h1>Your Cart</h1>
173
- <div id="cart-items"></div>
174
- <p id="cart-total-cost" style="font-size: 1.2em; font-weight: bold;">Total Cart Cost: $0.00</p>
175
- <div id="order-summary" style="margin-top: 20px;">
176
- <h2>Final Order Summary:</h2>
177
- <div id="final-order-summary"></div>
178
- <p><strong>Total Bill: $<span id="total-bill"></span></strong></p>
179
- </div>
180
- <button style="background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;" onclick="proceedToCheckout()">Proceed to Checkout</button>
181
- </div>
182
- </div>
183
  """
184
  return modal_html
185
 
@@ -280,35 +243,7 @@ def modal_js():
280
  totalCostElement.innerText = Total Cart Cost: $${totalCartCost.toFixed(2)};
281
  }
282
  function proceedToCheckout() {
283
- const cartSummary = cart.map(item => ({
284
- name: item.name,
285
- quantity: item.quantity,
286
- totalCost: item.totalCost.toFixed(2),
287
- extras: item.extras.map(extra => extra.name).join(', ') || 'None',
288
- instructions: item.instructions || 'None',
289
- }));
290
- const totalCost = totalCartCost.toFixed(2);
291
- const loginDetails = {
292
- email: document.getElementById('login-email').value,
293
- name: document.getElementById('login-name').innerText,
294
- };
295
- // Send data to the hidden Gradio field
296
- const orderData = {
297
- cartSummary,
298
- totalCost,
299
- loginDetails,
300
- };
301
- // Use Gradio's built-in JS function to set value of hidden field
302
- gradioApp().querySelector('#order_data input').value = JSON.stringify(orderData);
303
- // Preserve original functionality of proceedToCheckout
304
- const cartSummaryHtml = cart.map(item =>
305
- ${item.name} (x${item.quantity}) - $${item.totalCost.toFixed(2)}
306
- Extras: ${item.extras.map(extra => extra.name).join(', ') || 'None'}
307
- Instructions: ${item.instructions || 'None'}
308
- ).join('<br>');
309
- document.getElementById('final-order-summary').innerHTML = cartSummaryHtml;
310
- document.getElementById('total-bill').innerText = totalCost;
311
- openCartModal();
312
  }
313
  // Reset all selected add-ons when opening a new item modal
314
  function resetAddOns() {
@@ -319,106 +254,19 @@ def modal_js():
319
  """
320
  return modal_script
321
 
322
- import bcrypt
323
- import gradio as gr
324
- from simple_salesforce import Salesforce
325
- import json
326
-
327
- # Salesforce Connection
328
- sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
329
-
330
- # Function to Hash Password
331
- def hash_password(password):
332
- return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
333
-
334
- # Function to Verify Password
335
- def verify_password(plain_password, hashed_password):
336
- return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
337
-
338
- # Signup function
339
- def signup(name, email, phone, password):
340
- try:
341
- email = email.strip()
342
- query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'"
343
- result = sf.query(query)
344
-
345
- if len(result['records']) > 0:
346
- return "Email already exists! Please use a different email."
347
-
348
- hashed_password = hash_password(password)
349
-
350
- sf.Customer_Login__c.create({
351
- 'Name': name.strip(),
352
- 'Email__c': email,
353
- 'Phone_Number__c': phone.strip(),
354
- 'Password__c': hashed_password
355
- })
356
- return "Signup successful! You can now login."
357
- except Exception as e:
358
- return f"Error during signup: {str(e)}"
359
-
360
- # Login function
361
- def login(email, password):
362
- try:
363
- email = email.strip()
364
- query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
365
- result = sf.query(query)
366
-
367
- if len(result['records']) == 0:
368
- return "Invalid email or password.", None
369
-
370
- user = result['records'][0]
371
- stored_password = user['Password__c']
372
-
373
- if verify_password(password.strip(), stored_password):
374
- return "Login successful!", user['Name']
375
- else:
376
- return "Invalid email or password.", None
377
- except Exception as e:
378
- return f"Error during login: {str(e)}", None
379
-
380
- # Function to Save Order Data to Salesforce
381
- def save_order_to_salesforce(order_json):
382
- try:
383
- # Parse JSON data
384
- order_data = json.loads(order_json)
385
- cart_summary = json.dumps(order_data['cartSummary']) # Convert cart to JSON string
386
- total_cost = float(order_data['totalCost'])
387
- login_details = order_data['loginDetails']
388
-
389
- # Save to Salesforce
390
- sf.Order__c.create({
391
- 'Items__c': cart_summary,
392
- 'Total_Cost__c': total_cost,
393
- 'Name2__c': login_details['name'],
394
- 'Email__c': login_details['email'],
395
- })
396
- return "Order saved to Salesforce successfully!"
397
- except Exception as e:
398
- return f"Error saving order to Salesforce: {str(e)}"
399
-
400
  # Gradio App
401
  with gr.Blocks() as app:
402
- # Define the hidden field for order data first
403
- order_data = gr.Textbox(visible=False, label="Order Data", interactive=False)
404
-
405
- # Attach the change event to the hidden field
406
- order_data.change(save_order_to_salesforce, inputs=order_data, outputs=None)
407
-
408
- # Header
409
  with gr.Row():
410
  gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
411
 
412
- # Login Page
413
  with gr.Row(visible=True) as login_page:
414
  with gr.Column():
415
- login_email = gr.Textbox(label="Email", id="login-email")
416
  login_password = gr.Textbox(label="Password", type="password")
417
  login_button = gr.Button("Login")
418
  signup_button = gr.Button("Go to Signup")
419
  login_output = gr.Textbox(label="Status")
420
 
421
- # Signup Page
422
  with gr.Row(visible=False) as signup_page:
423
  with gr.Column():
424
  signup_name = gr.Textbox(label="Name")
@@ -429,19 +277,20 @@ with gr.Blocks() as app:
429
  login_redirect = gr.Button("Go to Login")
430
  signup_output = gr.Textbox(label="Status")
431
 
432
- # Menu Page
433
  with gr.Row(visible=False) as menu_page:
434
  with gr.Column():
435
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
436
  menu_output = gr.HTML()
437
  gr.HTML("<div id='cart-button' style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>")
438
- gr.HTML("<div id='cart-modal' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;'><div style='padding: 20px;'><div style='text-align: right;'><button onclick='closeCartModal()' style='background: none; border: none; font-size: 24px; cursor: pointer;'>&times;</button></div><h1>Your Cart</h1><div id='cart-items'></div><p id='cart-total-cost' style='font-size: 1.2em; font-weight: bold;'>Total Cart Cost: $0.00</p><div id='order-summary'><h2>Final Order Summary:</h2><div id='final-order-summary'></div><p><strong>Total Bill: $<span id='total-bill'></span></strong></p></div><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button></div></div>")
 
 
439
 
440
- # Event handling for login button
441
  login_button.click(
442
- lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value="Menu loaded"), "Login successful!")
443
  if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
444
  [login_email, login_password], [login_page, menu_page, menu_output, login_output]
445
  )
 
446
 
447
  app.launch()
 
1
+ import bcrypt
2
  import gradio as gr
3
  from simple_salesforce import Salesforce
4
 
 
111
  return "<p>No items match your filter.</p>"
112
 
113
  return html_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  # Create Modal Window HTML
116
  def create_modal_window():
 
143
  <textarea id="special-instructions" placeholder="Add your special instructions here..." style="width: 100%; height: 60px;"></textarea>
144
  <button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
145
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  """
147
  return modal_html
148
 
 
243
  totalCostElement.innerText = Total Cart Cost: $${totalCartCost.toFixed(2)};
244
  }
245
  function proceedToCheckout() {
246
+ alert("Proceeding to checkout...");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  }
248
  // Reset all selected add-ons when opening a new item modal
249
  function resetAddOns() {
 
254
  """
255
  return modal_script
256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  # Gradio App
258
  with gr.Blocks() as app:
 
 
 
 
 
 
 
259
  with gr.Row():
260
  gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
261
 
 
262
  with gr.Row(visible=True) as login_page:
263
  with gr.Column():
264
+ login_email = gr.Textbox(label="Email")
265
  login_password = gr.Textbox(label="Password", type="password")
266
  login_button = gr.Button("Login")
267
  signup_button = gr.Button("Go to Signup")
268
  login_output = gr.Textbox(label="Status")
269
 
 
270
  with gr.Row(visible=False) as signup_page:
271
  with gr.Column():
272
  signup_name = gr.Textbox(label="Name")
 
277
  login_redirect = gr.Button("Go to Login")
278
  signup_output = gr.Textbox(label="Status")
279
 
 
280
  with gr.Row(visible=False) as menu_page:
281
  with gr.Column():
282
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
283
  menu_output = gr.HTML()
284
  gr.HTML("<div id='cart-button' style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>")
285
+ gr.HTML("<div id='cart-modal' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;'><div style='padding: 20px;'><div style='text-align: right;'><button onclick='closeCartModal()' style='background: none; border: none; font-size: 24px; cursor: pointer;'>&times;</button></div><h1>Your Cart</h1><div id='cart-items'></div><p id='cart-total-cost' style='font-size: 1.2em; font-weight: bold;'>Total Cart Cost: $0.00</p><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button></div></div>")
286
+ gr.HTML(create_modal_window())
287
+ gr.HTML(modal_js())
288
 
 
289
  login_button.click(
290
+ lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
291
  if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
292
  [login_email, login_password], [login_page, menu_page, menu_output, login_output]
293
  )
294
+ preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
295
 
296
  app.launch()