nagasurendra commited on
Commit
641a1a6
·
verified ·
1 Parent(s): a088b71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -16
app.py CHANGED
@@ -1,29 +1,123 @@
1
  import bcrypt
2
  import gradio as gr
 
3
 
4
- # Dummy data for testing (replace Salesforce queries temporarily)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def load_menu_from_salesforce():
6
- # Replace with a simple dummy dataset to test UI functionality
7
- return [
8
- {"Name": "Chicken Biryani", "Price__c": "8.99", "Description__c": "Delicious chicken biryani",
9
- "Image1__c": "https://via.placeholder.com/200", "Veg_NonVeg__c": "Non-Veg", "Section__c": "Biryanis"},
10
- {"Name": "Veg Biryani", "Price__c": "6.99", "Description__c": "Healthy veg biryani",
11
- "Image1__c": "https://via.placeholder.com/200", "Veg_NonVeg__c": "Veg", "Section__c": "Biryanis"},
12
- {"Name": "Paneer Tikka", "Price__c": "5.99", "Description__c": "Grilled paneer tikka",
13
- "Image1__c": "https://via.placeholder.com/200", "Veg_NonVeg__c": "Veg", "Section__c": "Starters"},
14
- {"Name": "Chicken Wings", "Price__c": "7.99", "Description__c": "Spicy chicken wings",
15
- "Image1__c": "https://via.placeholder.com/200", "Veg_NonVeg__c": "Non-Veg", "Section__c": "Starters"}
16
- ]
 
 
17
 
18
  # Function to filter menu items based on preference
19
  def filter_menu(preferences):
20
  menu_data = load_menu_from_salesforce()
21
 
 
 
 
 
 
 
 
 
 
22
  filtered_data = {}
 
23
  for item in menu_data:
 
 
 
 
 
24
  if item["Section__c"] not in filtered_data:
25
  filtered_data[item["Section__c"]] = []
26
 
 
27
  if "All" in preferences:
28
  filtered_data[item["Section__c"]].append(item)
29
  elif "Veg" in preferences and item["Veg_NonVeg__c"] in ["Veg", "Both"]:
@@ -31,6 +125,7 @@ def filter_menu(preferences):
31
  elif "Non-Veg" in preferences and item["Veg_NonVeg__c"] in ["Non-Veg", "Both"]:
32
  filtered_data[item["Section__c"]].append(item)
33
 
 
34
  html_content = '<div style="padding: 20px; max-width: 1200px; margin: auto;">'
35
  for section, items in filtered_data.items():
36
  if items:
@@ -39,7 +134,7 @@ def filter_menu(preferences):
39
  for item in items:
