nagasurendra commited on
Commit
c3fb9ca
·
verified ·
1 Parent(s): fc7416b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -41
app.py CHANGED
@@ -1,49 +1,123 @@
 
 
1
  from simple_salesforce import Salesforce
2
-
3
  # Salesforce Connection
4
  sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
5
 
6
- # Function to fetch menu items from Salesforce
7
- def load_menu_from_salesforce():
 
 
 
 
 
 
 
 
 
8
  try:
9
- query = "SELECT Name, Price__c, Description__c, Image1__c, Ingredients__c FROM Menu_Item__c"
 
 
 
10
  result = sf.query(query)
11
- return result['records']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  except Exception as e:
13
- raise ValueError(f"Error loading menu data from Salesforce: {e}")
14
-
15
- # Function to filter menu items based on preference
16
- def filter_menu_from_salesforce(preference):
17
- menu_data = load_menu_from_salesforce()
18
- filtered_data = []
19
-
20
- for item in menu_data:
21
- if preference == "Halal/Non-Veg":
22
- if any(x in item.get("Ingredients__c", "").lower() for x in ["chicken", "mutton", "fish", "prawns", "goat"]):
23
- filtered_data.append(item)
24
- elif preference == "Vegetarian":
25
- if not any(x in item.get("Ingredients__c", "").lower() for x in ["chicken", "mutton", "fish", "prawns", "goat"]):
26
- filtered_data.append(item)
27
- elif preference == "Guilt-Free":
28
- if "fat:" in item.get("Description__c", "").lower():
29
- filtered_data.append(item)
 
 
 
 
 
 
 
 
 
30
  else:
31
- filtered_data = menu_data
32
-
33
- # Generate HTML content
34
- html_content = ""
35
- for item in filtered_data:
36
- html_content += f"""
37
- <div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
38
- <div style="flex: 1; margin-right: 15px;">
39
- <h3 style="margin: 0; font-size: 18px;">{item['Name']}</h3>
40
- <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price__c']}</p>
41
- <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description__c']}</p>
42
- </div>
43
- <div style="flex-shrink: 0; text-align: center;">
44
- <img src="{item['Image1__c']}" alt="{item['Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
45
- <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="openModal('{item['Name']}', '{item['Image1__c']}', '{item['Description__c']}', '{item['Price__c']}')">Add</button>
46
- </div>
47
- </div>
48
- """
49
- return html_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import bcrypt
2
+ import gradio as gr
3
  from simple_salesforce import Salesforce
 
4
  # Salesforce Connection
5
  sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
6
 
7
+
8
+ # Function to Hash Password
9
+ def hash_password(password):
10
+ return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
11
+
12
+ # Function to Verify 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 function with email validation
17
+ def signup(name, email, phone, password):
18
  try:
19
+ email = email.strip()
20
+
21
+ # Check if the email already exists in Salesforce
22
+ query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'"
23
  result = sf.query(query)
24
+
25
+ if len(result['records']) > 0:
26
+ # Email already exists
27
+ return "Email already exists! Please use a different email."
28
+
29
+ # Hash the password
30
+ hashed_password = hash_password(password)
31
+
32
+ # Create the new user record
33
+ sf.Customer_Login__c.create({
34
+ 'Name': name.strip(),
35
+ 'Email__c': email,
36
+ 'Phone_Number__c': phone.strip(),
37
+ 'Password__c': hashed_password # Use Password3__c field for hashed passwords
38
+ })
39
+ return "Signup successful! You can now login."
40
  except Exception as e:
41
+ return f"Error during signup: {str(e)}"
42
+
43
+ # Login function
44
+ def login(email, password):
45
+ try:
46
+ email = email.strip()
47
+ password = password.strip()
48
+
49
+ # Query Salesforce for user details
50
+ query = f"SELECT Name, Email__c, Phone_Number__c, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
51
+ result = sf.query(query)
52
+
53
+ if len(result['records']) == 0:
54
+ return "Invalid email or password.", None, None, None
55
+
56
+ user = result['records'][0]
57
+ stored_password = user['Password__c']
58
+
59
+ print(f"Entered Email: {email}")
60
+ print(f"Entered Password: {password}")
61
+ print(f"Stored Hashed Password: {stored_password}")
62
+
63
+ # Verify the entered password with the stored hash
64
+ if verify_password(password, stored_password):
65
+ print("Password matched!")
66
+ return "Login successful!", user['Name'], user['Email__c'], user['Phone_Number__c']
67
  else:
68
+ print("Password did not match!")
69
+ return "Invalid email or password.", None, None, None
70
+ except Exception as e:
71
+ print(f"Error during login: {str(e)}")
72
+ return f"Error during login: {str(e)}", None, None, None
73
+
74
+ # Gradio Signup Page
75
+ def signup_page(name, email, phone, password):
76
+ response = signup(name, email, phone, password)
77
+ return response
78
+
79
+ signup_interface = gr.Interface(
80
+ fn=signup_page,
81
+ inputs=[
82
+ gr.Textbox(label="Name"),
83
+ gr.Textbox(label="Email"),
84
+ gr.Textbox(label="Phone"),
85
+ gr.Textbox(label="Password", type="password"),
86
+ ],
87
+ outputs=gr.Textbox(label="Signup Status"),
88
+ title="Signup Page"
89
+ )
90
+
91
+ # Gradio Login Page
92
+ def login_page(email, password):
93
+ login_status, name, email, phone = login(email, password)
94
+ if login_status == "Login successful!":
95
+ return login_status, name, email, phone
96
+ else:
97
+ return login_status, "Error", "Error", "Error"
98
+
99
+ login_interface = gr.Interface(
100
+ fn=login_page,
101
+ inputs=[
102
+ gr.Textbox(label="Email"),
103
+ gr.Textbox(label="Password", type="password"),
104
+ ],
105
+ outputs=[
106
+ gr.Textbox(label="Login Status"),
107
+ gr.Textbox(label="Name"),
108
+ gr.Textbox(label="Email"),
109
+ gr.Textbox(label="Phone"),
110
+ ],
111
+ title="Login Page"
112
+ )
113
+
114
+ # Combine Pages in Gradio
115
+ with gr.Blocks() as app:
116
+ with gr.Tab("Signup"):
117
+ signup_interface.render()
118
+
119
+ with gr.Tab("Login"):
120
+ login_interface.render()
121
+
122
+ # Launch Gradio App
123
+ app.launch()