Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from simple_salesforce import Salesforce
|
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
import logging
|
|
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
@@ -71,7 +72,7 @@ def get_menu_items():
|
|
71 |
logger.error(f"Failed to fetch items from Sector_Detail__c: {str(e)}")
|
72 |
return jsonify({"error": f"Failed to fetch items from Salesforce: {str(e)}"}), 500
|
73 |
|
74 |
-
# Fetch suggestions from Ingredient_Object__c
|
75 |
@app.route('/suggest_items', methods=['POST'])
|
76 |
def suggest_items():
|
77 |
global sf
|
@@ -85,87 +86,33 @@ def suggest_items():
|
|
85 |
return jsonify({"error": "Search term is required"}), 400
|
86 |
|
87 |
try:
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
91 |
suggestions = [
|
92 |
-
{"name": record['Ingredient_Name__c'], "image_url": record.get('Image_URL__c', '')}
|
93 |
-
for record in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
]
|
|
|
95 |
logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
|
96 |
return jsonify({"suggestions": suggestions})
|
97 |
except Exception as e:
|
98 |
logger.error(f"Failed to fetch suggestions: {str(e)}")
|
99 |
return jsonify({"error": "Failed to fetch suggestions from Salesforce"}), 500
|
100 |
|
101 |
-
# Fetch item details from
|
102 |
-
@app.route('/
|
103 |
-
def
|
104 |
-
global sf
|
105 |
-
if not sf:
|
106 |
-
sf = get_salesforce_connection()
|
107 |
-
if not sf:
|
108 |
-
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
109 |
-
|
110 |
-
item_name = request.json.get('item_name', '').strip()
|
111 |
-
if not item_name:
|
112 |
-
return jsonify({"error": "Item name is required"}), 400
|
113 |
-
|
114 |
-
try:
|
115 |
-
soql = f"SELECT Ingredient_Name__c, Image_URL__c FROM Ingredient_Object__c WHERE Ingredient_Name__c LIKE '%{item_name}%' LIMIT 1"
|
116 |
-
logger.info(f"Executing SOQL query: {soql}")
|
117 |
-
result = sf.query(soql)
|
118 |
-
|
119 |
-
if result['totalSize'] == 0:
|
120 |
-
return jsonify({"error": f"No item found matching '{item_name}' in Ingredient_Object__c"}), 404
|
121 |
-
|
122 |
-
record = result['records'][0]
|
123 |
-
item_details = {
|
124 |
-
"name": record.get('Ingredient_Name__c', ''),
|
125 |
-
"image_url": record.get('Image_URL__c', '')
|
126 |
-
}
|
127 |
-
logger.info(f"Fetched details for '{item_name}'")
|
128 |
-
return jsonify({"item_details": item_details})
|
129 |
-
except Exception as e:
|
130 |
-
logger.error(f"Failed to fetch item details: {str(e)}")
|
131 |
-
return jsonify({"error": "Failed to fetch item details from Salesforce"}), 500
|
132 |
-
|
133 |
-
# Fetch item details from Sector_Detail__c by name
|
134 |
-
@app.route('/get_item_details_by_name', methods=['POST'])
|
135 |
-
def get_item_details_by_name():
|
136 |
-
global sf
|
137 |
-
if not sf:
|
138 |
-
sf = get_salesforce_connection()
|
139 |
-
if not sf:
|
140 |
-
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
141 |
-
|
142 |
-
item_name = request.json.get('item_name', '').strip()
|
143 |
-
if not item_name:
|
144 |
-
return jsonify({"error": "Item name is required"}), 400
|
145 |
-
|
146 |
-
try:
|
147 |
-
soql = f"SELECT Name, Category__c, Description__c FROM Sector_Detail__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
148 |
-
logger.info(f"Executing SOQL query: {soql}")
|
149 |
-
result = sf.query(soql)
|
150 |
-
|
151 |
-
if result['totalSize'] == 0:
|
152 |
-
return jsonify({"error": f"No item found matching '{item_name}' in Sector_Detail__c"}), 404
|
153 |
-
|
154 |
-
record = result['records'][0]
|
155 |
-
item_details = {
|
156 |
-
"name": record.get('Name', ''),
|
157 |
-
"category": record.get('Category__c', 'Unknown'),
|
158 |
-
"description": record.get('Description__c', 'No description available')
|
159 |
-
}
|
160 |
-
logger.info(f"Fetched details for '{item_name}' from Sector_Detail__c")
|
161 |
-
return jsonify({"item_details": item_details})
|
162 |
-
except Exception as e:
|
163 |
-
logger.error(f"Failed to fetch item details from Sector_Detail__c: {str(e)}")
|
164 |
-
return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500
|
165 |
-
|
166 |
-
# Fetch item details (Name, Image_URL, Category, Description) from Sector_Detail__c by name
|
167 |
-
@app.route('/get_sector_item_details', methods=['POST'])
|
168 |
-
def get_sector_item_details():
|
169 |
global sf
|
170 |
if not sf:
|
171 |
sf = get_salesforce_connection()
|
@@ -177,27 +124,40 @@ def get_sector_item_details():
|
|
177 |
return jsonify({"error": "Item name is required"}), 400
|
178 |
|
179 |
try:
|
180 |
-
soql = f"
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
logger.info(f"Executing SOQL query: {soql}")
|
182 |
result = sf.query(soql)
|
183 |
|
184 |
if result['totalSize'] == 0:
|
185 |
-
return jsonify({"error": f"No item found matching '{item_name}' in
|
186 |
|
187 |
record = result['records'][0]
|
188 |
item_details = {
|
189 |
"name": record.get('Name', ''),
|
190 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
"category": record.get('Category__c', 'Unknown'),
|
192 |
-
"
|
193 |
}
|
194 |
-
logger.info(f"Fetched details for '{item_name}' from
|
195 |
return jsonify({"item_details": item_details})
|
196 |
except Exception as e:
|
197 |
-
logger.error(f"Failed to fetch item details from
|
198 |
return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500
|
199 |
|
200 |
-
# Submit selected items to Ingredient_Object__c
|
201 |
@app.route('/submit_items', methods=['POST'])
|
202 |
def submit_items():
|
203 |
global sf
|
@@ -213,12 +173,28 @@ def submit_items():
|
|
213 |
try:
|
214 |
for item in items:
|
215 |
logger.info(f"Submitting item: {item}")
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
return jsonify({"success": f"Successfully submitted {len(items)} items to Salesforce"})
|
223 |
except Exception as e:
|
224 |
logger.error(f"Failed to submit items: {str(e)}")
|
|
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
import logging
|
6 |
+
import uuid
|
7 |
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
|
|
72 |
logger.error(f"Failed to fetch items from Sector_Detail__c: {str(e)}")
|
73 |
return jsonify({"error": f"Failed to fetch items from Salesforce: {str(e)}"}), 500
|
74 |
|
75 |
+
# Fetch suggestions from Ingredient_Object__c and Menu_Item__c
|
76 |
@app.route('/suggest_items', methods=['POST'])
|
77 |
def suggest_items():
|
78 |
global sf
|
|
|
86 |
return jsonify({"error": "Search term is required"}), 400
|
87 |
|
88 |
try:
|
89 |
+
# Query Ingredient_Object__c
|
90 |
+
soql_ingredient = f"SELECT Ingredient_Name__c, Image_URL__c FROM Ingredient_Object__c WHERE Ingredient_Name__c LIKE '%{search_term}%' LIMIT 5"
|
91 |
+
logger.info(f"Executing SOQL query for Ingredient_Object__c: {soql_ingredient}")
|
92 |
+
result_ingredient = sf.query(soql_ingredient)
|
93 |
suggestions = [
|
94 |
+
{"name": record['Ingredient_Name__c'], "image_url": record.get('Image_URL__c', ''), "source": "Ingredient_Object__c"}
|
95 |
+
for record in result_ingredient['records'] if 'Ingredient_Name__c' in record
|
96 |
+
]
|
97 |
+
|
98 |
+
# Query Menu_Item__c
|
99 |
+
soql_menu = f"SELECT Name, Image1__c, Veg_NonVeg__c FROM Menu_Item__c WHERE Name LIKE '%{search_term}%' LIMIT 5"
|
100 |
+
logger.info(f"Executing SOQL query for Menu_Item__c: {soql_menu}")
|
101 |
+
result_menu = sf.query(soql_menu)
|
102 |
+
suggestions += [
|
103 |
+
{"name": record['Name'], "image_url": record.get('Image1__c', ''), "source": "Menu_Item__c", "veg_nonveg": record.get('Veg_NonVeg__c', '')}
|
104 |
+
for record in result_menu['records'] if 'Name' in record
|
105 |
]
|
106 |
+
|
107 |
logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
|
108 |
return jsonify({"suggestions": suggestions})
|
109 |
except Exception as e:
|
110 |
logger.error(f"Failed to fetch suggestions: {str(e)}")
|
111 |
return jsonify({"error": "Failed to fetch suggestions from Salesforce"}), 500
|
112 |
|
113 |
+
# Fetch item details from Menu_Item__c by name
|
114 |
+
@app.route('/get_menu_item_details', methods=['POST'])
|
115 |
+
def get_menu_item_details():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
global sf
|
117 |
if not sf:
|
118 |
sf = get_salesforce_connection()
|
|
|
124 |
return jsonify({"error": "Item name is required"}), 400
|
125 |
|
126 |
try:
|
127 |
+
soql = f"""
|
128 |
+
SELECT Name, Description__c, Image1__c, Ingredientsinfo__c, NutritionalInfo__c, Price__c,
|
129 |
+
Sector__c, Spice_Levels__c, Veg_NonVeg__c, Category__c, Dynamic_Dish__c
|
130 |
+
FROM Menu_Item__c
|
131 |
+
WHERE Name LIKE '%{item_name}%'
|
132 |
+
LIMIT 1
|
133 |
+
"""
|
134 |
logger.info(f"Executing SOQL query: {soql}")
|
135 |
result = sf.query(soql)
|
136 |
|
137 |
if result['totalSize'] == 0:
|
138 |
+
return jsonify({"error": f"No item found matching '{item_name}' in Menu_Item__c"}), 404
|
139 |
|
140 |
record = result['records'][0]
|
141 |
item_details = {
|
142 |
"name": record.get('Name', ''),
|
143 |
+
"description": record.get('Description__c', 'No description available'),
|
144 |
+
"image_url": record.get('Image1__c', 'https://via.placeholder.com/100'),
|
145 |
+
"ingredients": record.get('Ingredientsinfo__c', 'No ingredients info'),
|
146 |
+
"nutritional_info": record.get('NutritionalInfo__c', 'No nutritional info'),
|
147 |
+
"price": record.get('Price__c', 0.0),
|
148 |
+
"sector": record.get('Sector__c', 'Unknown'),
|
149 |
+
"spice_levels": record.get('Spice_Levels__c', 'Not specified'),
|
150 |
+
"veg_nonveg": record.get('Veg_NonVeg__c', 'Unknown'),
|
151 |
"category": record.get('Category__c', 'Unknown'),
|
152 |
+
"dynamic_dish": record.get('Dynamic_Dish__c', False)
|
153 |
}
|
154 |
+
logger.info(f"Fetched details for '{item_name}' from Menu_Item__c")
|
155 |
return jsonify({"item_details": item_details})
|
156 |
except Exception as e:
|
157 |
+
logger.error(f"Failed to fetch item details from Menu_Item__c: {str(e)}")
|
158 |
return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500
|
159 |
|
160 |
+
# Submit selected items to Ingredient_Object__c or Menu_Item__c
|
161 |
@app.route('/submit_items', methods=['POST'])
|
162 |
def submit_items():
|
163 |
global sf
|
|
|
173 |
try:
|
174 |
for item in items:
|
175 |
logger.info(f"Submitting item: {item}")
|
176 |
+
# Check if item is from Menu_Item__c or Ingredient_Object__c
|
177 |
+
if item.get('source') == 'Menu_Item__c':
|
178 |
+
sf.Menu_Item__c.create({
|
179 |
+
'Name': item['name'],
|
180 |
+
'Description__c': item.get('description', 'No description available'),
|
181 |
+
'Image1__c': item.get('image_url', ''),
|
182 |
+
'Ingredientsinfo__c': item.get('ingredients', 'No ingredients info'),
|
183 |
+
'NutritionalInfo__c': item.get('nutritional_info', 'No nutritional info'),
|
184 |
+
'Price__c': item.get('price', 0.0),
|
185 |
+
'Sector__c': item.get('sector', 'Unknown'),
|
186 |
+
'Spice_Levels__c': item.get('spice_levels', 'Not specified'),
|
187 |
+
'Veg_NonVeg__c': item.get('veg_nonveg', 'Unknown'),
|
188 |
+
'Category__c': item.get('category', 'Unknown'),
|
189 |
+
'Dynamic_Dish__c': item.get('dynamic_dish', False)
|
190 |
+
})
|
191 |
+
else:
|
192 |
+
sf.Ingredient_Object__c.create({
|
193 |
+
'Ingredient_Name__c': item['name'],
|
194 |
+
'Category__c': item.get('category', 'Unknown'),
|
195 |
+
'Description__c': item.get('description', 'No description available')
|
196 |
+
})
|
197 |
+
logger.info(f"Successfully submitted item to {item.get('source', 'Ingredient_Object__c')}: {item['name']}")
|
198 |
return jsonify({"success": f"Successfully submitted {len(items)} items to Salesforce"})
|
199 |
except Exception as e:
|
200 |
logger.error(f"Failed to submit items: {str(e)}")
|