lokesh341 commited on
Commit
8641b06
·
verified ·
1 Parent(s): 2e3347b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -35,7 +35,7 @@ sf = get_salesforce_connection()
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
@@ -45,13 +45,19 @@ def get_menu_items():
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, Description__c FROM Sector_Detail__c"
51
- if dietary_preference == 'vegetarian':
52
- soql += " WHERE Category__c = 'Veg'"
53
- elif dietary_preference == 'non-vegetarian':
54
- soql += " WHERE Category__c = 'Non-Veg'"
 
 
 
55
  soql += " LIMIT 200"
56
 
57
  logger.info(f"Executing SOQL query: {soql}")
@@ -65,10 +71,10 @@ def get_menu_items():
65
  }
66
  for record in result['records'] if 'Name' in record
67
  ]
68
- logger.info(f"Fetched {len(items)} items from Sector_Detail__c for {dietary_preference}")
69
  return jsonify({"menu_items": items})
70
  except Exception as e:
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
@@ -163,7 +169,7 @@ def get_item_details_by_name():
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 and Image_URL) from Sector_Detail__c by name
167
  @app.route('/get_sector_item_details', methods=['POST'])
168
  def get_sector_item_details():
169
  global sf
@@ -177,7 +183,7 @@ def get_sector_item_details():
177
  return jsonify({"error": "Item name is required"}), 400
178
 
179
  try:
180
- soql = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
181
  logger.info(f"Executing SOQL query: {soql}")
182
  result = sf.query(soql)
183
 
@@ -187,9 +193,11 @@ def get_sector_item_details():
187
  record = result['records'][0]
188
  item_details = {
189
  "name": record.get('Name', ''),
190
- "image_url": record.get('Image_URL__c', 'https://via.placeholder.com/30')
 
 
191
  }
192
- logger.info(f"Fetched details for '{item_name}' from Sector_Detail__c")
193
  return jsonify({"item_details": item_details})
194
  except Exception as e:
195
  logger.error(f"Failed to fetch item details from Sector_Detail__c: {str(e)}")
@@ -210,9 +218,10 @@ def submit_items():
210
 
211
  try:
212
  for item in items:
213
- logger.info(f"Submitting item: {item}")
214
  sf.Ingredient_Object__c.create({
215
  'Ingredient_Name__c': item['name'],
 
216
  'Description__c': item.get('description', 'No description available')
217
  })
218
  logger.info(f"Successfully submitted item to Ingredient_Object__c: {item['name']}")
 
35
  def index():
36
  return render_template('index.html')
37
 
38
+ # Fetch items from Sector_Detail__c with search support
39
  @app.route('/get_menu_items', methods=['POST'])
40
  def get_menu_items():
41
  global sf
 
45
  logger.error("Salesforce connection failed after retry")
46
  return jsonify({"error": "Unable to connect to Salesforce"}), 500
47
 
48
+ data = request.json
49
+ dietary_preference = data.get('dietary_preference', 'both').lower()
50
+ search_term = data.get('search_term', '').strip()
51
+
52
  try:
53
  soql = "SELECT Name, Image_URL__c, Category__c, Description__c FROM Sector_Detail__c"
54
+ conditions = []
55
+ if search_term:
56
+ conditions.append(f"Name LIKE '%{search_term}%'")
57
+ if dietary_preference in ['vegetarian', 'non-vegetarian']:
58
+ conditions.append(f"Category__c = '{dietary_preference.capitalize()}'")
59
+ if conditions:
60
+ soql += " WHERE " + " AND ".join(conditions)
61
  soql += " LIMIT 200"
62
 
63
  logger.info(f"Executing SOQL query: {soql}")
 
71
  }
72
  for record in result['records'] if 'Name' in record
73
  ]
74
+ logger.info(f"Fetched {len(items)} items from Sector_Detail__c for {dietary_preference} with search '{search_term}'")
75
  return jsonify({"menu_items": items})
76
  except Exception as e:
77
+ logger.error(f"Failed to fetch items from Sector_Detail__c: {e}")
78
  return jsonify({"error": f"Failed to fetch items from Salesforce: {str(e)}"}), 500
79
 
80
  # Fetch suggestions from Ingredient_Object__c
 
169
  logger.error(f"Failed to fetch item details from Sector_Detail__c: {str(e)}")
170
  return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500
171
 
172
+ # Fetch item details (Name, Image_URL, Category, Description) from Sector_Detail__c by name
173
  @app.route('/get_sector_item_details', methods=['POST'])
174
  def get_sector_item_details():
175
  global sf
 
183
  return jsonify({"error": "Item name is required"}), 400
184
 
185
  try:
186
+ soql = f"SELECT Name, Image_URL__c, Category__c, Description__c FROM Sector_Detail__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
187
  logger.info(f"Executing SOQL query: {soql}")
188
  result = sf.query(soql)
189
 
 
193
  record = result['records'][0]
194
  item_details = {
195
  "name": record.get('Name', ''),
196
+ "image_url": record.get('Image_URL__c', 'https://via.placeholder.com/30'),
197
+ "category": record.get('Category__c', 'Unknown'),
198
+ "description": record.get('Description__c', 'No description available')
199
  }
200
+ logger.info(f"Fetched details for '{item_name}' from Sector_Detail__c: {item_details}")
201
  return jsonify({"item_details": item_details})
202
  except Exception as e:
203
  logger.error(f"Failed to fetch item details from Sector_Detail__c: {str(e)}")
 
218
 
219
  try:
220
  for item in items:
221
+ logger.info(f"Submitting item to Ingredient_Object__c: {item}")
222
  sf.Ingredient_Object__c.create({
223
  'Ingredient_Name__c': item['name'],
224
+ 'Category__c': item.get('category', 'Unknown'), # Explicitly set Category__c
225
  'Description__c': item.get('description', 'No description available')
226
  })
227
  logger.info(f"Successfully submitted item to Ingredient_Object__c: {item['name']}")