Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,15 @@
|
|
1 |
-
from flask import Flask, render_template, request, jsonify
|
2 |
from simple_salesforce import Salesforce
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
import logging
|
6 |
|
7 |
-
|
8 |
-
load_dotenv()
|
9 |
|
10 |
-
|
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,55 +18,82 @@ def get_salesforce_connection():
|
|
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 |
-
|
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 |
@app.route('/get_ingredients', methods=['POST'])
|
39 |
def get_ingredients():
|
40 |
-
|
41 |
-
|
42 |
-
sf = get_salesforce_connection()
|
43 |
-
if not sf:
|
44 |
-
return jsonify({"error": "Failed to connect to Salesforce"}), 500
|
45 |
|
46 |
-
dietary_preference
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
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 |
-
|
70 |
return jsonify({"ingredients": ingredients})
|
71 |
except Exception as e:
|
72 |
-
|
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)
|
|
|
1 |
+
from flask import Flask, render_template, send_from_directory, request, jsonify
|
2 |
from simple_salesforce import Salesforce
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
import logging
|
6 |
|
7 |
+
logging.basicConfig(level=logging.DEBUG)
|
|
|
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 |
security_token=os.getenv('SFDC_SECURITY_TOKEN'),
|
19 |
domain=os.getenv('SFDC_DOMAIN', 'login')
|
20 |
)
|
|
|
21 |
return sf
|
22 |
except Exception as e:
|
23 |
+
print(f"Error connecting to Salesforce: {e}")
|
24 |
return None
|
25 |
|
|
|
26 |
sf = get_salesforce_connection()
|
27 |
|
28 |
@app.route('/')
|
29 |
def index():
|
30 |
return render_template('index.html')
|
31 |
|
32 |
+
@app.route('/static/<path:filename>')
|
33 |
+
def serve_static(filename):
|
34 |
+
return send_from_directory('static', filename)
|
35 |
+
|
36 |
@app.route('/get_ingredients', methods=['POST'])
|
37 |
def get_ingredients():
|
38 |
+
dietary_preference = request.json.get('dietary_preference', '').strip().lower()
|
39 |
+
logging.debug(f"Received dietary preference: {dietary_preference}")
|
|
|
|
|
|
|
40 |
|
41 |
+
if dietary_preference == 'vegetable':
|
42 |
+
logging.debug("Fetching ingredients for Vegetables...")
|
43 |
+
soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c = 'Veg' AND Subcategory__c = 'Vegetable' LIMIT 200"
|
44 |
+
elif dietary_preference in ['chicken', 'beef', 'lamb']:
|
45 |
+
logging.debug(f"Fetching ingredients for {dietary_preference}...")
|
46 |
+
soql = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c = 'Non-Veg' AND Subcategory__c = '{dietary_preference.capitalize()}' LIMIT 200"
|
47 |
+
else:
|
48 |
+
logging.debug("Invalid dietary preference received.")
|
49 |
+
return jsonify({"error": "Invalid dietary preference."}), 400
|
50 |
|
51 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
result = sf.query(soql)
|
53 |
ingredients = [
|
54 |
{"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
|
55 |
for record in result['records'] if 'Name' in record
|
56 |
]
|
57 |
+
logging.debug(f"Fetched {len(ingredients)} ingredients.")
|
58 |
return jsonify({"ingredients": ingredients})
|
59 |
except Exception as e:
|
60 |
+
logging.error(f"Error while fetching ingredients: {str(e)}")
|
61 |
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
62 |
|
63 |
+
@app.route('/get_menu_items', methods=['POST'])
|
64 |
+
def get_menu_items():
|
65 |
+
category = request.json.get('category', '').strip().lower()
|
66 |
+
logging.debug(f"Received category: {category}")
|
67 |
+
|
68 |
+
if category == 'fish':
|
69 |
+
logging.debug("Fetching fish-based menu items...")
|
70 |
+
soql = "SELECT Item_Name__c, Image_URL__c FROM Menu_Item__c WHERE Category__c = 'Fish' LIMIT 200"
|
71 |
+
else:
|
72 |
+
logging.debug("Invalid category received.")
|
73 |
+
return jsonify({"error": "Invalid category."}), 400
|
74 |
+
|
75 |
+
try:
|
76 |
+
result = sf.query(soql)
|
77 |
+
menu_items = [
|
78 |
+
{"name": record['Item_Name__c'], "image_url": record.get('Image_URL__c', '')}
|
79 |
+
for record in result['records'] if 'Item_Name__c' in record
|
80 |
+
]
|
81 |
+
logging.debug(f"Fetched {len(menu_items)} menu items.")
|
82 |
+
return jsonify({"menu_items": menu_items})
|
83 |
+
except Exception as e:
|
84 |
+
logging.error(f"Error while fetching menu items: {str(e)}")
|
85 |
+
return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
|
86 |
+
|
87 |
+
@app.route('/submit_ingredients', methods=['POST'])
|
88 |
+
def submit_ingredients():
|
89 |
+
data = request.json
|
90 |
+
ingredients = data.get('ingredients', [])
|
91 |
+
|
92 |
+
if not ingredients:
|
93 |
+
return jsonify({'error': 'No ingredients selected'}), 400
|
94 |
+
|
95 |
+
logging.debug(f"Ingredients submitted: {ingredients}")
|
96 |
+
return jsonify({'success': True})
|
97 |
+
|
98 |
if __name__ == '__main__':
|
99 |
app.run(debug=True, host='0.0.0.0', port=7860)
|