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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import bcrypt
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from simple_salesforce import Salesforce
|
| 4 |
+
|
| 5 |
+
# Salesforce Connection
|
| 6 |
+
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 7 |
+
|
| 8 |
+
# Hash Password
|
| 9 |
+
def hash_password(password):
|
| 10 |
+
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 11 |
+
|
| 12 |
+
# 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 |
+
query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email.strip()}'"
|
| 20 |
+
result = sf.query(query)
|
| 21 |
+
if len(result['records']) > 0:
|
| 22 |
+
return "Email already exists! Please use a different email."
|
| 23 |
+
|
| 24 |
+
hashed_password = hash_password(password)
|
| 25 |
+
sf.Customer_Login__c.create({
|
| 26 |
+
'Name': name.strip(),
|
| 27 |
+
'Email__c': email.strip(),
|
| 28 |
+
'Phone_Number__c': phone.strip(),
|
| 29 |
+
'Password__c': hashed_password
|
| 30 |
+
})
|
| 31 |
+
return "Signup successful! You can now login."
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error during signup: {str(e)}"
|
| 34 |
+
|
| 35 |
+
# Login Function
|
| 36 |
+
def login(email, password):
|
| 37 |
+
try:
|
| 38 |
+
query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email.strip()}'"
|
| 39 |
+
result = sf.query(query)
|
| 40 |
+
if len(result['records']) == 0:
|
| 41 |
+
return "Invalid email or password.", None
|
| 42 |
+
|
| 43 |
+
user = result['records'][0]
|
| 44 |
+
if verify_password(password.strip(), user['Password__c']):
|
| 45 |
+
return "Login successful!", user['Name']
|
| 46 |
+
else:
|
| 47 |
+
return "Invalid email or password.", None
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error during login: {str(e)}", None
|
| 50 |
+
|
| 51 |
+
# Load Menu Data from Salesforce
|
| 52 |
+
def load_menu_from_salesforce():
|
| 53 |
+
try:
|
| 54 |
+
query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
|
| 55 |
+
result = sf.query(query)
|
| 56 |
+
return result['records']
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return []
|
| 59 |
+
|
| 60 |
+
# Filter Menu Items
|
| 61 |
+
def filter_menu(preference):
|
| 62 |
+
menu_data = load_menu_from_salesforce()
|
| 63 |
+
filtered_data = {}
|
| 64 |
+
for item in menu_data:
|
| 65 |
+
if "Section__c" not in item or "Veg_NonVeg__c" not in item:
|
| 66 |
+
continue
|
| 67 |
+
if item["Section__c"] not in filtered_data:
|
| 68 |
+
filtered_data[item["Section__c"]] = []
|
| 69 |
+
if preference == "All" or (preference == "Veg" and item["Veg_NonVeg__c"] in ["Veg", "Both"]) or (preference == "Non-Veg" and item["Veg_NonVeg__c"] in ["Non veg", "Both"]):
|
| 70 |
+
filtered_data[item["Section__c"].strip()].append(item)
|
| 71 |
+
return filtered_data
|
| 72 |
+
|
| 73 |
+
# Render Menu as HTML
|
| 74 |
+
def render_menu_html(filtered_data):
|
| 75 |
+
html_content = '<div style="padding: 0 10px; max-width: 1200px; margin: auto;">'
|
| 76 |
+
for section, items in filtered_data.items():
|
| 77 |
+
html_content += f"<h2 style='text-align: center; margin-top: 5px;'>{section}</h2>"
|
| 78 |
+
html_content += '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px; justify-content: center; margin-top: 10px;">'
|
| 79 |
+
for item in items:
|
| 80 |
+
html_content += f"""
|
| 81 |
+
<div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;">
|
| 82 |
+
<img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover; cursor: pointer;" onclick="openModal('{item.get('Image2__c', '')}', '{item['Name']}', '{item['Description__c']}', '{item['Price__c']}')">
|
| 83 |
+
<div style="padding: 10px;">
|
| 84 |
+
<h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3>
|
| 85 |
+
<p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p>
|
| 86 |
+
<p style='font-size: 0.9em; text-align: justify; margin: 5px;'>{item['Description__c']}</p>
|
| 87 |
+
</div>
|
| 88 |
+
</div>
|
| 89 |
+
"""
|
| 90 |
+
html_content += '</div>'
|
| 91 |
+
html_content += '</div>'
|
| 92 |
+
|
| 93 |
+
if not any(filtered_data.values()):
|
| 94 |
+
return "<p>No items match your filter.</p>"
|
| 95 |
+
|
| 96 |
+
return html_content
|
| 97 |
+
|
| 98 |
+
# JavaScript Functions for Modal
|
| 99 |
+
modal_script = """
|
| 100 |
+
<script>
|
| 101 |
+
function openModal(image, name, description, price) {
|
| 102 |
+
document.getElementById('modal').style.display = 'block';
|
| 103 |
+
document.getElementById('modal-image').src = image;
|
| 104 |
+
document.getElementById('modal-name').innerText = name;
|
| 105 |
+
document.getElementById('modal-description').innerText = description;
|
| 106 |
+
document.getElementById('modal-price').innerText = `$${price}`;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
function closeModal() {
|
| 110 |
+
document.getElementById('modal').style.display = 'none';
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
function addToCart() {
|
| 114 |
+
const quantity = document.getElementById('modal-quantity').value;
|
| 115 |
+
const instructions = document.getElementById('modal-instructions').value;
|
| 116 |
+
alert(`Item added to cart with quantity: ${quantity}\nSpecial instructions: ${instructions}`);
|
| 117 |
+
closeModal();
|
| 118 |
+
}
|
| 119 |
+
</script>
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
# Gradio App
|
| 123 |
+
with gr.Blocks() as app:
|
| 124 |
+
with gr.Row():
|
| 125 |
+
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
| 126 |
+
|
| 127 |
+
with gr.Row(visible=True) as login_page:
|
| 128 |
+
with gr.Column():
|
| 129 |
+
login_email = gr.Textbox(label="Email")
|
| 130 |
+
login_password = gr.Textbox(label="Password", type="password")
|
| 131 |
+
login_button = gr.Button("Login")
|
| 132 |
+
signup_button = gr.Button("Go to Signup")
|
| 133 |
+
login_output = gr.Textbox(label="Status")
|
| 134 |
+
|
| 135 |
+
with gr.Row(visible=False) as signup_page:
|
| 136 |
+
with gr.Column():
|
| 137 |
+
signup_name = gr.Textbox(label="Name")
|
| 138 |
+
signup_email = gr.Textbox(label="Email")
|
| 139 |
+
signup_phone = gr.Textbox(label="Phone")
|
| 140 |
+
signup_password = gr.Textbox(label="Password", type="password")
|
| 141 |
+
submit_signup = gr.Button("Signup")
|
| 142 |
+
login_redirect = gr.Button("Go to Login")
|
| 143 |
+
signup_output = gr.Textbox(label="Status")
|
| 144 |
+
|
| 145 |
+
with gr.Row(visible=False) as menu_page:
|
| 146 |
+
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
| 147 |
+
menu_output = gr.HTML()
|
| 148 |
+
modal_window = gr.HTML(f"""
|
| 149 |
+
<div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000; max-width: 30%; max-height: 80%; overflow: auto;">
|
| 150 |
+
<div style="text-align: right;">
|
| 151 |
+
<button onclick="closeModal()" style="background: none; border: none; font-size: 20px; cursor: pointer;">×</button>
|
| 152 |
+
</div>
|
| 153 |
+
<img id="modal-image" style="width: 100%; height: 200px; object-fit: cover; margin-bottom: 10px;">
|
| 154 |
+
<h2 id="modal-name"></h2>
|
| 155 |
+
<p id="modal-description"></p>
|
| 156 |
+
<p id="modal-price"></p>
|
| 157 |
+
<label>Quantity: <input type="number" id="modal-quantity" value="1" min="1" style="width: 50px;"></label>
|
| 158 |
+
<textarea id="modal-instructions" placeholder="Add special instructions" style="width: 100%; height: 50px; margin-top: 10px;"></textarea>
|
| 159 |
+
<button onclick="addToCart()" style="margin-top: 10px; padding: 10px; background-color: green; color: white; border: none; border-radius: 5px;">Add to Cart</button>
|
| 160 |
+
</div>
|
| 161 |
+
""")
|
| 162 |
+
gr.HTML(modal_script)
|
| 163 |
+
|
| 164 |
+
def handle_login(email, password):
|
| 165 |
+
status, user = login(email, password)
|
| 166 |
+
if status == "Login successful!":
|
| 167 |
+
content = render_menu_html(filter_menu("All"))
|
| 168 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(value=content), status
|
| 169 |
+
else:
|
| 170 |
+
return gr.update(), gr.update(), gr.update(), status
|
| 171 |
+
|
| 172 |
+
def handle_signup(name, email, phone, password):
|
| 173 |
+
return signup(name, email, phone, password)
|
| 174 |
+
|
| 175 |
+
def handle_menu(preference):
|
| 176 |
+
content = render_menu_html(filter_menu(preference))
|
| 177 |
+
return content
|
| 178 |
+
|
| 179 |
+
login_button.click(handle_login, [login_email, login_password], [login_page, menu_page, menu_output, login_output])
|
| 180 |
+
signup_button.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [login_page, signup_page])
|
| 181 |
+
submit_signup.click(handle_signup, [signup_name, signup_email, signup_phone, signup_password], signup_output)
|
| 182 |
+
preference.change(handle_menu, [preference], menu_output)
|
| 183 |
+
|
| 184 |
+
app.launch()
|