Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -78,36 +78,37 @@ def generate_custom_dish():
|
|
78 |
if not dish_name or not description:
|
79 |
return jsonify({"success": False, "error": "Both fields are required"}), 400
|
80 |
|
81 |
-
#
|
82 |
-
|
|
|
83 |
|
84 |
-
#
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
'Description__c': description,
|
99 |
-
'Veg_NonVeg__c': category,
|
100 |
-
'Total_Ordered__c': 0
|
101 |
-
}
|
102 |
|
103 |
-
|
104 |
-
|
105 |
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
108 |
email = session.get('user_email') # Assuming you have the user's email in session
|
109 |
-
|
110 |
-
# Create the cart item with custom dish details
|
111 |
cart_item = {
|
112 |
'Name': dish_name,
|
113 |
'Price__c': price,
|
@@ -116,11 +117,12 @@ def generate_custom_dish():
|
|
116 |
'Quantity__c': 1, # Assuming a default quantity of 1 for the custom dish
|
117 |
'Add_Ons__c': '', # Set Add_ons__c to empty
|
118 |
'Add_Ons_Price__c': 0, # Set Add_Ons_Price__c to 0
|
119 |
-
'Instructions__c':
|
120 |
-
'Customer_Email__c': email # Associate the custom dish with the logged-in user
|
|
|
121 |
}
|
122 |
|
123 |
-
# Insert the
|
124 |
cart_result = sf.Cart_Item__c.create(cart_item)
|
125 |
|
126 |
if cart_result.get('success'):
|
@@ -129,13 +131,65 @@ def generate_custom_dish():
|
|
129 |
return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
|
130 |
|
131 |
else:
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
except Exception as e:
|
135 |
return jsonify({"success": False, "error": str(e)}), 500
|
136 |
|
137 |
|
138 |
|
|
|
139 |
import re
|
140 |
@app.route("/edit_profile", methods=["GET", "POST"])
|
141 |
def edit_profile():
|
|
|
78 |
if not dish_name or not description:
|
79 |
return jsonify({"success": False, "error": "Both fields are required"}), 400
|
80 |
|
81 |
+
# Check if the custom dish already exists (case-sensitive check)
|
82 |
+
query = f"SELECT Id, Name, Total_Ordered__c FROM Custom_Dish__c WHERE Name = '{dish_name}'"
|
83 |
+
result = sf.query(query)
|
84 |
|
85 |
+
# If the dish exists, use the existing record ID and increment Total_Ordered__c
|
86 |
+
if result['records']:
|
87 |
+
# Custom dish already exists
|
88 |
+
custom_dish = result['records'][0] # Get the existing custom dish record
|
89 |
+
custom_dish_id = custom_dish['Id']
|
90 |
+
total_ordered = custom_dish.get('Total_Ordered__c', 0)
|
91 |
+
|
92 |
+
# Increment the Total_Ordered__c by 1
|
93 |
+
new_total_ordered = total_ordered + 1
|
94 |
+
|
95 |
+
# Update the existing custom dish with the new Total_Ordered__c value
|
96 |
+
update_data = {
|
97 |
+
'Total_Ordered__c': new_total_ordered
|
98 |
+
}
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
# Update the existing custom dish in Salesforce
|
101 |
+
update_result = sf.Custom_Dish__c.update(custom_dish_id, update_data)
|
102 |
|
103 |
+
# Check if update was successful
|
104 |
+
if not update_result.get('success'):
|
105 |
+
return jsonify({"success": False, "error": "Failed to update custom dish in Salesforce"}), 500
|
106 |
+
|
107 |
+
# Set up cart item using the existing custom dish details
|
108 |
+
price = custom_dish['Price__c'] # Use the existing price
|
109 |
email = session.get('user_email') # Assuming you have the user's email in session
|
110 |
+
|
111 |
+
# Create the cart item with custom dish details
|
112 |
cart_item = {
|
113 |
'Name': dish_name,
|
114 |
'Price__c': price,
|
|
|
117 |
'Quantity__c': 1, # Assuming a default quantity of 1 for the custom dish
|
118 |
'Add_Ons__c': '', # Set Add_ons__c to empty
|
119 |
'Add_Ons_Price__c': 0, # Set Add_Ons_Price__c to 0
|
120 |
+
'Instructions__c': description, # Use the custom dish description as the instructions
|
121 |
+
'Customer_Email__c': email, # Associate the custom dish with the logged-in user
|
122 |
+
'Custom_Dish__c': custom_dish_id # Link the cart item to the existing custom dish
|
123 |
}
|
124 |
|
125 |
+
# Insert the cart item as a Cart_Item__c record in Salesforce
|
126 |
cart_result = sf.Cart_Item__c.create(cart_item)
|
127 |
|
128 |
if cart_result.get('success'):
|
|
|
131 |
return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
|
132 |
|
133 |
else:
|
134 |
+
# Custom dish does not exist, create a new one
|
135 |
+
price = random.randint(10, 30) # Example logic for price setting
|
136 |
+
|
137 |
+
# Determine Veg/Non-Veg
|
138 |
+
veg_keywords = ["paneer", "vegetable", "mushroom", "cheese"]
|
139 |
+
non_veg_keywords = ["chicken", "mutton", "fish", "egg"]
|
140 |
+
|
141 |
+
category = "Veg" if any(word in description.lower() for word in veg_keywords) else \
|
142 |
+
"Non veg" if any(word in description.lower() for word in non_veg_keywords) else \
|
143 |
+
"both"
|
144 |
+
|
145 |
+
# Create a new Custom_Dish__c record in Salesforce
|
146 |
+
custom_dish = {
|
147 |
+
'Name': dish_name,
|
148 |
+
'Price__c': price,
|
149 |
+
'Image1__c': item_image_url,
|
150 |
+
'Image2__c': item_image_url2,
|
151 |
+
'Description__c': description,
|
152 |
+
'Veg_NonVeg__c': category,
|
153 |
+
'Total_Ordered__c': 1 # Set Total_Ordered__c to 1 for new custom dishes
|
154 |
+
}
|
155 |
+
|
156 |
+
# Insert the custom dish into Salesforce
|
157 |
+
result = sf.Custom_Dish__c.create(custom_dish)
|
158 |
+
|
159 |
+
if result.get('success'):
|
160 |
+
# After creating the custom dish, add it to the Cart_Item__c
|
161 |
+
email = session.get('user_email') # Assuming you have the user's email in session
|
162 |
+
|
163 |
+
# Create the cart item with custom dish details and set other fields to default values
|
164 |
+
cart_item = {
|
165 |
+
'Name': dish_name,
|
166 |
+
'Price__c': price,
|
167 |
+
'Base_Price__c': price,
|
168 |
+
'Image1__c': item_image_url,
|
169 |
+
'Quantity__c': 1, # Assuming a default quantity of 1 for the custom dish
|
170 |
+
'Add_Ons__c': '', # Set Add_ons__c to empty
|
171 |
+
'Add_Ons_Price__c': 0, # Set Add_Ons_Price__c to 0
|
172 |
+
'Instructions__c': description, # Use the custom dish description as the instructions
|
173 |
+
'Customer_Email__c': email # Associate the custom dish with the logged-in user
|
174 |
+
}
|
175 |
+
|
176 |
+
# Insert the custom dish as a Cart_Item__c record in Salesforce
|
177 |
+
cart_result = sf.Cart_Item__c.create(cart_item)
|
178 |
+
|
179 |
+
if cart_result.get('success'):
|
180 |
+
return redirect(url_for("cart")) # Redirect to the cart page after adding to the cart
|
181 |
+
else:
|
182 |
+
return jsonify({"success": False, "error": "Failed to add custom dish to the cart"}), 500
|
183 |
+
|
184 |
+
else:
|
185 |
+
return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500
|
186 |
|
187 |
except Exception as e:
|
188 |
return jsonify({"success": False, "error": str(e)}), 500
|
189 |
|
190 |
|
191 |
|
192 |
+
|
193 |
import re
|
194 |
@app.route("/edit_profile", methods=["GET", "POST"])
|
195 |
def edit_profile():
|