Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,15 @@ from flask import Flask, render_template, request, jsonify
|
|
| 2 |
from simple_salesforce import Salesforce
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
# Load environment variables from .env file
|
| 7 |
load_dotenv()
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 10 |
|
| 11 |
# Function to get Salesforce connection
|
|
@@ -17,12 +22,13 @@ def get_salesforce_connection():
|
|
| 17 |
security_token=os.getenv('SFDC_SECURITY_TOKEN'),
|
| 18 |
domain=os.getenv('SFDC_DOMAIN', 'login')
|
| 19 |
)
|
|
|
|
| 20 |
return sf
|
| 21 |
except Exception as e:
|
| 22 |
-
|
| 23 |
return None
|
| 24 |
|
| 25 |
-
# Initialize Salesforce connection
|
| 26 |
sf = get_salesforce_connection()
|
| 27 |
|
| 28 |
@app.route('/')
|
|
@@ -43,19 +49,27 @@ def get_ingredients():
|
|
| 43 |
preference_map = {
|
| 44 |
'vegetarian': "Category__c = 'Veg'",
|
| 45 |
'non-vegetarian': "Category__c = 'Non-Veg'",
|
| 46 |
-
'both':
|
| 47 |
}
|
| 48 |
-
condition = preference_map.get(dietary_preference
|
| 49 |
|
| 50 |
try:
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
return jsonify({"ingredients": ingredients})
|
| 58 |
except Exception as e:
|
|
|
|
| 59 |
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
| 60 |
|
| 61 |
if __name__ == '__main__':
|
|
|
|
| 2 |
from simple_salesforce import Salesforce
|
| 3 |
from dotenv import load_dotenv
|
| 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
|
|
|
|
| 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('/')
|
|
|
|
| 49 |
preference_map = {
|
| 50 |
'vegetarian': "Category__c = 'Veg'",
|
| 51 |
'non-vegetarian': "Category__c = 'Non-Veg'",
|
| 52 |
+
'both': None # No condition to fetch all records, including "Both" category
|
| 53 |
}
|
| 54 |
+
condition = preference_map.get(dietary_preference)
|
| 55 |
|
| 56 |
try:
|
| 57 |
+
# Construct the base query
|
| 58 |
+
soql = "SELECT Name, Image_URL__c, Category__c FROM Sector_Detail__c" # Added Category__c for debugging
|
| 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', ''), "category": record.get('Category__c', 'Unknown')}
|
| 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__':
|