Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -35,18 +35,19 @@ sf = get_salesforce_connection()
|
|
35 |
def index():
|
36 |
return render_template('index.html')
|
37 |
|
38 |
-
# Fetch
|
39 |
@app.route('/get_menu_items', methods=['POST'])
|
40 |
def get_menu_items():
|
41 |
global sf
|
42 |
if not sf:
|
43 |
sf = get_salesforce_connection()
|
44 |
if not sf:
|
|
|
45 |
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
46 |
|
47 |
dietary_preference = request.json.get('dietary_preference', 'both').lower()
|
48 |
try:
|
49 |
-
soql = "SELECT Name, Image_URL__c, Category__c FROM
|
50 |
if dietary_preference == 'vegetarian':
|
51 |
soql += " WHERE Category__c = 'Veg'"
|
52 |
elif dietary_preference == 'non-vegetarian':
|
@@ -55,7 +56,7 @@ def get_menu_items():
|
|
55 |
|
56 |
logger.info(f"Executing SOQL query: {soql}")
|
57 |
result = sf.query(soql)
|
58 |
-
|
59 |
{
|
60 |
"name": record['Name'],
|
61 |
"image_url": record.get('Image_URL__c', ''),
|
@@ -63,11 +64,11 @@ def get_menu_items():
|
|
63 |
}
|
64 |
for record in result['records'] if 'Name' in record
|
65 |
]
|
66 |
-
logger.info(f"Fetched {len(
|
67 |
-
return jsonify({"menu_items":
|
68 |
except Exception as e:
|
69 |
-
logger.error(f"Failed to fetch
|
70 |
-
return jsonify({"error": "Failed to fetch
|
71 |
|
72 |
# Fetch suggestions from Ingredient_Object__c
|
73 |
@app.route('/suggest_items', methods=['POST'])
|
@@ -125,9 +126,9 @@ def get_item_details():
|
|
125 |
"ingredients": "Ingredients not listed",
|
126 |
"image_url": record.get('Image_URL__c', '')
|
127 |
}
|
128 |
-
logger.info(f"Fallback: Found '{item_name}' in
|
129 |
return jsonify({"item_details": item_details})
|
130 |
-
return jsonify({"error": f"No item found matching '{item_name}'"}), 404
|
131 |
|
132 |
record = result['records'][0]
|
133 |
item_details = {
|
@@ -157,15 +158,14 @@ def submit_items():
|
|
157 |
|
158 |
try:
|
159 |
for item in items:
|
160 |
-
# Create a new record in Ingredient_Object__c
|
161 |
sf.Ingredient_Object__c.create({
|
162 |
'Name': item['name'],
|
163 |
'Description__c': item['description'],
|
164 |
'Ingredients__c': item['ingredients'],
|
165 |
'Image_URL__c': item['image_url'],
|
166 |
-
'Category__c': item.get('category', 'Both')
|
167 |
})
|
168 |
-
logger.info(f"Submitted item to
|
169 |
return jsonify({"success": f"Successfully submitted {len(items)} items to Salesforce"})
|
170 |
except Exception as e:
|
171 |
logger.error(f"Failed to submit items: {str(e)}")
|
|
|
35 |
def index():
|
36 |
return render_template('index.html')
|
37 |
|
38 |
+
# Fetch items from Sector_Detail__c for dietary preferences
|
39 |
@app.route('/get_menu_items', methods=['POST'])
|
40 |
def get_menu_items():
|
41 |
global sf
|
42 |
if not sf:
|
43 |
sf = get_salesforce_connection()
|
44 |
if not sf:
|
45 |
+
logger.error("Salesforce connection failed after retry")
|
46 |
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
47 |
|
48 |
dietary_preference = request.json.get('dietary_preference', 'both').lower()
|
49 |
try:
|
50 |
+
soql = "SELECT Name, Image_URL__c, Category__c FROM Sector_Detail__c"
|
51 |
if dietary_preference == 'vegetarian':
|
52 |
soql += " WHERE Category__c = 'Veg'"
|
53 |
elif dietary_preference == 'non-vegetarian':
|
|
|
56 |
|
57 |
logger.info(f"Executing SOQL query: {soql}")
|
58 |
result = sf.query(soql)
|
59 |
+
items = [
|
60 |
{
|
61 |
"name": record['Name'],
|
62 |
"image_url": record.get('Image_URL__c', ''),
|
|
|
64 |
}
|
65 |
for record in result['records'] if 'Name' in record
|
66 |
]
|
67 |
+
logger.info(f"Fetched {len(items)} items from Sector_Detail__c for {dietary_preference}")
|
68 |
+
return jsonify({"menu_items": items})
|
69 |
except Exception as e:
|
70 |
+
logger.error(f"Failed to fetch items from Sector_Detail__c: {str(e)}")
|
71 |
+
return jsonify({"error": f"Failed to fetch items from Salesforce: {str(e)}"}), 500
|
72 |
|
73 |
# Fetch suggestions from Ingredient_Object__c
|
74 |
@app.route('/suggest_items', methods=['POST'])
|
|
|
126 |
"ingredients": "Ingredients not listed",
|
127 |
"image_url": record.get('Image_URL__c', '')
|
128 |
}
|
129 |
+
logger.info(f"Fallback: Found '{item_name}' in Ingredient_Object__c")
|
130 |
return jsonify({"item_details": item_details})
|
131 |
+
return jsonify({"error": f"No item found matching '{item_name}' in Ingredient_Object__c"}), 404
|
132 |
|
133 |
record = result['records'][0]
|
134 |
item_details = {
|
|
|
158 |
|
159 |
try:
|
160 |
for item in items:
|
|
|
161 |
sf.Ingredient_Object__c.create({
|
162 |
'Name': item['name'],
|
163 |
'Description__c': item['description'],
|
164 |
'Ingredients__c': item['ingredients'],
|
165 |
'Image_URL__c': item['image_url'],
|
166 |
+
'Category__c': item.get('category', 'Both')
|
167 |
})
|
168 |
+
logger.info(f"Submitted item to Ingredient_Object__c: {item['name']}")
|
169 |
return jsonify({"success": f"Successfully submitted {len(items)} items to Salesforce"})
|
170 |
except Exception as e:
|
171 |
logger.error(f"Failed to submit items: {str(e)}")
|