Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,16 @@ from dotenv import load_dotenv
|
|
4 |
import os
|
5 |
import logging
|
6 |
|
7 |
-
|
8 |
-
|
9 |
load_dotenv()
|
10 |
|
|
|
|
|
|
|
|
|
11 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
12 |
|
|
|
13 |
def get_salesforce_connection():
|
14 |
try:
|
15 |
sf = Salesforce(
|
@@ -18,11 +22,13 @@ def get_salesforce_connection():
|
|
18 |
security_token=os.getenv('SFDC_SECURITY_TOKEN'),
|
19 |
domain=os.getenv('SFDC_DOMAIN', 'login')
|
20 |
)
|
|
|
21 |
return sf
|
22 |
except Exception as e:
|
23 |
-
|
24 |
return None
|
25 |
|
|
|
26 |
sf = get_salesforce_connection()
|
27 |
|
28 |
@app.route('/')
|
@@ -35,35 +41,51 @@ def serve_static(filename):
|
|
35 |
|
36 |
@app.route('/get_ingredients', methods=['POST'])
|
37 |
def get_ingredients():
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
|
51 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
result = sf.query(soql)
|
53 |
ingredients = [
|
54 |
{"name": record['Name'], "image_url": record.get('Image_URL__c', ''), "category": record.get('Category__c', '')}
|
55 |
for record in result['records'] if 'Name' in record
|
56 |
]
|
57 |
-
|
58 |
return jsonify({"ingredients": ingredients})
|
59 |
except Exception as e:
|
60 |
-
|
61 |
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
62 |
|
63 |
@app.route('/get_menu_items_by_ingredients', methods=['POST'])
|
64 |
def get_menu_items_by_ingredients():
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
ingredients = request.json.get('ingredients', '').strip().lower()
|
66 |
-
|
67 |
|
68 |
# Assuming Menu_Items__c object has an Ingredients__c field to match with selected ingredients
|
69 |
soql = f"SELECT Item_Name__c, Image_URL__c FROM Menu_Items__c WHERE Ingredients__c LIKE '%{ingredients}%' LIMIT 200"
|
@@ -74,10 +96,10 @@ def get_menu_items_by_ingredients():
|
|
74 |
{"name": record['Item_Name__c'], "image_url": record.get('Image_URL__c', '')}
|
75 |
for record in result['records'] if 'Item_Name__c' in record
|
76 |
]
|
77 |
-
|
78 |
return jsonify({"menu_items": menu_items})
|
79 |
except Exception as e:
|
80 |
-
|
81 |
return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
|
82 |
|
83 |
@app.route('/submit_ingredients', methods=['POST'])
|
@@ -88,8 +110,8 @@ def submit_ingredients():
|
|
88 |
if not ingredients:
|
89 |
return jsonify({'error': 'No ingredients selected'}), 400
|
90 |
|
91 |
-
|
92 |
return jsonify({'success': True})
|
93 |
|
94 |
if __name__ == '__main__':
|
95 |
-
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
4 |
import os
|
5 |
import logging
|
6 |
|
7 |
+
# Load environment variables from .env file
|
|
|
8 |
load_dotenv()
|
9 |
|
10 |
+
# Set up logging
|
11 |
+
logging.basicConfig(level=logging.INFO)
|
12 |
+
logger = logging.getLogger(__name__)
|
13 |
+
|
14 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
15 |
|
16 |
+
# Function to get Salesforce connection
|
17 |
def get_salesforce_connection():
|
18 |
try:
|
19 |
sf = Salesforce(
|
|
|
22 |
security_token=os.getenv('SFDC_SECURITY_TOKEN'),
|
23 |
domain=os.getenv('SFDC_DOMAIN', 'login')
|
24 |
)
|
25 |
+
logger.info("Successfully connected to Salesforce")
|
26 |
return sf
|
27 |
except Exception as e:
|
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('/')
|
|
|
41 |
|
42 |
@app.route('/get_ingredients', methods=['POST'])
|
43 |
def get_ingredients():
|
44 |
+
global sf
|
45 |
+
if not sf:
|
46 |
+
sf = get_salesforce_connection()
|
47 |
+
if not sf:
|
48 |
+
return jsonify({"error": "Failed to connect to Salesforce"}), 500
|
49 |
+
|
50 |
+
dietary_preference = request.json.get('dietary_preference', '').lower()
|
51 |
+
|
52 |
+
# Map dietary preference to SOQL condition
|
53 |
+
preference_map = {
|
54 |
+
'vegetarian': "Category__c = 'Veg'",
|
55 |
+
'non-vegetarian': "Category__c = 'Non-Veg'",
|
56 |
+
'both': None # No condition to fetch all records
|
57 |
+
}
|
58 |
+
condition = preference_map.get(dietary_preference)
|
59 |
|
60 |
try:
|
61 |
+
# Construct the base query
|
62 |
+
soql = "SELECT Name, Image_URL__c, Category__c FROM Sector_Detail__c"
|
63 |
+
if condition:
|
64 |
+
soql += f" WHERE {condition}"
|
65 |
+
soql += " LIMIT 200"
|
66 |
+
|
67 |
+
logger.info(f"Executing SOQL query: {soql}")
|
68 |
result = sf.query(soql)
|
69 |
ingredients = [
|
70 |
{"name": record['Name'], "image_url": record.get('Image_URL__c', ''), "category": record.get('Category__c', '')}
|
71 |
for record in result['records'] if 'Name' in record
|
72 |
]
|
73 |
+
logger.info(f"Fetched {len(ingredients)} ingredients: {[ing['name'] + ' (' + ing['category'] + ')' for ing in ingredients]}")
|
74 |
return jsonify({"ingredients": ingredients})
|
75 |
except Exception as e:
|
76 |
+
logger.error(f"Failed to fetch ingredients: {str(e)}")
|
77 |
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
78 |
|
79 |
@app.route('/get_menu_items_by_ingredients', methods=['POST'])
|
80 |
def get_menu_items_by_ingredients():
|
81 |
+
global sf
|
82 |
+
if not sf:
|
83 |
+
sf = get_salesforce_connection()
|
84 |
+
if not sf:
|
85 |
+
return jsonify({"error": "Failed to connect to Salesforce"}), 500
|
86 |
+
|
87 |
ingredients = request.json.get('ingredients', '').strip().lower()
|
88 |
+
logger.info(f"Received ingredients: {ingredients}")
|
89 |
|
90 |
# Assuming Menu_Items__c object has an Ingredients__c field to match with selected ingredients
|
91 |
soql = f"SELECT Item_Name__c, Image_URL__c FROM Menu_Items__c WHERE Ingredients__c LIKE '%{ingredients}%' LIMIT 200"
|
|
|
96 |
{"name": record['Item_Name__c'], "image_url": record.get('Image_URL__c', '')}
|
97 |
for record in result['records'] if 'Item_Name__c' in record
|
98 |
]
|
99 |
+
logger.info(f"Fetched {len(menu_items)} menu items.")
|
100 |
return jsonify({"menu_items": menu_items})
|
101 |
except Exception as e:
|
102 |
+
logger.error(f"Error while fetching menu items: {str(e)}")
|
103 |
return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
|
104 |
|
105 |
@app.route('/submit_ingredients', methods=['POST'])
|
|
|
110 |
if not ingredients:
|
111 |
return jsonify({'error': 'No ingredients selected'}), 400
|
112 |
|
113 |
+
logger.info(f"Ingredients submitted: {ingredients}")
|
114 |
return jsonify({'success': True})
|
115 |
|
116 |
if __name__ == '__main__':
|
117 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|