40
  html_content += f"""
41
  <div style="width: 300px; border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
42
- <img src="{item['Image1__c']}" style="width: 100%; height: 200px; object-fit: cover;">
43
  <div style="padding: 10px;">
44
  <h3>{item['Name']}</h3>
45
  <p>${item['Price__c']}</p>
@@ -67,8 +162,20 @@ with gr.Blocks() as app:
67
  login_email = gr.Textbox(label="Email")
68
  login_password = gr.Textbox(label="Password", type="password")
69
  login_button = gr.Button("Login")
 
70
  login_output = gr.Textbox(label="Status")
71
 
 
 
 
 
 
 
 
 
 
 
 
72
  # Menu Page
73
  with gr.Row(visible=False) as menu_page:
74
  preferences = gr.CheckboxGroup(
@@ -82,10 +189,15 @@ with gr.Blocks() as app:
82
 
83
  # Login Button Logic
84
  def handle_login(email, password):
85
- if email == "[email protected]" and password == "password":
86
- return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), "Login successful!"
 
87
  else:
88
- return gr.update(), gr.update(), gr.update(), "Invalid login credentials."
 
 
 
 
89
 
90
  # Menu Logic
91
  def handle_menu(preferences):
@@ -95,6 +207,8 @@ with gr.Blocks() as app:
95
  login_button.click(
96
  handle_login, [login_email, login_password], [login_page, menu_page, footer, login_output]
97
  )
 
 
98
  preferences.change(handle_menu, [preferences], menu_output)
99
 
100
  app.launch()
 
1
  import bcrypt
2
  import gradio as gr
3
+ from simple_salesforce import Salesforce
4
 
5
+ # Salesforce Connection
6
+ sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
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
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
+ return "Email already exists! Please use a different email."
27
+
28
+ # Hash the password
29
+ hashed_password = hash_password(password)
30
+
31
+ # Create the new user record
32
+ sf.Customer_Login__c.create({
33
+ 'Name': name.strip(),
34
+ 'Email__c': email,
35
+ 'Phone_Number__c': phone.strip(),
36
+ 'Password__c': hashed_password
37
+ })
38
+ return "Signup successful! You can now login."
39
+ except Exception as e:
40
+ return f"Error during signup: {str(e)}"
41
+
42
+ # Login function
43
+ def login(email, password):
44
+ try:
45
+ email = email.strip()
46
+ password = password.strip()
47
+
48
+ # Debug: Print entered email and password
49
+ print(f"Entered Email: {email}")
50
+ print(f"Entered Password: {password}")
51
+
52
+ # Query Salesforce for user details
53
+ query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
54
+ result = sf.query(query)
55
+
56
+ # Debug: Print query results
57
+ print(f"Query Result: {result}")
58
+
59
+ if len(result['records']) == 0:
60
+ print("No matching user found.")
61
+ return "Invalid email or password.", None
62
+
63
+ user = result['records'][0]
64
+ stored_password = user['Password__c']
65
+
66
+ # Debug: Print stored hashed password
67
+ print(f"Stored Hashed Password: {stored_password}")
68
+
69
+ # Verify the entered password with the stored hash
70
+ if verify_password(password, stored_password):
71
+ print("Password matched!")
72
+ return "Login successful!", user['Name']
73
+ else:
74
+ print("Password did not match!")
75
+ return "Invalid email or password.", None
76
+ except Exception as e:
77
+ print(f"Error during login: {e}")
78
+ return f"Error during login: {str(e)}", None
79
+
80
+ # Dummy function to load menu data
81
  def load_menu_from_salesforce():
82
+ try:
83
+ query = "SELECT Name, Price__c, Description__c, Image1__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
84
+ result = sf.query(query)
85
+
86
+ # Debugging: Print fetched records
87
+ print("Fetched Records from Salesforce:")
88
+ for record in result['records']:
89
+ print(record)
90
+
91
+ return result['records']
92
+ except Exception as e:
93
+ print(f"Error fetching menu data: {e}")
94
+ return []
95
 
96
  # Function to filter menu items based on preference
97
  def filter_menu(preferences):
98
  menu_data = load_menu_from_salesforce()
99
 
100
+ # Debugging: Print the fetched menu data
101
+ print("Menu Data Fetched:")
102
+ for item in menu_data:
103
+ print(item)
104
+
105
+ # Check if data exists
106
+ if not menu_data:
107
+ return "<p style='text-align: center;'>No menu data available. Please try again later.</p>"
108
+
109
  filtered_data = {}
110
+
111
  for item in menu_data:
112
+ # Ensure the required fields exist
113
+ if "Section__c" not in item or "Veg_NonVeg__c" not in item:
114
+ continue
115
+
116
+ # Add the section to filtered data if not already present
117
  if item["Section__c"] not in filtered_data:
118
  filtered_data[item["Section__c"]] = []
119
 
120
+ # Apply filters based on preferences
121
  if "All" in preferences:
122
  filtered_data[item["Section__c"]].append(item)
123
  elif "Veg" in preferences and item["Veg_NonVeg__c"] in ["Veg", "Both"]:
 
125
  elif "Non-Veg" in preferences and item["Veg_NonVeg__c"] in ["Non-Veg", "Both"]:
126
  filtered_data[item["Section__c"]].append(item)
127
 
128
+ # Generate HTML content
129
  html_content = '<div style="padding: 20px; max-width: 1200px; margin: auto;">'
130
  for section, items in filtered_data.items():
131
  if items:
 
134
  for item in items:
135
  html_content += f"""
136
  <div style="width: 300px; border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
137
+ <img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;">
138
  <div style="padding: 10px;">
139
  <h3>{item['Name']}</h3>
140
  <p>${item['Price__c']}</p>
 
162
  login_email = gr.Textbox(label="Email")
163
  login_password = gr.Textbox(label="Password", type="password")
164
  login_button = gr.Button("Login")
165
+ signup_button = gr.Button("Go to Signup")
166
  login_output = gr.Textbox(label="Status")
167
 
168
+ # Signup Page
169
+ with gr.Row(visible=False) as signup_page:
170
+ with gr.Column():
171
+ signup_name = gr.Textbox(label="Name")
172
+ signup_email = gr.Textbox(label="Email")
173
+ signup_phone = gr.Textbox(label="Phone")
174
+ signup_password = gr.Textbox(label="Password", type="password")
175
+ submit_signup = gr.Button("Signup")
176
+ login_redirect = gr.Button("Go to Login")
177
+ signup_output = gr.Textbox(label="Status")
178
+
179
  # Menu Page
180
  with gr.Row(visible=False) as menu_page:
181
  preferences = gr.CheckboxGroup(
 
189
 
190
  # Login Button Logic
191
  def handle_login(email, password):
192
+ status, user = login(email, password)
193
+ if status == "Login successful!":
194
+ return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), status
195
  else:
196
+ return gr.update(), gr.update(), gr.update(), status
197
+
198
+ # Signup Button Logic
199
+ def handle_signup(name, email, phone, password):
200
+ return signup(name, email, phone, password)
201
 
202
  # Menu Logic
203
  def handle_menu(preferences):
 
207
  login_button.click(
208
  handle_login, [login_email, login_password], [login_page, menu_page, footer, login_output]
209
  )
210
+ signup_button.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [login_page, signup_page])
211
+ submit_signup.click(handle_signup, [signup_name, signup_email, signup_phone, signup_password], signup_output)
212
  preferences.change(handle_menu, [preferences], menu_output)
213
 
214
  app.launch()