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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -21
app.py CHANGED
@@ -35,42 +35,91 @@ sf = get_salesforce_connection()
35
  def index():
36
  return render_template('index.html')
37
 
38
- @app.route('/get_ingredients', methods=['POST'])
39
- def get_ingredients():
 
40
  global sf
41
  if not sf:
42
  sf = get_salesforce_connection()
43
  if not sf:
44
  return jsonify({"error": "Failed to connect to Salesforce"}), 500
45
 
46
- dietary_preference = request.json.get('dietary_preference', '').lower()
47
-
48
- # Map dietary preference to SOQL condition
49
- preference_map = {
50
- 'vegetarian': "Category__c = 'Veg'",
51
- 'non-vegetarian': "Category__c = 'Non-Veg'",
52
- 'both': None # No condition to fetch all records
53
- }
54
- condition = preference_map.get(dietary_preference)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  try:
57
- # Construct the base query
58
- soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c"
59
- if condition:
60
- soql += f" WHERE {condition}"
61
- soql += " LIMIT 200"
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  logger.info(f"Executing SOQL query: {soql}")
64
  result = sf.query(soql)
65
- ingredients = [
66
  {"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
67
  for record in result['records'] if 'Name' in record
68
  ]
69
- logger.info(f"Fetched {len(ingredients)} ingredients: {ingredients}")
70
- return jsonify({"ingredients": ingredients})
71
  except Exception as e:
72
- logger.error(f"Failed to fetch ingredients: {str(e)}")
73
- return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
74
 
75
  if __name__ == '__main__':
76
  app.run(debug=True, host='0.0.0.0', port=7860)
 
35
  def index():
36
  return render_template('index.html')
37
 
38
+ # Fetch all menu items (names and images) from Salesforce
39
+ @app.route('/get_menu_items', methods=['GET'])
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": "Failed to connect to Salesforce"}), 500
46
 
47
+ try:
48
+ soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c LIMIT 200"
49
+ logger.info(f"Executing SOQL query: {soql}")
50
+ result = sf.query(soql)
51
+ 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'])
63
+ def get_item_details():
64
+ global sf
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)
 
79
 
80
+ if result['totalSize'] == 0:
81
+ return jsonify({"error": f"No item found with name '{item_name}'"}), 404
82
+
83
+ record = result['records'][0]
84
+ item_details = {
85
+ "name": record.get('Name', ''),
86
+ "description": record.get('Description__c', 'No description available'),
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)
114
+ suggestions = [
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)