Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,8 +49,20 @@ def get_ingredients():
|
|
| 49 |
if not sf:
|
| 50 |
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
soql += " LIMIT 200"
|
| 55 |
logger.debug(f"Executing SOQL query for Sector_Detail__c: {soql}")
|
| 56 |
result = sf.query(soql)
|
|
@@ -70,50 +82,6 @@ def get_ingredients():
|
|
| 70 |
|
| 71 |
@app.route('/get_menu_items', methods=['POST'])
|
| 72 |
def get_menu_items():
|
| 73 |
-
global sf
|
| 74 |
-
if not sf:
|
| 75 |
-
sf = get_salesforce_connection()
|
| 76 |
-
if not sf:
|
| 77 |
-
logger.error("Salesforce connection failed after retry")
|
| 78 |
-
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 79 |
-
|
| 80 |
-
data = request.json
|
| 81 |
-
dietary_preference = data.get('dietary_preference', 'both').lower()
|
| 82 |
-
search_term = data.get('search_term', '').strip()
|
| 83 |
-
|
| 84 |
-
try:
|
| 85 |
-
soql_menu = "SELECT Name, Description__c, Image1__c, Image2__c, Ingredientsinfo__c, Price__c, Section__c, Veg_NonVeg__c, Total_Ordered__c FROM Menu_Item__c"
|
| 86 |
-
if search_term:
|
| 87 |
-
soql_menu += f" WHERE Name LIKE '%{search_term}%'"
|
| 88 |
-
elif dietary_preference == 'vegetarian':
|
| 89 |
-
soql_menu += " WHERE Veg_NonVeg__c = 'Vegetarian'"
|
| 90 |
-
elif dietary_preference == 'non-vegetarian':
|
| 91 |
-
soql_menu += " WHERE Veg_NonVeg__c = 'Non-Vegetarian'"
|
| 92 |
-
soql_menu += " LIMIT 200"
|
| 93 |
-
logger.debug(f"Executing SOQL query for Menu_Item__c: {soql_menu}")
|
| 94 |
-
result_menu = sf.query(soql_menu)
|
| 95 |
-
menu_items = [
|
| 96 |
-
{
|
| 97 |
-
"name": record['Name'],
|
| 98 |
-
"description": record.get('Description__c', 'No description available'),
|
| 99 |
-
"image_url": record.get('Image1__c', '') or record.get('Image2__c', ''),
|
| 100 |
-
"ingredients": record.get('Ingredientsinfo__c', ''),
|
| 101 |
-
"price": record.get('Price__c', 0.0),
|
| 102 |
-
"section": record.get('Section__c', ''),
|
| 103 |
-
"veg_nonveg": record.get('Veg_NonVeg__c', ''),
|
| 104 |
-
"total_ordered": record.get('Total_Ordered__c', 0),
|
| 105 |
-
"source": "Menu_Item__c"
|
| 106 |
-
}
|
| 107 |
-
for record in result_menu['records'] if 'Name' in record
|
| 108 |
-
]
|
| 109 |
-
logger.debug(f"Fetched {len(menu_items)} menu items")
|
| 110 |
-
return jsonify({"menu_items": menu_items})
|
| 111 |
-
except Exception as e:
|
| 112 |
-
logger.error(f"Failed to fetch menu items: {str(e)}")
|
| 113 |
-
return jsonify({"error": f"Failed to fetch menu items from Salesforce: {str(e)}"}), 500
|
| 114 |
-
|
| 115 |
-
@app.route('/get_related_menu_items', methods=['POST'])
|
| 116 |
-
def get_related_menu_items():
|
| 117 |
global sf
|
| 118 |
if not sf:
|
| 119 |
sf = get_salesforce_connection()
|
|
@@ -121,43 +89,46 @@ def get_related_menu_items():
|
|
| 121 |
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 122 |
|
| 123 |
data = request.json
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
if not ingredient_name:
|
| 128 |
-
return jsonify({"error": "Ingredient name is required"}), 400
|
| 129 |
|
| 130 |
try:
|
| 131 |
-
soql = "SELECT Name, Description__c, Image1__c, Image2__c,
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
soql += " LIMIT 200"
|
| 137 |
-
logger.debug(f"Executing SOQL query for
|
| 138 |
result = sf.query(soql)
|
| 139 |
menu_items = [
|
| 140 |
{
|
| 141 |
"name": record['Name'],
|
| 142 |
"description": record.get('Description__c', 'No description available'),
|
| 143 |
"image_url": record.get('Image1__c', '') or record.get('Image2__c', ''),
|
| 144 |
-
"ingredients": record.get('Ingredientsinfo__c', ''),
|
| 145 |
"price": record.get('Price__c', 0.0),
|
| 146 |
"section": record.get('Section__c', ''),
|
| 147 |
"veg_nonveg": record.get('Veg_NonVeg__c', ''),
|
| 148 |
-
"total_ordered": record.get('Total_Ordered__c', 0)
|
| 149 |
-
"source": "Menu_Item__c"
|
| 150 |
}
|
| 151 |
for record in result['records'] if 'Name' in record
|
| 152 |
]
|
| 153 |
-
logger.debug(f"Fetched {len(menu_items)}
|
| 154 |
return jsonify({"menu_items": menu_items})
|
| 155 |
except Exception as e:
|
| 156 |
-
logger.error(f"Failed to fetch
|
| 157 |
-
return jsonify({"error": f"Failed to fetch
|
| 158 |
|
| 159 |
-
@app.route('/
|
| 160 |
-
def
|
| 161 |
global sf
|
| 162 |
if not sf:
|
| 163 |
sf = get_salesforce_connection()
|
|
@@ -166,35 +137,47 @@ def submit_ingredients():
|
|
| 166 |
|
| 167 |
data = request.json
|
| 168 |
items = data.get('items', [])
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
if not items:
|
| 173 |
-
return jsonify({'error': 'No items selected'}), 400
|
| 174 |
|
| 175 |
try:
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
sf.Ingredient_Object__c.create({
|
| 185 |
'Ingredient_Name__c': ingredient_name,
|
| 186 |
-
'Category__c':
|
| 187 |
-
'Description__c':
|
| 188 |
-
'Image_URL__c':
|
| 189 |
-
'Quantity__c':
|
| 190 |
-
'Ingredientsinfo__c':
|
| 191 |
})
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
| 195 |
except Exception as e:
|
| 196 |
-
logger.error(f"Failed to submit
|
| 197 |
-
return jsonify({"error": f"Failed to submit
|
| 198 |
|
| 199 |
if __name__ == '__main__':
|
| 200 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
| 49 |
if not sf:
|
| 50 |
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 51 |
|
| 52 |
+
data = request.json
|
| 53 |
+
dietary_preference = data.get('dietary_preference', 'both').lower()
|
| 54 |
+
|
| 55 |
try:
|
| 56 |
+
category_map = {
|
| 57 |
+
'vegetarian': 'Veg',
|
| 58 |
+
'non-vegetarian': 'Non-Veg',
|
| 59 |
+
'chicken': 'Non-Veg',
|
| 60 |
+
'beef': 'Non-Veg',
|
| 61 |
+
'lamb': 'Non-Veg',
|
| 62 |
+
'both': 'both'
|
| 63 |
+
}
|
| 64 |
+
category = category_map.get(dietary_preference, 'both')
|
| 65 |
+
soql = f"SELECT Name, Image_URL__c, Category__c FROM Sector_Detail__c WHERE Category__c = '{category}'"
|
| 66 |
soql += " LIMIT 200"
|
| 67 |
logger.debug(f"Executing SOQL query for Sector_Detail__c: {soql}")
|
| 68 |
result = sf.query(soql)
|
|
|
|
| 82 |
|
| 83 |
@app.route('/get_menu_items', methods=['POST'])
|
| 84 |
def get_menu_items():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
global sf
|
| 86 |
if not sf:
|
| 87 |
sf = get_salesforce_connection()
|
|
|
|
| 89 |
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 90 |
|
| 91 |
data = request.json
|
| 92 |
+
ingredient_names = data.get('ingredient_names', '')
|
| 93 |
+
category = data.get('category', '')
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
try:
|
| 96 |
+
soql = "SELECT Name, Description__c, Image1__c, Image2__c, Price__c, Section__c, Veg_NonVeg__c, Total_Ordered__c FROM Menu_Item__c"
|
| 97 |
+
conditions = []
|
| 98 |
+
if ingredient_names:
|
| 99 |
+
words = ingredient_names.split()
|
| 100 |
+
name_conditions = [f"Name LIKE '%{word}%'" for word in words]
|
| 101 |
+
conditions.append(f"({' OR '.join(name_conditions)})")
|
| 102 |
+
if category:
|
| 103 |
+
if category.lower() == 'vegetarian':
|
| 104 |
+
conditions.append("Veg_NonVeg__c = 'Vegetarian'")
|
| 105 |
+
elif category.lower() == 'non-vegetarian':
|
| 106 |
+
conditions.append("Veg_NonVeg__c = 'Non-Vegetarian'")
|
| 107 |
+
if conditions:
|
| 108 |
+
soql += " WHERE " + " AND ".join(conditions)
|
| 109 |
soql += " LIMIT 200"
|
| 110 |
+
logger.debug(f"Executing SOQL query for Menu_Item__c: {soql}")
|
| 111 |
result = sf.query(soql)
|
| 112 |
menu_items = [
|
| 113 |
{
|
| 114 |
"name": record['Name'],
|
| 115 |
"description": record.get('Description__c', 'No description available'),
|
| 116 |
"image_url": record.get('Image1__c', '') or record.get('Image2__c', ''),
|
|
|
|
| 117 |
"price": record.get('Price__c', 0.0),
|
| 118 |
"section": record.get('Section__c', ''),
|
| 119 |
"veg_nonveg": record.get('Veg_NonVeg__c', ''),
|
| 120 |
+
"total_ordered": record.get('Total_Ordered__c', 0)
|
|
|
|
| 121 |
}
|
| 122 |
for record in result['records'] if 'Name' in record
|
| 123 |
]
|
| 124 |
+
logger.debug(f"Fetched {len(menu_items)} menu items")
|
| 125 |
return jsonify({"menu_items": menu_items})
|
| 126 |
except Exception as e:
|
| 127 |
+
logger.error(f"Failed to fetch menu items: {str(e)}")
|
| 128 |
+
return jsonify({"error": f"Failed to fetch menu items from Salesforce: {str(e)}"}), 500
|
| 129 |
|
| 130 |
+
@app.route('/submit_customization_ingredients', methods=['POST'])
|
| 131 |
+
def submit_customization_ingredients():
|
| 132 |
global sf
|
| 133 |
if not sf:
|
| 134 |
sf = get_salesforce_connection()
|
|
|
|
| 137 |
|
| 138 |
data = request.json
|
| 139 |
items = data.get('items', [])
|
| 140 |
+
menu_item = data.get('menu_item', {})
|
| 141 |
+
ingredients = data.get('ingredients', [])
|
| 142 |
+
instructions = data.get('instructions', '')
|
|
|
|
|
|
|
| 143 |
|
| 144 |
try:
|
| 145 |
+
if items: # Cart submission
|
| 146 |
+
ingredient_name = f"Order_{datetime.now().strftime('%Y%m%d')}_{uuid.uuid4().hex[:8]}"
|
| 147 |
+
for item in items:
|
| 148 |
+
description = f"{item['name']} ({item.get('instructions', 'No instructions')})"
|
| 149 |
+
if item.get('ingredients'):
|
| 150 |
+
description += f", Ingredients: {', '.join(i['name'] for i in item['ingredients'])}"
|
| 151 |
+
sf.Ingredient_Object__c.create({
|
| 152 |
+
'Ingredient_Name__c': ingredient_name,
|
| 153 |
+
'Category__c': item.get('veg_nonveg', ''),
|
| 154 |
+
'Description__c': description,
|
| 155 |
+
'Image_URL__c': item.get('image_url', ''),
|
| 156 |
+
'Quantity__c': 1,
|
| 157 |
+
'Ingredientsinfo__c': item.get('instructions', '')
|
| 158 |
+
})
|
| 159 |
+
logger.debug(f"Submitted {len(items)} items under {ingredient_name}")
|
| 160 |
+
return jsonify({"success": True, "message": f"Submitted {len(items)} items"})
|
| 161 |
+
elif menu_item: # Single item customization
|
| 162 |
+
ingredient_name = f"Custom_{menu_item['name']}_{uuid.uuid4().hex[:8]}"
|
| 163 |
+
description = f"{menu_item['name']} ({instructions or 'No instructions'})"
|
| 164 |
+
if ingredients:
|
| 165 |
+
description += f", Ingredients: {', '.join(i['name'] for i in ingredients)}"
|
| 166 |
sf.Ingredient_Object__c.create({
|
| 167 |
'Ingredient_Name__c': ingredient_name,
|
| 168 |
+
'Category__c': menu_item.get('veg_nonveg', ''),
|
| 169 |
+
'Description__c': description,
|
| 170 |
+
'Image_URL__c': menu_item.get('image_url', ''),
|
| 171 |
+
'Quantity__c': 1,
|
| 172 |
+
'Ingredientsinfo__c': instructions
|
| 173 |
})
|
| 174 |
+
logger.debug(f"Submitted customization for {menu_item['name']}")
|
| 175 |
+
return jsonify({"success": True, "message": "Customization submitted"})
|
| 176 |
+
else:
|
| 177 |
+
return jsonify({"error": "No items or menu item provided"}), 400
|
| 178 |
except Exception as e:
|
| 179 |
+
logger.error(f"Failed to submit: {str(e)}")
|
| 180 |
+
return jsonify({"error": f"Failed to submit: {str(e)}"}), 500
|
| 181 |
|
| 182 |
if __name__ == '__main__':
|
| 183 |
app.run(debug=True, host='0.0.0.0', port=7860)
|