Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,48 @@
|
|
| 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 |
-
import time
|
| 7 |
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
-
logging.basicConfig(level=logging.INFO
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 16 |
|
| 17 |
-
# Salesforce connection function
|
| 18 |
-
def get_salesforce_connection(
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
logger.critical("Failed to connect to Salesforce after retries")
|
| 34 |
-
return None
|
| 35 |
-
|
| 36 |
-
# Initialize Salesforce connection globally
|
| 37 |
sf = get_salesforce_connection()
|
| 38 |
|
| 39 |
@app.route('/')
|
| 40 |
def index():
|
| 41 |
return render_template('index.html')
|
| 42 |
|
| 43 |
-
# Fetch menu items
|
| 44 |
@app.route('/get_menu_items', methods=['POST'])
|
| 45 |
def get_menu_items():
|
| 46 |
global sf
|
| 47 |
if not sf:
|
| 48 |
sf = get_salesforce_connection()
|
| 49 |
if not sf:
|
| 50 |
-
return jsonify({"error": "Unable to connect to Salesforce
|
| 51 |
|
| 52 |
dietary_preference = request.json.get('dietary_preference', 'both').lower()
|
| 53 |
try:
|
|
@@ -58,34 +53,30 @@ def get_menu_items():
|
|
| 58 |
soql += " WHERE Category__c = 'Non-Veg'"
|
| 59 |
soql += " LIMIT 200"
|
| 60 |
|
| 61 |
-
logger.info(f"Executing SOQL query
|
| 62 |
result = sf.query(soql)
|
| 63 |
menu_items = [
|
| 64 |
{
|
| 65 |
"name": record['Name'],
|
| 66 |
-
"image_url": record.get('Image_URL__c', '
|
| 67 |
"category": record.get('Category__c', '')
|
| 68 |
}
|
| 69 |
for record in result['records'] if 'Name' in record
|
| 70 |
]
|
| 71 |
-
if not menu_items:
|
| 72 |
-
logger.warning(f"No items found for {dietary_preference}")
|
| 73 |
-
return jsonify({"menu_items": [], "message": f"No {dietary_preference} items found in Salesforce"})
|
| 74 |
-
|
| 75 |
logger.info(f"Fetched {len(menu_items)} menu items for {dietary_preference}")
|
| 76 |
return jsonify({"menu_items": menu_items})
|
| 77 |
except Exception as e:
|
| 78 |
logger.error(f"Failed to fetch menu items: {str(e)}")
|
| 79 |
return jsonify({"error": "Failed to fetch menu items from Salesforce"}), 500
|
| 80 |
|
| 81 |
-
# Fetch suggestions
|
| 82 |
@app.route('/suggest_items', methods=['POST'])
|
| 83 |
def suggest_items():
|
| 84 |
global sf
|
| 85 |
if not sf:
|
| 86 |
sf = get_salesforce_connection()
|
| 87 |
if not sf:
|
| 88 |
-
return jsonify({"error": "Unable to connect to Salesforce
|
| 89 |
|
| 90 |
search_term = request.json.get('search_term', '').strip()
|
| 91 |
if not search_term:
|
|
@@ -93,10 +84,10 @@ def suggest_items():
|
|
| 93 |
|
| 94 |
try:
|
| 95 |
soql = f"SELECT Name, Image_URL__c FROM Ingredient_Object__c WHERE Name LIKE '%{search_term}%' OR Ingredients__c LIKE '%{search_term}%' LIMIT 10"
|
| 96 |
-
logger.info(f"Executing SOQL query
|
| 97 |
result = sf.query(soql)
|
| 98 |
suggestions = [
|
| 99 |
-
{"name": record['Name'], "image_url": record.get('Image_URL__c', '
|
| 100 |
for record in result['records'] if 'Name' in record
|
| 101 |
]
|
| 102 |
logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
|
|
@@ -105,14 +96,14 @@ def suggest_items():
|
|
| 105 |
logger.error(f"Failed to fetch suggestions: {str(e)}")
|
| 106 |
return jsonify({"error": "Failed to fetch suggestions from Salesforce"}), 500
|
| 107 |
|
| 108 |
-
# Fetch item details
|
| 109 |
@app.route('/get_item_details', methods=['POST'])
|
| 110 |
def get_item_details():
|
| 111 |
global sf
|
| 112 |
if not sf:
|
| 113 |
sf = get_salesforce_connection()
|
| 114 |
if not sf:
|
| 115 |
-
return jsonify({"error": "Unable to connect to Salesforce
|
| 116 |
|
| 117 |
item_name = request.json.get('item_name', '').strip()
|
| 118 |
if not item_name:
|
|
@@ -120,7 +111,7 @@ def get_item_details():
|
|
| 120 |
|
| 121 |
try:
|
| 122 |
soql = f"SELECT Name, Description__c, Ingredients__c, Image_URL__c FROM Ingredient_Object__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
| 123 |
-
logger.info(f"Executing SOQL query
|
| 124 |
result = sf.query(soql)
|
| 125 |
|
| 126 |
if result['totalSize'] == 0:
|
|
@@ -132,7 +123,7 @@ def get_item_details():
|
|
| 132 |
"name": record.get('Name', ''),
|
| 133 |
"description": "Description not available",
|
| 134 |
"ingredients": "Ingredients not listed",
|
| 135 |
-
"image_url": record.get('Image_URL__c', '
|
| 136 |
}
|
| 137 |
logger.info(f"Fallback: Found '{item_name}' in menu items")
|
| 138 |
return jsonify({"item_details": item_details})
|
|
@@ -143,7 +134,7 @@ def get_item_details():
|
|
| 143 |
"name": record.get('Name', ''),
|
| 144 |
"description": record.get('Description__c', 'No description available'),
|
| 145 |
"ingredients": record.get('Ingredients__c', 'No ingredients listed'),
|
| 146 |
-
"image_url": record.get('Image_URL__c', '
|
| 147 |
}
|
| 148 |
logger.info(f"Fetched details for '{item_name}'")
|
| 149 |
return jsonify({"item_details": item_details})
|
|
@@ -151,30 +142,34 @@ def get_item_details():
|
|
| 151 |
logger.error(f"Failed to fetch item details: {str(e)}")
|
| 152 |
return jsonify({"error": "Failed to fetch item details from Salesforce"}), 500
|
| 153 |
|
| 154 |
-
# Submit selected items to
|
| 155 |
-
@app.route('/
|
| 156 |
-
def
|
| 157 |
global sf
|
| 158 |
if not sf:
|
| 159 |
sf = get_salesforce_connection()
|
| 160 |
if not sf:
|
| 161 |
-
return jsonify({"error": "Unable to connect to Salesforce
|
| 162 |
|
| 163 |
-
|
| 164 |
-
if not
|
| 165 |
-
return jsonify({"error": "No
|
| 166 |
|
| 167 |
try:
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
except Exception as e:
|
| 176 |
-
logger.error(f"Failed to submit
|
| 177 |
-
return jsonify({"error": "Failed to submit
|
| 178 |
|
| 179 |
if __name__ == '__main__':
|
| 180 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
| 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 |
# Load environment variables
|
| 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 |
+
# Salesforce connection function
|
| 17 |
+
def get_salesforce_connection():
|
| 18 |
+
try:
|
| 19 |
+
sf = Salesforce(
|
| 20 |
+
username=os.getenv('SFDC_USERNAME'),
|
| 21 |
+
password=os.getenv('SFDC_PASSWORD'),
|
| 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('/')
|
| 35 |
def index():
|
| 36 |
return render_template('index.html')
|
| 37 |
|
| 38 |
+
# Fetch menu items from Ingredient_Object__c
|
| 39 |
@app.route('/get_menu_items', methods=['POST'])
|
| 40 |
def get_menu_items():
|
| 41 |
global sf
|
| 42 |
if not sf:
|
| 43 |
sf = get_salesforce_connection()
|
| 44 |
if not sf:
|
| 45 |
+
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 46 |
|
| 47 |
dietary_preference = request.json.get('dietary_preference', 'both').lower()
|
| 48 |
try:
|
|
|
|
| 53 |
soql += " WHERE Category__c = 'Non-Veg'"
|
| 54 |
soql += " LIMIT 200"
|
| 55 |
|
| 56 |
+
logger.info(f"Executing SOQL query: {soql}")
|
| 57 |
result = sf.query(soql)
|
| 58 |
menu_items = [
|
| 59 |
{
|
| 60 |
"name": record['Name'],
|
| 61 |
+
"image_url": record.get('Image_URL__c', ''),
|
| 62 |
"category": record.get('Category__c', '')
|
| 63 |
}
|
| 64 |
for record in result['records'] if 'Name' in record
|
| 65 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
logger.info(f"Fetched {len(menu_items)} menu items for {dietary_preference}")
|
| 67 |
return jsonify({"menu_items": menu_items})
|
| 68 |
except Exception as e:
|
| 69 |
logger.error(f"Failed to fetch menu items: {str(e)}")
|
| 70 |
return jsonify({"error": "Failed to fetch menu items from Salesforce"}), 500
|
| 71 |
|
| 72 |
+
# Fetch suggestions from Ingredient_Object__c
|
| 73 |
@app.route('/suggest_items', methods=['POST'])
|
| 74 |
def suggest_items():
|
| 75 |
global sf
|
| 76 |
if not sf:
|
| 77 |
sf = get_salesforce_connection()
|
| 78 |
if not sf:
|
| 79 |
+
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 80 |
|
| 81 |
search_term = request.json.get('search_term', '').strip()
|
| 82 |
if not search_term:
|
|
|
|
| 84 |
|
| 85 |
try:
|
| 86 |
soql = f"SELECT Name, Image_URL__c FROM Ingredient_Object__c WHERE Name LIKE '%{search_term}%' OR Ingredients__c LIKE '%{search_term}%' LIMIT 10"
|
| 87 |
+
logger.info(f"Executing SOQL query: {soql}")
|
| 88 |
result = sf.query(soql)
|
| 89 |
suggestions = [
|
| 90 |
+
{"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
|
| 91 |
for record in result['records'] if 'Name' in record
|
| 92 |
]
|
| 93 |
logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
|
|
|
|
| 96 |
logger.error(f"Failed to fetch suggestions: {str(e)}")
|
| 97 |
return jsonify({"error": "Failed to fetch suggestions from Salesforce"}), 500
|
| 98 |
|
| 99 |
+
# Fetch item details from Ingredient_Object__c
|
| 100 |
@app.route('/get_item_details', methods=['POST'])
|
| 101 |
def get_item_details():
|
| 102 |
global sf
|
| 103 |
if not sf:
|
| 104 |
sf = get_salesforce_connection()
|
| 105 |
if not sf:
|
| 106 |
+
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 107 |
|
| 108 |
item_name = request.json.get('item_name', '').strip()
|
| 109 |
if not item_name:
|
|
|
|
| 111 |
|
| 112 |
try:
|
| 113 |
soql = f"SELECT Name, Description__c, Ingredients__c, Image_URL__c FROM Ingredient_Object__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
| 114 |
+
logger.info(f"Executing SOQL query: {soql}")
|
| 115 |
result = sf.query(soql)
|
| 116 |
|
| 117 |
if result['totalSize'] == 0:
|
|
|
|
| 123 |
"name": record.get('Name', ''),
|
| 124 |
"description": "Description not available",
|
| 125 |
"ingredients": "Ingredients not listed",
|
| 126 |
+
"image_url": record.get('Image_URL__c', '')
|
| 127 |
}
|
| 128 |
logger.info(f"Fallback: Found '{item_name}' in menu items")
|
| 129 |
return jsonify({"item_details": item_details})
|
|
|
|
| 134 |
"name": record.get('Name', ''),
|
| 135 |
"description": record.get('Description__c', 'No description available'),
|
| 136 |
"ingredients": record.get('Ingredients__c', 'No ingredients listed'),
|
| 137 |
+
"image_url": record.get('Image_URL__c', '')
|
| 138 |
}
|
| 139 |
logger.info(f"Fetched details for '{item_name}'")
|
| 140 |
return jsonify({"item_details": item_details})
|
|
|
|
| 142 |
logger.error(f"Failed to fetch item details: {str(e)}")
|
| 143 |
return jsonify({"error": "Failed to fetch item details from Salesforce"}), 500
|
| 144 |
|
| 145 |
+
# Submit selected items to Ingredient_Object__c
|
| 146 |
+
@app.route('/submit_items', methods=['POST'])
|
| 147 |
+
def submit_items():
|
| 148 |
global sf
|
| 149 |
if not sf:
|
| 150 |
sf = get_salesforce_connection()
|
| 151 |
if not sf:
|
| 152 |
+
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 153 |
|
| 154 |
+
items = request.json.get('items', [])
|
| 155 |
+
if not items:
|
| 156 |
+
return jsonify({"error": "No items to submit"}), 400
|
| 157 |
|
| 158 |
try:
|
| 159 |
+
for item in items:
|
| 160 |
+
# Create a new record in Ingredient_Object__c
|
| 161 |
+
sf.Ingredient_Object__c.create({
|
| 162 |
+
'Name': item['name'],
|
| 163 |
+
'Description__c': item['description'],
|
| 164 |
+
'Ingredients__c': item['ingredients'],
|
| 165 |
+
'Image_URL__c': item['image_url'],
|
| 166 |
+
'Category__c': item.get('category', 'Both') # Default to 'Both' if not provided
|
| 167 |
+
})
|
| 168 |
+
logger.info(f"Submitted item to Salesforce: {item['name']}")
|
| 169 |
+
return jsonify({"success": f"Successfully submitted {len(items)} items to Salesforce"})
|
| 170 |
except Exception as e:
|
| 171 |
+
logger.error(f"Failed to submit items: {str(e)}")
|
| 172 |
+
return jsonify({"error": "Failed to submit items to Salesforce"}), 500
|
| 173 |
|
| 174 |
if __name__ == '__main__':
|
| 175 |
app.run(debug=True, host='0.0.0.0', port=7860)
|