Spaces:
Sleeping
Sleeping
File size: 13,295 Bytes
4ee4ce0 54f831b 641a1a6 91f1be6 641a1a6 b924a0f 7adc402 b924a0f 7adc402 b924a0f 7adc402 b924a0f 641a1a6 b924a0f 641a1a6 b924a0f 641a1a6 b924a0f 641a1a6 b924a0f 641a1a6 b924a0f 641a1a6 b924a0f 641a1a6 b924a0f 7e7b87e 641a1a6 b924a0f 32f38e3 b924a0f 32f38e3 b924a0f 32f38e3 b924a0f 32f38e3 20c55b6 b924a0f e11a47c 012d61e 6cf5b04 e767e0f 012d61e 641a1a6 e767e0f e11a47c a57b5d2 e767e0f f94a797 e767e0f f6ba815 e7a806e 93a5360 e7a806e e767e0f e7a806e 2691732 f9601e7 45a308e a088b71 45a308e 3e01568 a2874a3 6cf5b04 a2874a3 6cf5b04 a2874a3 20c55b6 a2874a3 6cf5b04 93a5360 7e7b87e d299730 9532e1d 6cf5b04 93a5360 9532e1d 93a5360 a2874a3 93a5360 a2874a3 93a5360 a2874a3 9532e1d a2874a3 6cf5b04 a2874a3 6cf5b04 a2874a3 6cf5b04 a2874a3 6cf5b04 a2874a3 b4f3a62 93a5360 3e01568 54f831b e767e0f a088b71 e767e0f cc5a2e7 2691732 6cf5b04 2691732 91f1be6 32f38e3 cc5a2e7 6cf5b04 cc5a2e7 a2874a3 20c55b6 8117a65 2691732 6cf5b04 59cd355 6cf5b04 59cd355 a2874a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
import bcrypt
import gradio as gr
from simple_salesforce import Salesforce
# Salesforce Connection
sf = Salesforce(username='[email protected]', 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()
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."
hashed_password = hash_password(password)
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()
query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
result = sf.query(query)
if len(result['records']) == 0:
return "Invalid email or password.", None
user = result['records'][0]
stored_password = user['Password__c']
if verify_password(password.strip(), stored_password):
return "Login successful!", user['Name']
else:
return "Invalid email or password.", None
except Exception as e:
return f"Error during login: {str(e)}", None
# Function to load menu data
def load_menu_from_salesforce():
try:
query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
result = sf.query(query)
return result['records']
except Exception as e:
return []
# Function to load add-ons data
def load_add_ons_from_salesforce():
try:
query = "SELECT Name, Price__c FROM Add_Ons__c"
result = sf.query(query)
return result['records']
except Exception as e:
return []
# Function to filter menu items
def filter_menu(preference):
menu_data = load_menu_from_salesforce()
filtered_data = {}
for item in menu_data:
if "Section__c" not in item or "Veg_NonVeg__c" not in item:
continue
if item["Section__c"] not in filtered_data:
filtered_data[item["Section__c"]] = []
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"]):
filtered_data[item["Section__c"].strip()].append(item)
html_content = '<div style="padding: 0 10px; max-width: 1200px; margin: auto;">'
for section, items in filtered_data.items():
html_content += f"<h2 style='text-align: center; margin-top: 5px;'>{section}</h2>"
html_content += '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px; justify-content: center; margin-top: 10px;">'
for item in items:
html_content += f"""
<div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;">
<img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;"
onclick="openModal('{item['Name']}', '{item.get('Image2__c', '')}', '{item['Description__c']}', '${item['Price__c']}')">
<div style="padding: 10px;">
<h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3>
<p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p>
<p style='font-size: 0.9em; text-align: justify; margin: 5px;'>{item['Description__c']}</p>
</div>
</div>
"""
html_content += '</div>'
html_content += '</div>'
if not any(filtered_data.values()):
return "<p>No items match your filter.</p>"
return html_content
# Function to finalize order
def finalize_order(cart):
total_cost = sum(item['totalCost'] for item in cart)
order_details = ''
for item in cart:
order_details += f"""
<div>
<h3>{item['name']} (x{item['quantity']})</h3>
<p>Add-ons: {', '.join([extra['name'] for extra in item['extras']]) if item['extras'] else 'None'}</p>
<p>Special Instructions: {item['instructions']}</p>
<p>Cost: ${item['totalCost']}</p>
</div>
"""
order_details += f"<h2>Total Cart Cost: ${total_cost}</h2>"
return order_details
# Create Modal Window HTML
def create_modal_window():
add_ons = load_add_ons_from_salesforce()
add_ons_html = ""
for add_on in add_ons:
add_ons_html += f"""
<label>
<input type="checkbox" name="biryani-extra" value="{add_on['Name']}" data-price="{add_on['Price__c']}" />
{add_on['Name']} + ${add_on['Price__c']}
</label>
<br>
"""
modal_html = f"""
<div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
<div style="text-align: right;">
<button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">×</button>
</div>
<img id="modal-image" style="width: 100%; height: 300px; border-radius: 8px; margin-bottom: 20px;" />
<h2 id="modal-name"></h2>
<p id="modal-description"></p>
<p id="modal-price"></p>
<label for="biryani-extras"><strong>Add-ons :</strong></label>
<div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
{add_ons_html}
</div>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
<textarea id="special-instructions" placeholder="Add your special instructions here..." style="width: 100%; height: 60px;"></textarea>
<button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
</div>
"""
return modal_html
# JavaScript for Modal and Cart
def modal_js():
modal_script = """
<script>
let cart = [];
let totalCartCost = 0;
function openModal(name, image2, description, price) {
const modal = document.getElementById('modal');
modal.style.display = 'block';
modal.style.position = 'fixed';
modal.style.width = window.innerWidth <= 768 ? '90%' : '30%';
modal.style.top = `${event.clientY}px`;
modal.style.left = '50%';
modal.style.transform = 'translate(-50%, -50%)';
document.getElementById('modal-image').src = image2;
document.getElementById('modal-name').innerText = name;
document.getElementById('modal-description').innerText = description;
document.getElementById('modal-price').innerText = price;
document.getElementById('quantity').value = 1;
document.getElementById('special-instructions').value = '';
resetAddOns(); // Reset add-ons when opening the modal
}
function closeModal() {
document.getElementById('modal').style.display = 'none';
}
function resetAddOns() {
const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
}
function addToCart() {
const name = document.getElementById('modal-name').innerText;
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
const quantity = parseInt(document.getElementById('quantity').value) || 1;
const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'))
.map(checkbox => ({ name: checkbox.value, price: parseFloat(checkbox.getAttribute('data-price')) }));
const specialInstructions = document.getElementById('special-instructions').value;
const item = {
name,
quantity,
extras,
instructions: specialInstructions,
totalCost: (price + extras.reduce((acc, extra) => acc + extra.price, 0)) * quantity
};
cart.push(item);
totalCartCost += item.totalCost;
updateCart();
closeModal();
}
function updateCart() {
let cartItemsHtml = '';
cart.forEach(item => {
cartItemsHtml += `<div><h3>${item.name} (x${item.quantity})</h3>
<p>Special Instructions: ${item.instructions}</p>
<p>Add-ons: ${item.extras.map(extra => extra.name).join(', ')}</p>
<p>Cost: $${item.totalCost}</p></div>`;
});
document.getElementById('cart-items').innerHTML = cartItemsHtml;
document.getElementById('cart-total-cost').innerText = 'Total Cart Cost: $' + totalCartCost.toFixed(2);
}
function openCartModal() {
document.getElementById('cart-modal').style.display = 'block';
}
function closeCartModal() {
document.getElementById('cart-modal').style.display = 'none';
}
function proceedToCheckout() {
alert('Proceeding to Checkout!');
}
// Reset all selected add-ons when opening a new item modal
function resetAddOns() {
const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
}
</script>
"""
return modal_script
# Gradio App
with gr.Blocks() as app:
with gr.Row():
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
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")
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")
with gr.Row(visible=False) as menu_page:
with gr.Column():
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
menu_output = gr.HTML()
gr.HTML("<div id='cart-button' style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>")
gr.HTML("<div id='cart-modal' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;'><div style='padding: 20px;'><div style='text-align: right;'><button onclick='closeCartModal()' style='background: none; border: none; font-size: 24px; cursor: pointer;'>×</button></div><h1>Your Cart</h1><div id='cart-items'></div><p id='cart-total-cost' style='font-size: 1.2em; font-weight: bold;'>Total Cart Cost: $0.00</p><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button></div></div>")
gr.HTML(create_modal_window())
gr.HTML(modal_js())
login_button.click(
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
)
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
app.launch() |