nagasurendra commited on
Commit
012d61e
·
verified ·
1 Parent(s): 3e01568

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -50
app.py CHANGED
@@ -47,68 +47,66 @@ def login(email, password):
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
55
 
56
  user = result['records'][0]
57
- stored_password = user['Password__c']
58
-
59
- # Verify the entered password with the stored hash
60
- if verify_password(password, stored_password):
61
  return "Login successful!", user['Name']
62
  else:
63
  return "Invalid email or password.", None
64
  except Exception as e:
65
  return f"Error during login: {str(e)}", None
66
 
67
- # Fetch menu items from Salesforce
68
- def fetch_menu():
69
  try:
70
- query = "SELECT Name, Price__c, Description__c, Image1__c, Veg_NonVeg__c FROM Menu_Item__c"
71
  result = sf.query(query)
72
- return [
73
- {
74
- "Name": item["Name"],
75
- "Price": item["Price__c"],
76
- "Description": item["Description__c"],
77
- "Image": item["Image1__c"],
78
- "Category": item["Veg_NonVeg__c"],
79
- }
80
- for item in result["records"]
81
- ]
82
  except Exception as e:
83
- return f"Error fetching menu: {e}"
84
-
85
- # Render the menu as HTML
86
- def render_menu(veg, non_veg):
87
- menu_items = fetch_menu()
88
- if not isinstance(menu_items, list):
89
- return menu_items # Return error if fetching menu fails
90
-
91
- filtered_items = [
92
- item
93
- for item in menu_items
94
- if (veg and item["Category"] in ["Veg", "Both"]) or
95
- (non_veg and item["Category"] in ["Non-Veg", "Both"])
96
- ]
 
 
 
 
 
97
 
98
  html_content = ""
99
- for item in filtered_items:
100
  html_content += f"""
101
- <div style="border: 1px solid #ddd; padding: 10px; margin: 10px; border-radius: 5px; box-shadow: 2px 2px 8px #aaa;">
102
- <h3>{item['Name']} - ${item['Price']}</h3>
103
- <p>{item['Description']}</p>
104
- <img src="{item['Image']}" alt="{item['Name']}" style="width: 100px; height: 100px; object-fit: cover;">
 
 
 
 
 
 
105
  </div>
106
  """
107
- return html_content or "No items match your filter."
108
 
109
  # Gradio App
110
  with gr.Blocks() as app:
111
- # States for visibility
112
  login_status = gr.State(value=False)
113
 
114
  # Login Page
@@ -117,8 +115,27 @@ with gr.Blocks() as app:
117
  login_email = gr.Textbox(label="Email")
118
  login_password = gr.Textbox(label="Password", type="password")
119
  login_button = gr.Button("Login")
 
120
  login_output = gr.Textbox(label="Status")
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  def handle_login(email, password):
123
  status, user = login(email, password)
124
  if status == "Login successful!":
@@ -126,18 +143,17 @@ with gr.Blocks() as app:
126
  else:
127
  return gr.update(), gr.update(), status
128
 
129
- login_button.click(handle_login, [login_email, login_password], [login_page, gr.Row(visible=True), login_output])
130
-
131
- # Menu Page
132
- with gr.Row(visible=False) as menu_page:
133
- veg_filter = gr.Checkbox(label="Veg", value=True)
134
- non_veg_filter = gr.Checkbox(label="Non-Veg", value=True)
135
- filter_button = gr.Button("Filter Menu")
136
- menu_display = gr.HTML(label="Menu")
137
 
138
- def filter_menu(veg, non_veg):
139
- return render_menu(veg, non_veg)
140
 
141
- filter_button.click(filter_menu, [veg_filter, non_veg_filter], menu_display)
 
 
 
 
 
142
 
143
  app.launch()
 
47
  password = password.strip()
48
 
49
  # Query Salesforce for user details
50
+ query = f"SELECT Name, 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
55
 
56
  user = result['records'][0]
57
+ if verify_password(password, user['Password__c']):
 
 
 
58
  return "Login successful!", user['Name']
59
  else:
60
  return "Invalid email or password.", None
61
  except Exception as e:
62
  return f"Error during login: {str(e)}", None
63
 
