Create customdish.py
Browse files- customdish.py +104 -0
customdish.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Blueprint, request, jsonify, redirect, url_for, session
|
2 |
+
import random
|
3 |
+
from your_salesforce_module import sf # Import your Salesforce instance or utility
|
4 |
+
|
5 |
+
# Create a Blueprint for custom dish-related routes
|
6 |
+
customdish_blueprint = Blueprint('customdish', __name__)
|
7 |
+
|
8 |
+
@customdish_blueprint.route("/generate_custom_dish", methods=["POST"])
|
9 |
+
def generate_custom_dish():
|
10 |
+
try:
|
11 |
+
data = request.form
|
12 |
+
dish_name = data.get("name")
|
13 |
+
description = data.get("description")
|
14 |
+
item_image_url = "https://huggingface.co/spaces/nagasurendra/BiryaniHubflask30/resolve/main/static/customized.jpg"
|
15 |
+
item_image_url2 = "https://huggingface.co/spaces/nagasurendra/BiryaniHubflask30/resolve/main/static/customized1.jpg"
|
16 |
+
|
17 |
+
if not dish_name or not description:
|
18 |
+
return jsonify({"success": False, "error": "Both fields are required"}), 400
|
19 |
+
|
20 |
+
# Generate a random price for the custom dish
|
21 |
+
price = random.randint(10, 30) # Example logic for price setting
|
22 |
+
|
23 |
+
# Determine Veg/Non-Veg
|
24 |
+
veg_keywords = ["paneer", "vegetable", "mushroom", "cheese"]
|
25 |
+
non_veg_keywords = ["chicken", "mutton", "fish", "egg"]
|
26 |
+
|
27 |
+
category = "Veg" if any(word in description.lower() for word in veg_keywords) else \
|
28 |
+
"Non veg" if any(word in description.lower() for word in non_veg_keywords) else \
|
29 |
+
"both"
|
30 |
+
|
31 |
+
# Query to check if the dish already exists in Salesforce (Custom_Dish__c object)
|
32 |
+
existing_dish_query = f"SELECT Id, Name, Price__c, Image1__c, Image2__c, Description__c, Veg_NonVeg__c FROM Custom_Dish__c WHERE Name = '{dish_name}'"
|
33 |
+
existing_dish_result = sf.query(existing_dish_query)
|
34 |
+
|
35 |
+
if existing_dish_result['totalSize'] > 0:
|
36 |
+
# If the dish exists, use the existing details
|
37 |
+
existing_dish = existing_dish_result['records'][0]
|
38 |
+
price = existing_dish['Price__c']
|
39 |
+
item_image_url = existing_dish['Image1__c']
|
40 |
+
item_image_url2 = existing_dish['Image2__c']
|
41 |
+
category = existing_dish['Veg_NonVeg__c']
|
42 |
+
else:
|
43 |
+
# If the dish does not exist, create a new custom dish
|
44 |
+
custom_dish = {
|
45 |
+
'Name': dish_name,
|
46 |
+
'Price__c': price,
|
47 |
+
'Image1__c': item_image_url,
|
48 |
+
'Image2__c': item_image_url2,
|
49 |
+
'Description__c': description,
|
50 |
+
'Veg_NonVeg__c': category,
|
51 |
+
'Section__c': 'Customized dish',
|
52 |
+
'Total_Ordered__c': 0
|
53 |
+
}
|
54 |
+
|
55 |
+
# Insert the custom dish into Salesforce (Custom_Dish__c object)
|
56 |
+
result = sf.Custom_Dish__c.create(custom_dish)
|
57 |
+
|
58 |
+
if not result.get('success'):
|
59 |
+
return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500
|
60 |
+
|
61 |
+
# After ensuring the dish exists, check if it's already in the Cart_Item__c
|
62 |
+
email = session.get('user_email') # Assuming you have the user's email in session
|
63 |
+
|
64 |
+
# Query to check if the custom dish already exists in the cart for the logged-in user
|
65 |
+
cart_item_query = f"SELECT Id, Quantity__c, Price__c, Base_Price__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}' AND Name = '{dish_name}'"
|
66 |
+
cart_item_result = sf.query(cart_item_query)
|
67 |
+
|
68 |
+
if cart_item_result['totalSize'] > 0:
|
69 |
+
# If the custom dish is already in the cart, update the quantity and price
|
70 |
+
cart_item = cart_item_result['records'][0]
|
71 |
+
new_quantity = cart_item['Quantity__c'] + 1 # Increase quantity by 1
|
72 |
+
new_price = price * new_quantity # Update price based on new quantity
|
73 |
+
|
74 |
+
# Update the cart item in Salesforce
|
75 |
+
updated_cart_item = {
|
76 |
+
'Quantity__c': new_quantity,
|
77 |
+
'Price__c': new_price
|
78 |
+
}
|
79 |
+
|
80 |
+
cart_item_update = sf.Cart_Item__c.update(cart_item['Id'], updated_cart_item)
|
81 |
+
|
82 |
+
else:
|
83 |
+
# If the custom dish is not in the cart, create a new cart item
|
84 |
+
cart_item = {
|
85 |
+
'Name': dish_name,
|
86 |
+
'Price__c': price,
|
87 |
+
'Base_Price__c': price,
|
88 |
+
'Image1__c': item_image_url,
|
89 |
+
'Quantity__c': 1, # Default quantity is 1
|
90 |
+
'Add_Ons__c': '', # Set Add_ons__c to empty
|
91 |
+
'Add_Ons_Price__c': 0, # Set Add_ons_Price__c to 0
|
92 |
+
'Customer_Email__c': email # Associate the custom dish with the logged-in user
|
93 |
+
}
|
94 |
+
|
95 |
+
# Insert the custom dish as a Cart_Item__c record in Salesforce
|
96 |
+
cart_result = sf.Cart_Item__c.create(cart_item)
|
97 |
+
|
98 |
+
# Redirect to the cart page after successfully adding or updating the cart item
|
99 |
+
return redirect(url_for("cart"))
|
100 |
+
|
101 |
+
except Exception as e:
|
102 |
+
return jsonify({"success": False, "error": str(e)}), 500
|
103 |
+
|
104 |
+
|