lokesh341 commited on
Commit
20c4a07
·
verified ·
1 Parent(s): b189fed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -18
app.py CHANGED
@@ -52,29 +52,58 @@ def get_menu_items():
52
  try:
53
  items = []
54
  # Query Sector_Detail__c for dietary preferences
55
- soql_sector = "SELECT Name, Image_URL__c, Category__c, Description__c FROM Sector_Detail__c"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  if search_term:
57
- soql_sector += f" WHERE Name LIKE '%{search_term}%'"
58
  elif dietary_preference == 'vegetarian':
59
- soql_sector += " WHERE Category__c = 'Veg'"
60
  elif dietary_preference == 'non-vegetarian':
61
- soql_sector += " WHERE Category__c = 'Non-Veg'"
62
- soql_sector += " LIMIT 200"
63
- logger.info(f"Executing SOQL query for Sector_Detail__c: {soql_sector}")
64
- result_sector = sf.query(soql_sector)
65
- sector_items = [
66
  {
67
  "name": record['Name'],
68
- "image_url": record.get('Image_URL__c', ''),
69
- "category": record.get('Category__c', ''),
70
  "description": record.get('Description__c', 'No description available'),
71
- "source": "Sector_Detail__c"
 
 
 
 
 
 
 
 
 
72
  }
73
- for record in result_sector['records'] if 'Name' in record
74
  ]
75
- items.extend(sector_items)
76
 
77
- logger.info(f"Fetched {len(items)} items from Sector_Detail__c for {dietary_preference or search_term}")
78
  return jsonify({"menu_items": items})
79
  except Exception as e:
80
  logger.error(f"Failed to fetch items: {str(e)}")
@@ -114,6 +143,64 @@ def get_sector_item_details():
114
  logger.error(f"Failed to fetch item details from Sector_Detail__c: {str(e)}")
115
  return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  @app.route('/submit_items', methods=['POST'])
118
  def submit_items():
119
  global sf
@@ -133,13 +220,10 @@ def submit_items():
133
  description = f"Contains: {item_names}"
134
 
135
  for item in items:
136
- custom_desc = f"{item.get('description', 'No description')} - {description}"
137
- if item.get('custom_ingredients'):
138
- custom_desc += f" | Custom Ingredients: {item['custom_ingredients']}"
139
  sf.Ingredient_Object__c.create({
140
  'Ingredient_Name__c': ingredient_name,
141
  'Category__c': item.get('category', ''),
142
- 'Description__c': custom_desc,
143
  'Image_URL__c': item.get('image_url', ''),
144
  'Quantity__c': item.get('quantity', 1)
145
  })
 
52
  try:
53
  items = []
54
  # Query Sector_Detail__c for dietary preferences
55
+ if not search_term:
56
+ soql_sector = "SELECT Name, Image_URL__c, Category__c, Description__c FROM Sector_Detail__c"
57
+ if dietary_preference == 'vegetarian':
58
+ soql_sector += " WHERE Category__c = 'Veg'"
59
+ elif dietary_preference == 'non-vegetarian':
60
+ soql_sector += " WHERE Category__c = 'Non-Veg'"
61
+ soql_sector += " LIMIT 200"
62
+ logger.info(f"Executing SOQL query for Sector_Detail__c: {soql_sector}")
63
+ result_sector = sf.query(soql_sector)
64
+ sector_items = [
65
+ {
66
+ "name": record['Name'],
67
+ "image_url": record.get('Image_URL__c', ''),
68
+ "category": record.get('Category__c', ''),
69
+ "description": record.get('Description__c', 'No description available'),
70
+ "source": "Sector_Detail__c"
71
+ }
72
+ for record in result_sector['records'] if 'Name' in record
73
+ ]
74
+ items.extend(sector_items)
75
+
76
+ # Query Menu_Item__c
77
+ soql_menu = "SELECT Name, Description__c, Image1__c, Ingredientsinfo__c, NutritionalInfo__c, Price__c, Sector__c, Spice_Levels__c, Veg_NonVeg__c, Category__c, Dynamic_Dish__c FROM Menu_Item__c"
78
  if search_term:
79
+ soql_menu += f" WHERE Name LIKE '%{search_term}%'"
80
  elif dietary_preference == 'vegetarian':
81
+ soql_menu += " WHERE Veg_NonVeg__c = 'Vegetarian'"
82
  elif dietary_preference == 'non-vegetarian':
