Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -460,128 +460,7 @@ def login():
|
|
460 |
|
461 |
return render_template("login.html")
|
462 |
|
463 |
-
@app.route("/menu", methods=["GET", "POST"])
|
464 |
-
def menu():
|
465 |
-
selected_category = request.args.get("category", "All")
|
466 |
-
user_email = session.get('user_email')
|
467 |
-
|
468 |
-
if not user_email:
|
469 |
-
user_email = request.args.get("email")
|
470 |
-
user_name = request.args.get("name")
|
471 |
-
|
472 |
-
if user_email:
|
473 |
-
session['user_email'] = user_email
|
474 |
-
session['user_name'] = user_name # Store name in session
|
475 |
-
else:
|
476 |
-
return redirect(url_for("login"))
|
477 |
-
else:
|
478 |
-
user_name = session.get('user_name') # Get name from session if it's already stored
|
479 |
-
|
480 |
-
# Get the first letter of the user's name (make it uppercase for consistency)
|
481 |
-
first_letter = user_name[0].upper() if user_name else "A"
|
482 |
-
|
483 |
-
try:
|
484 |
-
# Fetch user referral and reward points
|
485 |
-
user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
|
486 |
-
user_result = sf.query(user_query)
|
487 |
-
|
488 |
-
if not user_result['records']:
|
489 |
-
return redirect(url_for('login'))
|
490 |
-
|
491 |
-
referral_code = user_result['records'][0].get('Referral__c', 'N/A')
|
492 |
-
reward_points = user_result['records'][0].get('Reward_Points__c', 0)
|
493 |
-
|
494 |
-
# Query to fetch Menu_Item__c records including Total_Ordered__c for best sellers
|
495 |
-
menu_query = """
|
496 |
-
SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c
|
497 |
-
FROM Menu_Item__c
|
498 |
-
"""
|
499 |
-
result = sf.query(menu_query)
|
500 |
-
food_items = result['records'] if 'records' in result else []
|
501 |
-
|
502 |
-
# Ensure Total_Ordered__c has a valid value
|
503 |
-
for item in food_items:
|
504 |
-
if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None:
|
505 |
-
item['Total_Ordered__c'] = 0 # Default value
|
506 |
-
|
507 |
-
# Query to fetch Custom_Dish__c records created within the last 7 days with Total_Ordered__c > 10
|
508 |
-
custom_dish_query = """
|
509 |
-
SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c
|
510 |
-
FROM Custom_Dish__c
|
511 |
-
WHERE CreatedDate >= LAST_N_DAYS:7
|
512 |
-
"""
|
513 |
-
custom_dish_result = sf.query(custom_dish_query)
|
514 |
-
custom_dishes = custom_dish_result['records'] if 'records' in custom_dish_result else []
|
515 |
-
|
516 |
-
# Merge both Menu_Item__c and Custom_Dish__c records into the ordered menu
|
517 |
-
all_items = food_items + custom_dishes
|
518 |
-
|
519 |
-
# Define the order of sections, adding "Best Sellers" at the top
|
520 |
-
section_order = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "Customized dish", "Apetizer", "Desserts", "Soft Drinks"]
|
521 |
-
ordered_menu = {section: [] for section in section_order}
|
522 |
-
|
523 |
-
# Sort items by Total_Ordered__c in descending order and pick top 4 as best sellers
|
524 |
-
best_sellers = sorted(all_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True)
|
525 |
-
|
526 |
-
if selected_category == "Veg":
|
527 |
-
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
|
528 |
-
elif selected_category == "Non veg":
|
529 |
-
best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
|
530 |
-
|
531 |
-
# Take only the top 4 best sellers after filtering
|
532 |
-
best_sellers = best_sellers[:4]
|
533 |
-
|
534 |
-
# Ensure "Best Sellers" is added only if there are items after filtering
|
535 |
-
if best_sellers:
|
536 |
-
ordered_menu["Best Sellers"] = best_sellers
|
537 |
-
|
538 |
-
# Create a set to track item names already added to prevent duplicates
|
539 |
-
added_item_names = set()
|
540 |
-
|
541 |
-
# Filter and organize menu items based on category and section (to avoid duplicates)
|
542 |
-
for item in all_items:
|
543 |
-
section = item.get("Section__c", "Others") # Default to "Others" if missing
|
544 |
-
if section not in ordered_menu:
|
545 |
-
ordered_menu[section] = []
|
546 |
-
|
547 |
-
# Skip item if it's already been added to avoid duplicates
|
548 |
-
if item['Name'] in added_item_names:
|
549 |
-
continue
|
550 |
-
|
551 |
-
# Apply category filters
|
552 |
-
if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]:
|
553 |
-
continue
|
554 |
-
if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]:
|
555 |
-
continue
|
556 |
-
|
557 |
-
ordered_menu[section].append(item)
|
558 |
-
added_item_names.add(item['Name']) # Add item to the set of added items
|
559 |
-
print(f"Added item to {section}: {item['Name']}") # Debugging
|
560 |
|
561 |
-
# Remove empty sections
|
562 |
-
ordered_menu = {section: items for section, items in ordered_menu.items() if items}
|
563 |
-
print(f"Final ordered menu: {ordered_menu.keys()}") # Debugging
|
564 |
-
|
565 |
-
categories = ["All", "Veg", "Non veg"]
|
566 |
-
|
567 |
-
except Exception as e:
|
568 |
-
print(f"Error fetching menu data: {str(e)}")
|
569 |
-
ordered_menu = {}
|
570 |
-
categories = ["All", "Veg", "Non veg"]
|
571 |
-
referral_code = 'N/A'
|
572 |
-
reward_points = 0
|
573 |
-
|
574 |
-
# Pass the user's first letter (first_letter) to the template
|
575 |
-
return render_template(
|
576 |
-
"menu.html",
|
577 |
-
ordered_menu=ordered_menu,
|
578 |
-
categories=categories,
|
579 |
-
selected_category=selected_category,
|
580 |
-
referral_code=referral_code,
|
581 |
-
reward_points=reward_points,
|
582 |
-
user_name=user_name, # Pass name to the template
|
583 |
-
first_letter=first_letter # Pass first letter to the template
|
584 |
-
)
|
585 |
|
586 |
|
587 |
@app.route("/cart", methods=["GET"])
|
@@ -765,108 +644,6 @@ def add_suggestion_to_cart():
|
|
765 |
print(f"Error adding item to cart: {str(e)}")
|
766 |
return jsonify({"success": False, "error": str(e)})
|
767 |
|
768 |
-
@app.route('/cart/add', methods=['POST'])
|
769 |
-
def add_to_cart():
|
770 |
-
try:
|
771 |
-
# Get data from request
|
772 |
-
data = request.json
|
773 |
-
item_name = data.get('itemName', '').strip()
|
774 |
-
item_price = data.get('itemPrice')
|
775 |
-
item_image = data.get('itemImage')
|
776 |
-
addons = data.get('addons', [])
|
777 |
-
instructions = data.get('instructions', '')
|
778 |
-
category = data.get('category')
|
779 |
-
section = data.get('section')
|
780 |
-
quantity = data.get('quantity', 1) # Get the quantity field from the request
|
781 |
-
customer_email = session.get('user_email')
|
782 |
-
|
783 |
-
# Basic validation for required fields
|
784 |
-
if not item_name or not item_price:
|
785 |
-
return jsonify({"success": False, "error": "Item name and price are required."}), 400
|
786 |
-
|
787 |
-
if not customer_email:
|
788 |
-
return jsonify({"success": False, "error": "User email is required."}), 400
|
789 |
-
|
790 |
-
# Query to check if the item is already in the cart
|
791 |
-
query = f"""
|
792 |
-
SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
|
793 |
-
FROM Cart_Item__c
|
794 |
-
WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
|
795 |
-
"""
|
796 |
-
result = sf.query(query)
|
797 |
-
cart_items = result.get("records", [])
|
798 |
-
|
799 |
-
# Calculate the total price for the addons
|
800 |
-
addons_price = sum(addon['price'] for addon in addons)
|
801 |
-
new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
|
802 |
-
|
803 |
-
# If the item is already in the cart, update it
|
804 |
-
if cart_items:
|
805 |
-
cart_item_id = cart_items[0]['Id']
|
806 |
-
existing_quantity = cart_items[0]['Quantity__c']
|
807 |
-
existing_addons = cart_items[0].get('Add_Ons__c', "None")
|
808 |
-
existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
|
809 |
-
existing_instructions = cart_items[0].get('Instructions__c', "")
|
810 |
-
|
811 |
-
# Combine the new addons with the existing ones
|
812 |
-
combined_addons = existing_addons if existing_addons != "None" else ""
|
813 |
-
if new_addons:
|
814 |
-
combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
|
815 |
-
|
816 |
-
# Combine existing instructions with new instructions
|
817 |
-
combined_instructions = existing_instructions
|
818 |
-
if instructions:
|
819 |
-
combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
|
820 |
-
|
821 |
-
# Calculate total addons price
|
822 |
-
combined_addons_list = combined_addons.split("; ")
|
823 |
-
combined_addons_price = sum(
|
824 |
-
float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
|
825 |
-
)
|
826 |
-
|
827 |
-
# Update the cart item in Salesforce (updating quantity)
|
828 |
-
sf.Cart_Item__c.update(cart_item_id, {
|
829 |
-
"Quantity__c": existing_quantity + quantity, # Add the selected quantity
|
830 |
-
"Add_Ons__c": combined_addons,
|
831 |
-
"Add_Ons_Price__c": combined_addons_price,
|
832 |
-
"Instructions__c": combined_instructions,
|
833 |
-
"Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
|
834 |
-
"Category__c": category,
|
835 |
-
"Section__c": section
|
836 |
-
})
|
837 |
-
else:
|
838 |
-
# If the item is not already in the cart, create a new entry
|
839 |
-
addons_string = "None"
|
840 |
-
if addons:
|
841 |
-
addons_string = new_addons
|
842 |
-
|
843 |
-
total_price = item_price * quantity + addons_price # Multiply by the quantity
|
844 |
-
|
845 |
-
# Create new cart item in Salesforce
|
846 |
-
sf.Cart_Item__c.create({
|
847 |
-
"Name": item_name,
|
848 |
-
"Price__c": total_price,
|
849 |
-
"Base_Price__c": item_price,
|
850 |
-
"Quantity__c": quantity, # Use the selected quantity
|
851 |
-
"Add_Ons_Price__c": addons_price,
|
852 |
-
"Add_Ons__c": addons_string,
|
853 |
-
"Image1__c": item_image,
|
854 |
-
"Customer_Email__c": customer_email,
|
855 |
-
"Instructions__c": instructions,
|
856 |
-
"Category__c": category,
|
857 |
-
"Section__c": section
|
858 |
-
})
|
859 |
-
|
860 |
-
return jsonify({"success": True, "message": "Item added to cart successfully."})
|
861 |
-
|
862 |
-
except KeyError as e:
|
863 |
-
# Handle missing expected keys in request data
|
864 |
-
return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
|
865 |
-
|
866 |
-
except Exception as e:
|
867 |
-
# Log the error for debugging and return a general error message
|
868 |
-
print(f"Error adding item to cart: {str(e)}")
|
869 |
-
return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
|
870 |
|
871 |
|
872 |
@app.route("/cart/add_item", methods=["POST"])
|
|
|
460 |
|
461 |
return render_template("login.html")
|
462 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
463 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
464 |
|
465 |
|
466 |
@app.route("/cart", methods=["GET"])
|
|
|
644 |
print(f"Error adding item to cart: {str(e)}")
|
645 |
return jsonify({"success": False, "error": str(e)})
|
646 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
647 |
|
648 |
|
649 |
@app.route("/cart/add_item", methods=["POST"])
|