nagasurendra commited on
Commit
2ba0ba2
·
verified ·
1 Parent(s): ab9d22a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -0
app.py CHANGED
@@ -120,6 +120,8 @@ def login():
120
  return render_template("login.html", error=f"Error: {str(e)}")
121
 
122
  return render_template("login.html")
 
 
123
  @app.route("/logout")
124
  def logout():
125
  # Retrieve table number before clearing session
@@ -134,5 +136,160 @@ def logout():
134
  # Pass table number to redirect page
135
  return render_template("redirect_page.html", table_number=table_number)
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  if __name__ == "__main__":
138
  app.run(debug=True, host="0.0.0.0", port=7860)
 
120
  return render_template("login.html", error=f"Error: {str(e)}")
121
 
122
  return render_template("login.html")
123
+
124
+
125
  @app.route("/logout")
126
  def logout():
127
  # Retrieve table number before clearing session
 
136
  # Pass table number to redirect page
137
  return render_template("redirect_page.html", table_number=table_number)
138
 
139
+
140
+
141
+ @app.route('/get_ingredients', methods=['POST'])
142
+ def get_ingredients():
143
+ global sf
144
+ if not sf:
145
+ sf = get_salesforce_connection()
146
+ if not sf:
147
+ return jsonify({"error": "Unable to connect to Salesforce"}), 500
148
+
149
+ data = request.json
150
+ dietary_preference = data.get('dietary_preference', 'both').lower()
151
+
152
+ try:
153
+ category_map = {
154
+ 'vegetarian': 'Veg',
155
+ 'non-vegetarian': 'Non-Veg',
156
+ 'chicken': 'Non-Veg',
157
+ 'beef': 'Non-Veg',
158
+ 'lamb': 'Non-Veg',
159
+ 'both': 'both'
160
+ }
161
+ category = category_map.get(dietary_preference, 'both')
162
+ soql = f"SELECT Name, Image_URL__c, Category__c FROM Sector_Detail__c WHERE Category__c = '{category}'"
163
+ soql += " LIMIT 200"
164
+ logger.debug(f"Executing SOQL query for Sector_Detail__c: {soql}")
165
+ result = sf.query(soql)
166
+ ingredients = [
167
+ {
168
+ "name": record['Name'],
169
+ "image_url": record.get('Image_URL__c', ''),
170
+ "category": record.get('Category__c', '')
171
+ }
172
+ for record in result['records'] if 'Name' in record
173
+ ]
174
+ logger.debug(f"Fetched {len(ingredients)} ingredients from Sector_Detail__c")
175
+ return jsonify({"ingredients": ingredients})
176
+ except Exception as e:
177
+ logger.error(f"Failed to fetch ingredients: {str(e)}")
178
+ return jsonify({"error": f"Failed to fetch ingredients from Salesforce: {str(e)}"}), 500
179
+
180
+ @app.route('/get_menu_items', methods=['POST'])
181
+ def get_menu_items():
182
+ global sf
183
+ if not sf:
184
+ sf = get_salesforce_connection()
185
+ if not sf:
186
+ return jsonify({"error": "Unable to connect to Salesforce"}), 500
187
+
188
+ data = request.json
189
+ ingredient_names = data.get('ingredient_names', '')
190
+ category = data.get('category', '')
191
+
192
+ try:
193
+ soql = "SELECT Name, Description__c, Image1__c, Image2__c, Price__c, Section__c, Veg_NonVeg__c, Total_Ordered__c FROM Menu_Item__c"
194
+ conditions = []
195
+ if ingredient_names:
196
+ words = ingredient_names.split()
197
+ name_conditions = [f"Name LIKE '%{word}%'" for word in words]
198
+ conditions.append(f"({' OR '.join(name_conditions)})")
199
+ if category:
200
+ if category.lower() == 'vegetarian':
201
+ conditions.append("Veg_NonVeg__c = 'Vegetarian'")
202
+ elif category.lower() == 'non-vegetarian':
203
+ conditions.append("Veg_NonVeg__c = 'Non-Vegetarian'")
204
+ if conditions:
205
+ soql += " WHERE " + " AND ".join(conditions)
206
+ soql += " LIMIT 200"
207
+ logger.debug(f"Executing SOQL query for Menu_Item__c: {soql}")
208
+ result = sf.query(soql)
209
+ menu_items = [
210
+ {
211
+ "name": record['Name'],
212
+ "description": record.get('Description__c', 'No description available'),
213
+ "image_url": record.get('Image1__c', '') or record.get('Image2__c', ''),
214
+ "price": record.get('Price__c', 0.0),
215
+ "section": record.get('Section__c', ''),
216
+ "veg_nonveg": record.get('Veg_NonVeg__c', ''),
217
+ "total_ordered": record.get('Total_Ordered__c', 0)
218
+ }
219
+ for record in result['records'] if 'Name' in record
220
+ ]
221
+ logger.debug(f"Fetched {len(menu_items)} menu items")
222
+ return jsonify({"menu_items": menu_items})
223
+ except Exception as e:
224
+ logger.error(f"Failed to fetch menu items: {str(e)}")
225
+ return jsonify({"error": f"Failed to fetch menu items from Salesforce: {str(e)}"}), 500
226
+
227
+ @app.route('/submit_customization_ingredients', methods=['POST'])
228
+ def submit_customization_ingredients():
229
+ global sf
230
+ if not sf:
231
+ sf = get_salesforce_connection()
232
+ if not sf:
233
+ return jsonify({"error": "Unable to connect to Salesforce"}), 500
234
+
235
+ data = request.json
236
+ items = data.get('items', [])
237
+ menu_item = data.get('menu_item', {})
238
+ ingredients = data.get('ingredients', [])
239
+ instructions = data.get('instructions', '')
240
+
241
+ try:
242
+ if items: # Cart submission
243
+ for item in items:
244
+ ingredient_names = ', '.join(i['name'] for i in item.get('ingredients', [])) if item.get('ingredients') else ''
245
+ base_price = item.get('price', 0.0)
246
+ quantity = 1
247
+ addons_price = 0
248
+ total_price = (base_price * quantity) + addons_price
249
+
250
+ sf.Cart_Item__c.create({
251
+ 'Name': item['name'],
252
+ 'Base_Price__c': base_price,
253
+ 'Quantity__c': quantity,
254
+ 'Add_Ons__c': ingredient_names,
255
+ 'Add_Ons_Price__c': addons_price,
256
+ 'Price__c': total_price,
257
+ 'Image1__c': item.get('image_url', ''),
258
+ 'Instructions__c': item.get('instructions', ''),
259
+ 'Category__c': item.get('veg_nonveg', ''),
260
+ 'Section__c': item.get('section', '')
261
+ })
262
+ logger.debug(f"Submitted {len(items)} items to Cart_Item__c")
263
+ return jsonify({"success": True, "message": f"Submitted {len(items)} items"})
264
+
265
+ elif menu_item: # Single item customization
266
+ ingredient_names = ', '.join(i['name'] for i in ingredients) if ingredients else ''
267
+ base_price = menu_item.get('price', 0.0)
268
+ quantity = 1
269
+ addons_price = 0
270
+ total_price = (base_price * quantity) + addons_price
271
+
272
+ sf.Cart_Item__c.create({
273
+ 'Name': menu_item['name'],
274
+ 'Base_Price__c': base_price,
275
+ 'Quantity__c': quantity,
276
+ 'Add_Ons__c': ingredient_names,
277
+ 'Add_Ons_Price__c': addons_price,
278
+ 'Price__c': total_price,
279
+ 'Image1__c': menu_item.get('image_url', ''),
280
+ 'Instructions__c': instructions,
281
+ 'Category__c': menu_item.get('veg_nonveg', ''),
282
+ 'Section__c': menu_item.get('section', '')
283
+ })
284
+ logger.debug(f"Submitted customization for {menu_item['name']} to Cart_Item__c")
285
+ return jsonify({"success": True, "message": "Customization submitted"})
286
+
287
+ else:
288
+ return jsonify({"error": "No items or menu item provided"}), 400
289
+
290
+ except Exception as e:
291
+ logger.error(f"Failed to submit: {str(e)}")
292
+ return jsonify({"error": f"Failed to submit: {str(e)}"}), 500
293
+
294
  if __name__ == "__main__":
295
  app.run(debug=True, host="0.0.0.0", port=7860)