83
+ soql_menu += " WHERE Veg_NonVeg__c = 'Non-Vegetarian'"
84
+ soql_menu += " LIMIT 200"
85
+ logger.info(f"Executing SOQL query for Menu_Item__c: {soql_menu}")
86
+ result_menu = sf.query(soql_menu)
87
+ menu_items = [
88
  {
89
  "name": record['Name'],
 
 
90
  "description": record.get('Description__c', 'No description available'),
91
+ "image_url": record.get('Image1__c', ''),
92
+ "ingredients": record.get('Ingredientsinfo__c', ''),
93
+ "nutritional_info": record.get('NutritionalInfo__c', ''),
94
+ "price": record.get('Price__c', 0.0),
95
+ "sector": record.get('Sector__c', ''),
96
+ "spice_levels": record.get('Spice_Levels__c', ''),
97
+ "veg_nonveg": record.get('Veg_NonVeg__c', ''),
98
+ "category": record.get('Category__c', ''),
99
+ "dynamic_dish": record.get('Dynamic_Dish__c', False),
100
+ "source": "Menu_Item__c"
101
  }
102
+ for record in result_menu['records'] if 'Name' in record
103
  ]
104
+ items.extend(menu_items)
105
 
106
+ logger.info(f"Fetched {len(items)} items (Sector_Detail__c: {len([i for i in items if i['source'] == 'Sector_Detail__c'])}, Menu_Item__c: {len([i for i in items if i['source'] == 'Menu_Item__c'])}) for {dietary_preference or search_term}")
107
  return jsonify({"menu_items": items})
108
  except Exception as e:
109
  logger.error(f"Failed to fetch items: {str(e)}")
 
143
  logger.error(f"Failed to fetch item details from Sector_Detail__c: {str(e)}")
144
  return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500
145
 
146
+ @app.route('/suggest_items', methods=['POST'])
147
+ def suggest_items():
148
+ global sf
149
+ if not sf:
150
+ sf = get_salesforce_connection()
151
+ if not sf:
152
+ return jsonify({"error": "Unable to connect to Salesforce"}), 500
153
+
154
+ search_term = request.json.get('search_term', '').strip()
155
+ if not search_term:
156
+ return jsonify({"error": "Search term is required"}), 400
157
+
158
+ try:
159
+ soql = f"SELECT Ingredient_Name__c, Image_URL__c FROM Ingredient_Object__c WHERE Ingredient_Name__c LIKE '%{search_term}%' LIMIT 10"
160
+ logger.info(f"Executing SOQL query: {soql}")
161
+ result = sf.query(soql)
162
+ suggestions = [
163
+ {"name": record['Ingredient_Name__c'], "image_url": record.get('Image_URL__c', '')}
164
+ for record in result['records'] if 'Ingredient_Name__c' in record
165
+ ]
166
+ logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
167
+ return jsonify({"suggestions": suggestions})
168
+ except Exception as e:
169
+ logger.error(f"Failed to fetch suggestions: {str(e)}")
170
+ return jsonify({"error": f"Failed to fetch suggestions: {str(e)}"}), 500
171
+
172
+ @app.route('/get_item_details', methods=['POST'])
173
+ def get_item_details():
174
+ global sf
175
+ if not sf:
176
+ sf = get_salesforce_connection()
177
+ if not sf:
178
+ return jsonify({"error": "Unable to connect to Salesforce"}), 500
179
+
180
+ item_name = request.json.get('item_name', '').strip()
181
+ if not item_name:
182
+ return jsonify({"error": "Item name is required"}), 400
183
+
184
+ try:
185
+ soql = f"SELECT Ingredient_Name__c, Image_URL__c FROM Ingredient_Object__c WHERE Ingredient_Name__c LIKE '%{item_name}%' LIMIT 1"
186
+ logger.info(f"Executing SOQL query: {soql}")
187
+ result = sf.query(soql)
188
+
189
+ if result['totalSize'] == 0:
190
+ return jsonify({"error": f"No item found matching '{item_name}' in Ingredient_Object__c"}), 404
191
+
192
+ record = result['records'][0]
193
+ item_details = {
194
+ "name": record.get('Ingredient_Name__c', ''),
195
+ "image_url": record.get('Image_URL__c', ''),
196
+ "source": "Ingredient_Object__c"
197
+ }
198
+ logger.info(f"Fetched details for '{item_name}' from Ingredient_Object__c")
199
+ return jsonify({"item_details": item_details})
200
+ except Exception as e:
201
+ logger.error(f"Failed to fetch item details: {str(e)}")
202
+ return jsonify({"error": f"Failed to fetch item details: {str(e)}"}), 500
203
+
204
  @app.route('/submit_items', methods=['POST'])
205
  def submit_items():
206
  global sf
 
220
  description = f"Contains: {item_names}"
221
 
222
  for item in items:
 
 
 
223
  sf.Ingredient_Object__c.create({
224
  'Ingredient_Name__c': ingredient_name,
225
  'Category__c': item.get('category', ''),
226
+ 'Description__c': f"{item.get('description', 'No description')} - {description}",
227
  'Image_URL__c': item.get('image_url', ''),
228
  'Quantity__c': item.get('quantity', 1)
229
  })