Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,29 @@
|
|
1 |
from flask import Flask, render_template, send_from_directory, request, jsonify
|
2 |
-
import
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
-
import time
|
6 |
|
7 |
# Load environment variables from .env file
|
8 |
load_dotenv()
|
9 |
|
10 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
SFDC_PASSWORD = os.getenv('SFDC_PASSWORD')
|
15 |
-
SFDC_SECURITY_TOKEN = os.getenv('SFDC_SECURITY_TOKEN')
|
16 |
-
SFDC_INSTANCE_URL = os.getenv('SFDC_INSTANCE_URL', 'https://login.salesforce.com')
|
17 |
-
CLIENT_ID = os.getenv('SFDC_CLIENT_ID') # Add to .env
|
18 |
-
CLIENT_SECRET = os.getenv('SFDC_CLIENT_SECRET') # Add to .env
|
19 |
-
|
20 |
-
# Cache the access token and its expiration
|
21 |
-
access_token = None
|
22 |
-
token_expiry = 0
|
23 |
-
|
24 |
-
# Function to get Salesforce access token
|
25 |
-
def get_salesforce_access_token():
|
26 |
-
global access_token, token_expiry
|
27 |
-
if time.time() < token_expiry - 300: # Use cached token if valid (with 5-minute buffer)
|
28 |
-
return access_token
|
29 |
-
|
30 |
-
auth_url = f"{SFDC_INSTANCE_URL}/services/oauth2/token"
|
31 |
-
data = {
|
32 |
-
'grant_type': 'password',
|
33 |
-
'client_id': CLIENT_ID,
|
34 |
-
'client_secret': CLIENT_SECRET,
|
35 |
-
'username': SFDC_USERNAME,
|
36 |
-
'password': f"{SFDC_PASSWORD}{SFDC_SECURITY_TOKEN}"
|
37 |
-
}
|
38 |
-
|
39 |
try:
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
return
|
47 |
-
except
|
48 |
-
print(f"
|
49 |
return None
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
|
54 |
@app.route('/')
|
55 |
def index():
|
@@ -61,41 +35,28 @@ def serve_static(filename):
|
|
61 |
|
62 |
@app.route('/get_ingredients', methods=['POST'])
|
63 |
def get_ingredients():
|
64 |
-
global
|
65 |
-
if not
|
66 |
-
|
67 |
-
if not
|
68 |
-
return jsonify({"error": "Failed to
|
69 |
|
70 |
dietary_preference = request.json.get('dietary_preference', '').lower()
|
71 |
|
72 |
-
#
|
73 |
if dietary_preference == 'veg':
|
74 |
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c WHERE Category__c IN ('Veg', 'Both') LIMIT 200"
|
75 |
elif dietary_preference == 'non-vegetarian':
|
76 |
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c WHERE Category__c IN ('Non-Veg', 'Both') LIMIT 200"
|
77 |
else:
|
78 |
-
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c LIMIT 200" #
|
79 |
|
80 |
-
headers = {
|
81 |
-
"Authorization": f"Bearer {access_token}",
|
82 |
-
"Content-Type": "application/json"
|
83 |
-
}
|
84 |
-
url = f"{SFDC_INSTANCE_URL}/services/data/v57.0/query/?q={soql}"
|
85 |
-
|
86 |
try:
|
87 |
-
|
88 |
-
|
89 |
-
data = response.json()
|
90 |
-
ingredients = [record['Sector_Detail_Name__c'] for record in data['records'] if 'Sector_Detail_Name__c' in record]
|
91 |
return jsonify({"ingredients": ingredients})
|
92 |
-
except
|
93 |
-
|
94 |
-
if e.response and e.response.status_code == 401: # Unauthorized, try refreshing token
|
95 |
-
access_token = get_salesforce_access_token()
|
96 |
-
if access_token:
|
97 |
-
return get_ingredients() # Retry the request
|
98 |
-
return jsonify({"error": f"Failed to fetch ingredients: {error_msg}"}), 500
|
99 |
|
100 |
if __name__ == '__main__':
|
101 |
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 |
|
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
|
12 |
+
def get_salesforce_connection():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
+
sf = Salesforce(
|
15 |
+
username=os.getenv('SFDC_USERNAME'),
|
16 |
+
password=os.getenv('SFDC_PASSWORD'),
|
17 |
+
security_token=os.getenv('SFDC_SECURITY_TOKEN'),
|
18 |
+
domain=os.getenv('SFDC_DOMAIN', 'login') # Default to 'login' for production
|
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
|
26 |
+
sf = get_salesforce_connection()
|
27 |
|
28 |
@app.route('/')
|
29 |
def index():
|
|
|
35 |
|
36 |
@app.route('/get_ingredients', methods=['POST'])
|
37 |
def get_ingredients():
|
38 |
+
global sf
|
39 |
+
if not sf:
|
40 |
+
sf = get_salesforce_connection()
|
41 |
+
if not sf:
|
42 |
+
return jsonify({"error": "Failed to connect to Salesforce"}), 500
|
43 |
|
44 |
dietary_preference = request.json.get('dietary_preference', '').lower()
|
45 |
|
46 |
+
# SOQL query based on dietary preference
|
47 |
if dietary_preference == 'veg':
|
48 |
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c WHERE Category__c IN ('Veg', 'Both') LIMIT 200"
|
49 |
elif dietary_preference == 'non-vegetarian':
|
50 |
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c WHERE Category__c IN ('Non-Veg', 'Both') LIMIT 200"
|
51 |
else:
|
52 |
+
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c LIMIT 200" # Default case for 50+ ingredients
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
try:
|
55 |
+
result = sf.query(soql)
|
56 |
+
ingredients = [record['Sector_Detail_Name__c'] for record in result['records'] if 'Sector_Detail_Name__c' in record]
|
|
|
|
|
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__':
|
62 |
app.run(debug=True, host='0.0.0.0', port=7860)
|