Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,37 +64,40 @@ def get_ingredients():
|
|
| 64 |
|
| 65 |
@app.route('/get_menu_items', methods=['POST'])
|
| 66 |
def get_menu_items():
|
| 67 |
-
|
| 68 |
-
logging.debug(f"Received ingredient: {
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
"""
|
| 81 |
|
| 82 |
try:
|
|
|
|
| 83 |
result = sf.query(soql)
|
|
|
|
| 84 |
menu_items = [
|
| 85 |
-
{"name": record['Name'], "price": record.get('Price__c', ''), "description": record.get('Description__c', ''),
|
| 86 |
-
"
|
| 87 |
-
"veg_nonveg": record.get('Veg_NonVeg__c', ''), "section": record.get('Section__c', ''),
|
| 88 |
"total_ordered": record.get('Total_Ordered__c', '')}
|
| 89 |
for record in result['records'] if 'Name' in record
|
| 90 |
]
|
| 91 |
-
logging.debug(f"Fetched {len(menu_items)} menu items.")
|
| 92 |
return jsonify({"menu_items": menu_items})
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
logging.error(f"Error while fetching menu items: {str(e)}")
|
| 95 |
return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
|
| 96 |
|
| 97 |
|
|
|
|
| 98 |
@app.route('/submit_ingredients', methods=['POST'])
|
| 99 |
def submit_ingredients():
|
| 100 |
data = request.json
|
|
|
|
| 64 |
|
| 65 |
@app.route('/get_menu_items', methods=['POST'])
|
| 66 |
def get_menu_items():
|
| 67 |
+
ingredient_names = request.json.get('ingredient_names', '').strip().lower()
|
| 68 |
+
logging.debug(f"Received ingredient names: {ingredient_names}")
|
| 69 |
|
| 70 |
+
# Constructing a SOQL query where Name contains any of the ingredient names
|
| 71 |
+
# Split the ingredient names into separate words to match each ingredient
|
| 72 |
+
ingredients_list = ingredient_names.split()
|
| 73 |
|
| 74 |
+
# Create a dynamic WHERE clause to match the ingredient names in the Menu Item names
|
| 75 |
+
condition = " OR ".join([f"Name LIKE '%{ingredient}%'" for ingredient in ingredients_list])
|
| 76 |
+
|
| 77 |
+
if not condition:
|
| 78 |
+
logging.debug("No ingredients received.")
|
| 79 |
+
return jsonify({"error": "No valid ingredients provided."}), 400
|
|
|
|
| 80 |
|
| 81 |
try:
|
| 82 |
+
soql = f"SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c FROM Menu_Item__c WHERE {condition} LIMIT 200"
|
| 83 |
result = sf.query(soql)
|
| 84 |
+
|
| 85 |
menu_items = [
|
| 86 |
+
{"name": record['Name'], "price": record.get('Price__c', ''), "description": record.get('Description__c', ''),
|
| 87 |
+
"image_url1": record.get('Image1__c', ''), "image_url2": record.get('Image2__c', ''),
|
| 88 |
+
"veg_nonveg": record.get('Veg_NonVeg__c', ''), "section": record.get('Section__c', ''),
|
| 89 |
"total_ordered": record.get('Total_Ordered__c', '')}
|
| 90 |
for record in result['records'] if 'Name' in record
|
| 91 |
]
|
| 92 |
+
logging.debug(f"Fetched {len(menu_items)} menu items based on ingredients.")
|
| 93 |
return jsonify({"menu_items": menu_items})
|
| 94 |
+
|
| 95 |
except Exception as e:
|
| 96 |
logging.error(f"Error while fetching menu items: {str(e)}")
|
| 97 |
return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
|
| 98 |
|
| 99 |
|
| 100 |
+
|
| 101 |
@app.route('/submit_ingredients', methods=['POST'])
|
| 102 |
def submit_ingredients():
|
| 103 |
data = request.json
|