lokesh341 commited on
Commit
567f5b5
·
verified ·
1 Parent(s): 427c1b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -13
app.py CHANGED
@@ -28,7 +28,7 @@ def get_salesforce_connection():
28
  logger.error(f"Error connecting to Salesforce: {e}")
29
  return None
30
 
31
- # Initialize Salesforce connection
32
  sf = get_salesforce_connection()
33
 
34
  @app.route('/')
@@ -42,7 +42,7 @@ def get_menu_items():
42
  if not sf:
43
  sf = get_salesforce_connection()
44
  if not sf:
45
- return jsonify({"error": "Failed to connect to Salesforce"}), 500
46
 
47
  try:
48
  soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c LIMIT 200"
@@ -52,11 +52,11 @@ def get_menu_items():
52
  {"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
53
  for record in result['records'] if 'Name' in record
54
  ]
55
- logger.info(f"Fetched {len(menu_items)} menu items: {menu_items}")
56
  return jsonify({"menu_items": menu_items})
57
  except Exception as e:
58
  logger.error(f"Failed to fetch menu items: {str(e)}")
59
- return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
60
 
61
  # Fetch details (description and ingredients) for a specific menu item
62
  @app.route('/get_item_details', methods=['POST'])
@@ -65,14 +65,13 @@ def get_item_details():
65
  if not sf:
66
  sf = get_salesforce_connection()
67
  if not sf:
68
- return jsonify({"error": "Failed to connect to Salesforce"}), 500
69
 
70
  item_name = request.json.get('item_name', '').strip()
71
  if not item_name:
72
  return jsonify({"error": "Item name is required"}), 400
73
 
74
  try:
75
- # Query Salesforce for the specific item
76
  soql = f"SELECT Name, Description__c, Ingredients__c, Image_URL__c FROM Sector_Detail__c WHERE Name = '{item_name}' LIMIT 1"
77
  logger.info(f"Executing SOQL query: {soql}")
78
  result = sf.query(soql)
@@ -87,27 +86,26 @@ def get_item_details():
87
  "ingredients": record.get('Ingredients__c', 'No ingredients listed'),
88
  "image_url": record.get('Image_URL__c', '')
89
  }
90
- logger.info(f"Fetched details for {item_name}: {item_details}")
91
  return jsonify({"item_details": item_details})
92
  except Exception as e:
93
  logger.error(f"Failed to fetch item details: {str(e)}")
94
- return jsonify({"error": f"Failed to fetch item details: {str(e)}"}), 500
95
 
96
- # Suggest suitable items based on user input (e.g., "chicken")
97
  @app.route('/suggest_items', methods=['POST'])
98
  def suggest_items():
99
  global sf
100
  if not sf:
101
  sf = get_salesforce_connection()
102
  if not sf:
103
- return jsonify({"error": "Failed to connect to Salesforce"}), 500
104
 
105
  search_term = request.json.get('search_term', '').strip().lower()
106
  if not search_term:
107
  return jsonify({"error": "Search term is required"}), 400
108
 
109
  try:
110
- # Search for items where Name or Ingredients__c contains the search term
111
  soql = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Name LIKE '%{search_term}%' OR Ingredients__c LIKE '%{search_term}%' LIMIT 10"
112
  logger.info(f"Executing SOQL query: {soql}")
113
  result = sf.query(soql)
@@ -115,11 +113,11 @@ def suggest_items():
115
  {"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
116
  for record in result['records'] if 'Name' in record
117
  ]
118
- logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}': {suggestions}")
119
  return jsonify({"suggestions": suggestions})
120
  except Exception as e:
121
  logger.error(f"Failed to fetch suggestions: {str(e)}")
122
- return jsonify({"error": f"Failed to fetch suggestions: {str(e)}"}), 500
123
 
124
  if __name__ == '__main__':
125
  app.run(debug=True, host='0.0.0.0', port=7860)
 
28
  logger.error(f"Error connecting to Salesforce: {e}")
29
  return None
30
 
31
+ # Initialize Salesforce connection globally
32
  sf = get_salesforce_connection()
33
 
34
  @app.route('/')
 
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
  try:
48
  soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c LIMIT 200"
 
52
  {"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
53
  for record in result['records'] if 'Name' in record
54
  ]
55
+ logger.info(f"Fetched {len(menu_items)} menu items")
56
  return jsonify({"menu_items": menu_items})
57
  except Exception as e:
58
  logger.error(f"Failed to fetch menu items: {str(e)}")
59
+ return jsonify({"error": "Failed to fetch menu items from Salesforce"}), 500
60
 
61
  # Fetch details (description and ingredients) for a specific menu item
62
  @app.route('/get_item_details', methods=['POST'])
 
65
  if not sf:
66
  sf = get_salesforce_connection()
67
  if not sf:
68
+ return jsonify({"error": "Unable to connect to Salesforce"}), 500
69
 
70
  item_name = request.json.get('item_name', '').strip()
71
  if not item_name:
72
  return jsonify({"error": "Item name is required"}), 400
73
 
74
  try:
 
75
  soql = f"SELECT Name, Description__c, Ingredients__c, Image_URL__c FROM Sector_Detail__c WHERE Name = '{item_name}' LIMIT 1"
76
  logger.info(f"Executing SOQL query: {soql}")
77
  result = sf.query(soql)
 
86
  "ingredients": record.get('Ingredients__c', 'No ingredients listed'),
87
  "image_url": record.get('Image_URL__c', '')
88
  }
89
+ logger.info(f"Fetched details for {item_name}")
90
  return jsonify({"item_details": item_details})
91
  except Exception as e:
92
  logger.error(f"Failed to fetch item details: {str(e)}")
93
+ return jsonify({"error": "Failed to fetch item details from Salesforce"}), 500
94
 
95
+ # Suggest suitable items based on user input
96
  @app.route('/suggest_items', methods=['POST'])
97
  def suggest_items():
98
  global sf
99
  if not sf:
100
  sf = get_salesforce_connection()
101
  if not sf:
102
+ return jsonify({"error": "Unable to connect to Salesforce"}), 500
103
 
104
  search_term = request.json.get('search_term', '').strip().lower()
105
  if not search_term:
106
  return jsonify({"error": "Search term is required"}), 400
107
 
108
  try:
 
109
  soql = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Name LIKE '%{search_term}%' OR Ingredients__c LIKE '%{search_term}%' LIMIT 10"
110
  logger.info(f"Executing SOQL query: {soql}")
111
  result = sf.query(soql)
 
113
  {"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
114
  for record in result['records'] if 'Name' in record
115
  ]
116
+ logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
117
  return jsonify({"suggestions": suggestions})
118
  except Exception as e:
119
  logger.error(f"Failed to fetch suggestions: {str(e)}")
120
+ return jsonify({"error": "Failed to fetch suggestions from Salesforce"}), 500
121
 
122
  if __name__ == '__main__':
123
  app.run(debug=True, host='0.0.0.0', port=7860)