import bcrypt import gradio as gr from simple_salesforce import Salesforce # Salesforce Connection sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') # Function to Hash Password def hash_password(password): return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') # Function to Verify Password def verify_password(plain_password, hashed_password): return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8')) # Signup function def signup(name, email, phone, password): try: email = email.strip() # Check if the email already exists in Salesforce query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'" result = sf.query(query) if len(result['records']) > 0: return "Email already exists! Please use a different email." # Hash the password hashed_password = hash_password(password) # Create the new user record sf.Customer_Login__c.create({ 'Name': name.strip(), 'Email__c': email, 'Phone_Number__c': phone.strip(), 'Password__c': hashed_password }) return "Signup successful! You can now login." except Exception as e: return f"Error during signup: {str(e)}" # Login function def login(email, password): try: email = email.strip() password = password.strip() # Debug: Print entered email and password print(f"Entered Email: {email}") print(f"Entered Password: {password}") # Query Salesforce for user details query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'" result = sf.query(query) # Debug: Print query results print(f"Query Result: {result}") if len(result['records']) == 0: print("No matching user found.") return "Invalid email or password.", None user = result['records'][0] stored_password = user['Password__c'] # Debug: Print stored hashed password print(f"Stored Hashed Password: {stored_password}") # Verify the entered password with the stored hash if verify_password(password, stored_password): print("Password matched!") return "Login successful!", user['Name'] else: print("Password did not match!") return "Invalid email or password.", None except Exception as e: print(f"Error during login: {e}") return f"Error during login: {str(e)}", None # Dummy function to load menu data def load_menu_from_salesforce(): try: query = "SELECT Name, Price__c, Description__c, Image1__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c" result = sf.query(query) # Debugging: Print fetched records print("Fetched Records from Salesforce:") for record in result['records']: print(record) return result['records'] except Exception as e: print(f"Error fetching menu data: {e}") return [] # Function to filter menu items based on preference def filter_menu(preferences): menu_data = load_menu_from_salesforce() # Debugging: Print the fetched menu data print("Menu Data Fetched:") for item in menu_data: print(item) # Check if data exists if not menu_data: return "

No menu data available. Please try again later.

" filtered_data = {} for item in menu_data: # Ensure the required fields exist if "Section__c" not in item or "Veg_NonVeg__c" not in item: continue # Add the section to filtered data if not already present if item["Section__c"] not in filtered_data: filtered_data[item["Section__c"]] = [] # Apply filters based on preferences if "All" in preferences: filtered_data[item["Section__c"]].append(item) elif "Veg" in preferences and item["Veg_NonVeg__c"] in ["Veg", "Both"]: filtered_data[item["Section__c"]].append(item) elif "Non-Veg" in preferences and item["Veg_NonVeg__c"] in ["Non-Veg", "Both"]: filtered_data[item["Section__c"]].append(item) # Generate HTML content html_content = '
' for section, items in filtered_data.items(): if items: html_content += f"

{section}

" html_content += '
' for item in items: html_content += f"""

{item['Name']}

${item['Price__c']}

{item['Description__c']}

""" html_content += '
' html_content += '
' if not any(filtered_data.values()): return "

No items match your filter.

" return html_content # Gradio App with gr.Blocks() as app: # Header with gr.Row(): gr.HTML("

Welcome to Biryani Hub

") # Login Page with gr.Row(visible=True) as login_page: with gr.Column(): login_email = gr.Textbox(label="Email") login_password = gr.Textbox(label="Password", type="password") login_button = gr.Button("Login") signup_button = gr.Button("Go to Signup") login_output = gr.Textbox(label="Status") # Signup Page with gr.Row(visible=False) as signup_page: with gr.Column(): signup_name = gr.Textbox(label="Name") signup_email = gr.Textbox(label="Email") signup_phone = gr.Textbox(label="Phone") signup_password = gr.Textbox(label="Password", type="password") submit_signup = gr.Button("Signup") login_redirect = gr.Button("Go to Login") signup_output = gr.Textbox(label="Status") # Menu Page with gr.Row(visible=False) as menu_page: preferences = gr.CheckboxGroup( choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value=["All"] ) menu_output = gr.HTML() # Footer with gr.Row(visible=False) as footer: gr.HTML("") # Login Button Logic def handle_login(email, password): status, user = login(email, password) if status == "Login successful!": return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), status else: return gr.update(), gr.update(), gr.update(), status # Signup Button Logic def handle_signup(name, email, phone, password): return signup(name, email, phone, password) # Menu Logic def handle_menu(preferences): content = filter_menu(preferences) return content login_button.click( handle_login, [login_email, login_password], [login_page, menu_page, footer, login_output] ) signup_button.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [login_page, signup_page]) submit_signup.click(handle_signup, [signup_name, signup_email, signup_phone, signup_password], signup_output) preferences.change(handle_menu, [preferences], menu_output) app.launch()