geethareddy commited on
Commit
540f4a3
·
verified ·
1 Parent(s): 40318f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
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
- print(f"Error connecting to Salesforce: {e}")
23
  return None
24
 
25
- # Initialize Salesforce connection (can be moved to request scope in production)
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': "1=1" # No category filter for "Both"
47
  }
48
- condition = preference_map.get(dietary_preference, "1=1") # Default to all if invalid
49
 
50
  try:
51
- soql = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE {condition} LIMIT 200"
 
 
 
 
 
 
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__':