Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ from dotenv import load_dotenv
|
|
4 |
import os
|
5 |
import logging
|
6 |
|
7 |
-
# Load environment variables
|
8 |
load_dotenv()
|
9 |
|
10 |
# Set up logging
|
@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
|
|
13 |
|
14 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
15 |
|
16 |
-
#
|
17 |
def get_salesforce_connection():
|
18 |
try:
|
19 |
sf = Salesforce(
|
@@ -28,15 +28,15 @@ 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('/')
|
35 |
def index():
|
36 |
return render_template('index.html')
|
37 |
|
38 |
-
# Fetch
|
39 |
-
@app.route('/get_menu_items', methods=['
|
40 |
def get_menu_items():
|
41 |
global sf
|
42 |
if not sf:
|
@@ -44,21 +44,59 @@ def get_menu_items():
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
logger.info(f"Executing SOQL query: {soql}")
|
50 |
result = sf.query(soql)
|
51 |
menu_items = [
|
52 |
-
{
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
@app.route('/get_item_details', methods=['POST'])
|
63 |
def get_item_details():
|
64 |
global sf
|
@@ -72,12 +110,26 @@ def get_item_details():
|
|
72 |
return jsonify({"error": "Item name is required"}), 400
|
73 |
|
74 |
try:
|
75 |
-
|
|
|
76 |
logger.info(f"Executing SOQL query: {soql}")
|
77 |
result = sf.query(soql)
|
78 |
|
79 |
if result['totalSize'] == 0:
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
record = result['records'][0]
|
83 |
item_details = {
|
@@ -86,38 +138,11 @@ def get_item_details():
|
|
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)
|
112 |
-
suggestions = [
|
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)
|
|
|
4 |
import os
|
5 |
import logging
|
6 |
|
7 |
+
# Load environment variables
|
8 |
load_dotenv()
|
9 |
|
10 |
# Set up logging
|
|
|
13 |
|
14 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
15 |
|
16 |
+
# Salesforce connection function
|
17 |
def get_salesforce_connection():
|
18 |
try:
|
19 |
sf = Salesforce(
|
|
|
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('/')
|
35 |
def index():
|
36 |
return render_template('index.html')
|
37 |
|
38 |
+
# Fetch menu items with dietary filter
|
39 |
+
@app.route('/get_menu_items', methods=['POST'])
|
40 |
def get_menu_items():
|
41 |
global sf
|
42 |
if not sf:
|
|
|
44 |
if not sf:
|
45 |
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
46 |
|
47 |
+
dietary_preference = request.json.get('dietary_preference', 'both').lower()
|
48 |
try:
|
49 |
+
soql = "SELECT Name, Image_URL__c, Category__c FROM Sector_Detail__c"
|
50 |
+
if dietary_preference == 'vegetarian':
|
51 |
+
soql += " WHERE Category__c = 'Veg'"
|
52 |
+
elif dietary_preference == 'non-vegetarian':
|
53 |
+
soql += " WHERE Category__c = 'Non-Veg'"
|
54 |
+
soql += " LIMIT 200"
|
55 |
+
|
56 |
logger.info(f"Executing SOQL query: {soql}")
|
57 |
result = sf.query(soql)
|
58 |
menu_items = [
|
59 |
+
{
|
60 |
+
"name": record['Name'],
|
61 |
+
"image_url": record.get('Image_URL__c', ''),
|
62 |
+
"category": record.get('Category__c', '')
|
63 |
+
}
|
64 |
for record in result['records'] if 'Name' in record
|
65 |
]
|
66 |
+
logger.info(f"Fetched {len(menu_items)} menu items for {dietary_preference}")
|
67 |
return jsonify({"menu_items": menu_items})
|
68 |
except Exception as e:
|
69 |
logger.error(f"Failed to fetch menu items: {str(e)}")
|
70 |
return jsonify({"error": "Failed to fetch menu items from Salesforce"}), 500
|
71 |
|
72 |
+
# Fetch suggestions based on search term
|
73 |
+
@app.route('/suggest_items', methods=['POST'])
|
74 |
+
def suggest_items():
|
75 |
+
global sf
|
76 |
+
if not sf:
|
77 |
+
sf = get_salesforce_connection()
|
78 |
+
if not sf:
|
79 |
+
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
80 |
+
|
81 |
+
search_term = request.json.get('search_term', '').strip()
|
82 |
+
if not search_term:
|
83 |
+
return jsonify({"error": "Search term is required"}), 400
|
84 |
+
|
85 |
+
try:
|
86 |
+
soql = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Name LIKE '%{search_term}%' OR Ingredients__c LIKE '%{search_term}%' LIMIT 10"
|
87 |
+
logger.info(f"Executing SOQL query: {soql}")
|
88 |
+
result = sf.query(soql)
|
89 |
+
suggestions = [
|
90 |
+
{"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
|
91 |
+
for record in result['records'] if 'Name' in record
|
92 |
+
]
|
93 |
+
logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
|
94 |
+
return jsonify({"suggestions": suggestions})
|
95 |
+
except Exception as e:
|
96 |
+
logger.error(f"Failed to fetch suggestions: {str(e)}")
|
97 |
+
return jsonify({"error": "Failed to fetch suggestions from Salesforce"}), 500
|
98 |
+
|
99 |
+
# Fetch item details with fallback to menu items
|
100 |
@app.route('/get_item_details', methods=['POST'])
|
101 |
def get_item_details():
|
102 |
global sf
|
|
|
110 |
return jsonify({"error": "Item name is required"}), 400
|
111 |
|
112 |
try:
|
113 |
+
# Case-insensitive search for exact or partial match
|
114 |
+
soql = f"SELECT Name, Description__c, Ingredients__c, Image_URL__c FROM Sector_Detail__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
115 |
logger.info(f"Executing SOQL query: {soql}")
|
116 |
result = sf.query(soql)
|
117 |
|
118 |
if result['totalSize'] == 0:
|
119 |
+
# Fallback: Check if it exists in menu items
|
120 |
+
soql_fallback = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
121 |
+
result_fallback = sf.query(soql_fallback)
|
122 |
+
if result_fallback['totalSize'] > 0:
|
123 |
+
record = result_fallback['records'][0]
|
124 |
+
item_details = {
|
125 |
+
"name": record.get('Name', ''),
|
126 |
+
"description": "Description not available",
|
127 |
+
"ingredients": "Ingredients not listed",
|
128 |
+
"image_url": record.get('Image_URL__c', '')
|
129 |
+
}
|
130 |
+
logger.info(f"Fallback: Found '{item_name}' in menu items")
|
131 |
+
return jsonify({"item_details": item_details})
|
132 |
+
return jsonify({"error": f"No item found matching '{item_name}'"}), 404
|
133 |
|
134 |
record = result['records'][0]
|
135 |
item_details = {
|
|
|
138 |
"ingredients": record.get('Ingredients__c', 'No ingredients listed'),
|
139 |
"image_url": record.get('Image_URL__c', '')
|
140 |
}
|
141 |
+
logger.info(f"Fetched details for '{item_name}'")
|
142 |
return jsonify({"item_details": item_details})
|
143 |
except Exception as e:
|
144 |
logger.error(f"Failed to fetch item details: {str(e)}")
|
145 |
return jsonify({"error": "Failed to fetch item details from Salesforce"}), 500
|
146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
if __name__ == '__main__':
|
148 |
app.run(debug=True, host='0.0.0.0', port=7860)
|