Spaces:
Sleeping
Sleeping
from flask import Flask, render_template, request, jsonify | |
from simple_salesforce import Salesforce | |
from dotenv import load_dotenv | |
import os | |
import logging | |
import uuid | |
# Load environment variables | |
load_dotenv() | |
# Set up logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
app = Flask(__name__, template_folder='templates', static_folder='static') | |
# Salesforce connection function | |
def get_salesforce_connection(): | |
try: | |
sf = Salesforce( | |
username=os.getenv('SFDC_USERNAME'), | |
password=os.getenv('SFDC_PASSWORD'), | |
security_token=os.getenv('SFDC_SECURITY_TOKEN'), | |
domain=os.getenv('SFDC_DOMAIN', 'login') | |
) | |
logger.info("Successfully connected to Salesforce") | |
return sf | |
except Exception as e: | |
logger.error(f"Error connecting to Salesforce: {e}") | |
return None | |
# Initialize Salesforce connection | |
sf = get_salesforce_connection() | |
def index(): | |
return render_template('index.html') | |
# Fetch items from Sector_Detail__c for dietary preferences | |
def get_menu_items(): | |
global sf | |
if not sf: | |
sf = get_salesforce_connection() | |
if not sf: | |
logger.error("Salesforce connection failed after retry") | |
return jsonify({"error": "Unable to connect to Salesforce"}), 500 | |
dietary_preference = request.json.get('dietary_preference', 'both').lower() | |
try: | |
soql = "SELECT Name, Image_URL__c, Category__c, Description__c FROM Sector_Detail__c" | |
if dietary_preference == 'vegetarian': | |
soql += " WHERE Category__c = 'Veg'" | |
elif dietary_preference == 'non-vegetarian': | |
soql += " WHERE Category__c = 'Non-Veg'" | |
soql += " LIMIT 200" | |
logger.info(f"Executing SOQL query: {soql}") | |
result = sf.query(soql) | |
items = [ | |
{ | |
"name": record['Name'], | |
"image_url": record.get('Image_URL__c', ''), | |
"category": record.get('Category__c', ''), | |
"description": record.get('Description__c', 'No description available') | |
} | |
for record in result['records'] if 'Name' in record | |
] | |
logger.info(f"Fetched {len(items)} items from Sector_Detail__c for {dietary_preference}") | |
return jsonify({"menu_items": items}) | |
except Exception as e: | |
logger.error(f"Failed to fetch items from Sector_Detail__c: {str(e)}") | |
return jsonify({"error": f"Failed to fetch items from Salesforce: {str(e)}"}), 500 | |
# Fetch suggestions from Ingredient_Object__c and Menu_Item__c | |
def suggest_items(): | |
global sf | |
if not sf: | |
sf = get_salesforce_connection() | |
if not sf: | |
return jsonify({"error": "Unable to connect to Salesforce"}), 500 | |
search_term = request.json.get('search_term', '').strip() | |
if not search_term: | |
return jsonify({"error": "Search term is required"}), 400 | |
try: | |
# Query Ingredient_Object__c | |
soql_ingredient = f"SELECT Ingredient_Name__c, Image_URL__c FROM Ingredient_Object__c WHERE Ingredient_Name__c LIKE '%{search_term}%' LIMIT 5" | |
logger.info(f"Executing SOQL query for Ingredient_Object__c: {soql_ingredient}") | |
result_ingredient = sf.query(soql_ingredient) | |
suggestions = [ | |
{"name": record['Ingredient_Name__c'], "image_url": record.get('Image_URL__c', ''), "source": "Ingredient_Object__c"} | |
for record in result_ingredient['records'] if 'Ingredient_Name__c' in record | |
] | |
# Query Menu_Item__c | |
soql_menu = f"SELECT Name, Image1__c, Veg_NonVeg__c FROM Menu_Item__c WHERE Name LIKE '%{search_term}%' LIMIT 5" | |
logger.info(f"Executing SOQL query for Menu_Item__c: {soql_menu}") | |
result_menu = sf.query(soql_menu) | |
suggestions += [ | |
{"name": record['Name'], "image_url": record.get('Image1__c', ''), "source": "Menu_Item__c", "veg_nonveg": record.get('Veg_NonVeg__c', '')} | |
for record in result_menu['records'] if 'Name' in record | |
] | |
logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'") | |
return jsonify({"suggestions": suggestions}) | |
except Exception as e: | |
logger.error(f"Failed to fetch suggestions: {str(e)}") | |
return jsonify({"error": "Failed to fetch suggestions from Salesforce"}), 500 | |
# Fetch item details from Menu_Item__c by name | |
def get_menu_item_details(): | |
global sf | |
if not sf: | |
sf = get_salesforce_connection() | |
if not sf: | |
return jsonify({"error": "Unable to connect to Salesforce"}), 500 | |
item_name = request.json.get('item_name', '').strip() | |
if not item_name: | |
return jsonify({"error": "Item name is required"}), 400 | |
try: | |
soql = f""" | |
SELECT Name, Description__c, Image1__c, Ingredientsinfo__c, NutritionalInfo__c, Price__c, | |
Sector__c, Spice_Levels__c, Veg_NonVeg__c, Category__c, Dynamic_Dish__c | |
FROM Menu_Item__c | |
WHERE Name LIKE '%{item_name}%' | |
LIMIT 1 | |
""" | |
logger.info(f"Executing SOQL query: {soql}") | |
result = sf.query(soql) | |
if result['totalSize'] == 0: | |
return jsonify({"error": f"No item found matching '{item_name}' in Menu_Item__c"}), 404 | |
record = result['records'][0] | |
item_details = { | |
"name": record.get('Name', ''), | |
"description": record.get('Description__c', 'No description available'), | |
"image_url": record.get('Image1__c', 'https://via.placeholder.com/100'), | |
"ingredients": record.get('Ingredientsinfo__c', 'No ingredients info'), | |
"nutritional_info": record.get('NutritionalInfo__c', 'No nutritional info'), | |
"price": record.get('Price__c', 0.0), | |
"sector": record.get('Sector__c', 'Unknown'), | |
"spice_levels": record.get('Spice_Levels__c', 'Not specified'), | |
"veg_nonveg": record.get('Veg_NonVeg__c', 'Unknown'), | |
"category": record.get('Category__c', 'Unknown'), | |
"dynamic_dish": record.get('Dynamic_Dish__c', False) | |
} | |
logger.info(f"Fetched details for '{item_name}' from Menu_Item__c") | |
return jsonify({"item_details": item_details}) | |
except Exception as e: | |
logger.error(f"Failed to fetch item details from Menu_Item__c: {str(e)}") | |
return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500 | |
# Submit selected items to Ingredient_Object__c or Menu_Item__c | |
def submit_items(): | |
global sf | |
if not sf: | |
sf = get_salesforce_connection() | |
if not sf: | |
return jsonify({"error": "Unable to connect to Salesforce"}), 500 | |
items = request.json.get('items', []) | |
if not items: | |
return jsonify({"error": "No items to submit"}), 400 | |
try: | |
for item in items: | |
logger.info(f"Submitting item: {item}") | |
# Check if item is from Menu_Item__c or Ingredient_Object__c | |
if item.get('source') == 'Menu_Item__c': | |
sf.Menu_Item__c.create({ | |
'Name': item['name'], | |
'Description__c': item.get('description', 'No description available'), | |
'Image1__c': item.get('image_url', ''), | |
'Ingredientsinfo__c': item.get('ingredients', 'No ingredients info'), | |
'NutritionalInfo__c': item.get('nutritional_info', 'No nutritional info'), | |
'Price__c': item.get('price', 0.0), | |
'Sector__c': item.get('sector', 'Unknown'), | |
'Spice_Levels__c': item.get('spice_levels', 'Not specified'), | |
'Veg_NonVeg__c': item.get('veg_nonveg', 'Unknown'), | |
'Category__c': item.get('category', 'Unknown'), | |
'Dynamic_Dish__c': item.get('dynamic_dish', False) | |
}) | |
else: | |
sf.Ingredient_Object__c.create({ | |
'Ingredient_Name__c': item['name'], | |
'Category__c': item.get('category', 'Unknown'), | |
'Description__c': item.get('description', 'No description available') | |
}) | |
logger.info(f"Successfully submitted item to {item.get('source', 'Ingredient_Object__c')}: {item['name']}") | |
return jsonify({"success": f"Successfully submitted {len(items)} items to Salesforce"}) | |
except Exception as e: | |
logger.error(f"Failed to submit items: {str(e)}") | |
return jsonify({"error": f"Failed to submit items to Salesforce: {str(e)}"}), 500 | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0', port=7860) |