Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -63,9 +63,6 @@ def generate_coupon_code(length=10):
|
|
63 |
"""Generates a random alphanumeric coupon code"""
|
64 |
characters = string.ascii_uppercase + string.digits # A-Z, 0-9
|
65 |
return ''.join(random.choice(characters) for _ in range(length))
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
@app.route("/generate_custom_dish", methods=["POST"])
|
70 |
def generate_custom_dish():
|
71 |
try:
|
@@ -89,7 +86,7 @@ def generate_custom_dish():
|
|
89 |
"Non veg" if any(word in description.lower() for word in non_veg_keywords) else \
|
90 |
"both"
|
91 |
|
92 |
-
# Query to check if the dish already exists in Salesforce
|
93 |
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}'"
|
94 |
existing_dish_result = sf.query(existing_dish_query)
|
95 |
|
@@ -112,51 +109,43 @@ def generate_custom_dish():
|
|
112 |
'Total_Ordered__c': 0
|
113 |
}
|
114 |
|
115 |
-
# Insert the custom dish into Salesforce
|
116 |
result = sf.Custom_Dish__c.create(custom_dish)
|
117 |
|
118 |
if not result.get('success'):
|
119 |
return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500
|
120 |
|
121 |
-
#
|
122 |
email = session.get('user_email') # Assuming you have the user's email in session
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
134 |
updated_cart_item = {
|
135 |
'Quantity__c': new_quantity,
|
136 |
'Price__c': new_price
|
137 |
}
|
|
|
|
|
138 |
|
139 |
-
|
140 |
-
|
141 |
-
cart_update_result = sf.Cart_Item__c.update(existing_cart_item['Id'], updated_cart_item)
|
142 |
-
|
143 |
-
# Check the result; Salesforce may return different types of responses
|
144 |
-
if isinstance(cart_update_result, dict) and cart_update_result.get('success'):
|
145 |
-
return jsonify({"success": True, "message": "Cart item updated successfully"}), 200
|
146 |
-
else:
|
147 |
-
return jsonify({"success": False, "error": "Failed to update cart item in Salesforce"}), 500
|
148 |
-
|
149 |
-
except Exception as e:
|
150 |
-
return jsonify({"success": False, "error": str(e)}), 500
|
151 |
-
|
152 |
else:
|
153 |
-
# If the
|
154 |
cart_item = {
|
155 |
'Name': dish_name,
|
156 |
'Price__c': price,
|
157 |
'Base_Price__c': price,
|
158 |
'Image1__c': item_image_url,
|
159 |
-
'Quantity__c': 1, #
|
160 |
'Add_Ons__c': '', # Set Add_ons__c to empty
|
161 |
'Add_Ons_Price__c': 0, # Set Add_ons_Price__c to 0
|
162 |
'Customer_Email__c': email # Associate the custom dish with the logged-in user
|
@@ -168,11 +157,13 @@ def generate_custom_dish():
|
|
168 |
if not cart_result.get('success'):
|
169 |
return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
|
170 |
|
171 |
-
|
|
|
172 |
|
173 |
except Exception as e:
|
174 |
return jsonify({"success": False, "error": str(e)}), 500
|
175 |
|
|
|
176 |
|
177 |
|
178 |
|
|
|
63 |
"""Generates a random alphanumeric coupon code"""
|
64 |
characters = string.ascii_uppercase + string.digits # A-Z, 0-9
|
65 |
return ''.join(random.choice(characters) for _ in range(length))
|
|
|
|
|
|
|
66 |
@app.route("/generate_custom_dish", methods=["POST"])
|
67 |
def generate_custom_dish():
|
68 |
try:
|
|
|
86 |
"Non veg" if any(word in description.lower() for word in non_veg_keywords) else \
|
87 |
"both"
|
88 |
|
89 |
+
# Query to check if the dish already exists in Salesforce (Custom_Dish__c object)
|
90 |
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}'"
|
91 |
existing_dish_result = sf.query(existing_dish_query)
|
92 |
|
|
|
109 |
'Total_Ordered__c': 0
|
110 |
}
|
111 |
|
112 |
+
# Insert the custom dish into Salesforce (Custom_Dish__c object)
|
113 |
result = sf.Custom_Dish__c.create(custom_dish)
|
114 |
|
115 |
if not result.get('success'):
|
116 |
return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500
|
117 |
|
118 |
+
# After ensuring the dish exists, check if it's already in the Cart_Item__c
|
119 |
email = session.get('user_email') # Assuming you have the user's email in session
|
120 |
+
|
121 |
+
# Query to check if the custom dish already exists in the cart for the logged-in user
|
122 |
+
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}'"
|
123 |
+
cart_item_result = sf.query(cart_item_query)
|
124 |
+
|
125 |
+
if cart_item_result['totalSize'] > 0:
|
126 |
+
# If the custom dish is already in the cart, update the quantity and price
|
127 |
+
cart_item = cart_item_result['records'][0]
|
128 |
+
new_quantity = cart_item['Quantity__c'] + 1 # Increase quantity by 1
|
129 |
+
new_price = price * new_quantity # Update price based on new quantity
|
130 |
+
|
131 |
+
# Update the cart item in Salesforce
|
132 |
updated_cart_item = {
|
133 |
'Quantity__c': new_quantity,
|
134 |
'Price__c': new_price
|
135 |
}
|
136 |
+
|
137 |
+
cart_item_update = sf.Cart_Item__c.update(cart_item['Id'], updated_cart_item)
|
138 |
|
139 |
+
if not cart_item_update.get('success'):
|
140 |
+
return jsonify({"success": False, "error": "Failed to update cart item"}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
else:
|
142 |
+
# If the custom dish is not in the cart, create a new cart item
|
143 |
cart_item = {
|
144 |
'Name': dish_name,
|
145 |
'Price__c': price,
|
146 |
'Base_Price__c': price,
|
147 |
'Image1__c': item_image_url,
|
148 |
+
'Quantity__c': 1, # Default quantity is 1
|
149 |
'Add_Ons__c': '', # Set Add_ons__c to empty
|
150 |
'Add_Ons_Price__c': 0, # Set Add_ons_Price__c to 0
|
151 |
'Customer_Email__c': email # Associate the custom dish with the logged-in user
|
|
|
157 |
if not cart_result.get('success'):
|
158 |
return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
|
159 |
|
160 |
+
# Redirect to the cart page after successfully adding or updating the cart item
|
161 |
+
return redirect(url_for("cart"))
|
162 |
|
163 |
except Exception as e:
|
164 |
return jsonify({"success": False, "error": str(e)}), 500
|
165 |
|
166 |
+
|
167 |
|
168 |
|
169 |
|