64
+ # Function to load menu items from Salesforce
65
+ def load_menu_from_salesforce():
66
  try:
67
+ query = "SELECT Name, Price__c, Description__c, Image1__c, Ingredients__c FROM Menu_Item__c"
68
  result = sf.query(query)
69
+ return result['records']
 
 
 
 
 
 
 
 
 
70
  except Exception as e:
71
+ raise ValueError(f"Error loading menu data from Salesforce: {e}")
72
+
73
+ # Function to filter menu items based on preference
74
+ def filter_menu_from_salesforce(preference):
75
+ menu_data = load_menu_from_salesforce()
76
+ filtered_data = []
77
+
78
+ for item in menu_data:
79
+ if preference == "Halal/Non-Veg":
80
+ if any(x in item.get("Ingredients__c", "").lower() for x in ["chicken", "mutton", "fish", "prawns", "goat"]):
81
+ filtered_data.append(item)
82
+ elif preference == "Vegetarian":
83
+ if not any(x in item.get("Ingredients__c", "").lower() for x in ["chicken", "mutton", "fish", "prawns", "goat"]):
84
+ filtered_data.append(item)
85
+ elif preference == "Guilt-Free":
86
+ if "fat:" in item.get("Description__c", "").lower():
87
+ filtered_data.append(item)
88
+ else:
89
+ filtered_data = menu_data
90
 
91
  html_content = ""
92
+ for item in filtered_data:
93
  html_content += f"""
94
+ <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);">
95
+ <div style="flex: 1; margin-right: 15px;">
96
+ <h3 style="margin: 0; font-size: 18px;">{item['Name']}</h3>
97
+ <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price__c']}</p>
98
+ <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description__c']}</p>
99
+ </div>
100
+ <div style="flex-shrink: 0; text-align: center;">
101
+ <img src="{item['Image1__c']}" alt="{item['Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
102
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
103
+ </div>
104
  </div>
105
  """
106
+ return html_content
107
 
108
  # Gradio App
109
  with gr.Blocks() as app:
 
110
  login_status = gr.State(value=False)
111
 
112
  # Login Page
 
115
  login_email = gr.Textbox(label="Email")
116
  login_password = gr.Textbox(label="Password", type="password")
117
  login_button = gr.Button("Login")
118
+ signup_button = gr.Button("Go to Signup")
119
  login_output = gr.Textbox(label="Status")
120
 
121
+ # Signup Page
122
+ with gr.Row(visible=False) as signup_page:
123
+ with gr.Column():
124
+ signup_name = gr.Textbox(label="Name")
125
+ signup_email = gr.Textbox(label="Email")
126
+ signup_phone = gr.Textbox(label="Phone")
127
+ signup_password = gr.Textbox(label="Password", type="password")
128
+ submit_signup = gr.Button("Signup")
129
+ login_redirect = gr.Button("Go to Login")
130
+ signup_output = gr.Textbox(label="Status")
131
+
132
+ # Menu Page
133
+ with gr.Row(visible=False) as menu_page:
134
+ preference = gr.Radio(["All", "Halal/Non-Veg", "Vegetarian", "Guilt-Free"], label="Filter Preference")
135
+ show_menu = gr.Button("Show Menu")
136
+ menu_output = gr.HTML()
137
+
138
+ # Functions for page transitions and operations
139
  def handle_login(email, password):
140
  status, user = login(email, password)
141
  if status == "Login successful!":
 
143
  else:
144
  return gr.update(), gr.update(), status
145
 
146
+ def handle_signup(name, email, phone, password):
147
+ return signup(name, email, phone, password)
 
 
 
 
 
 
148
 
149
+ def handle_menu(preference):
150
+ return filter_menu_from_salesforce(preference)
151
 
152
+ # Button Actions
153
+ login_button.click(handle_login, [login_email, login_password], [login_page, menu_page, login_output])
154
+ signup_button.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [login_page, signup_page])
155
+ submit_signup.click(handle_signup, [signup_name, signup_email, signup_phone, signup_password], signup_output)
156
+ login_redirect.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [signup_page, login_page])
157
+ show_menu.click(handle_menu, [preference], menu_output)
158
 
159
  app.launch()