Spaces:
Build error
Build error
File size: 6,084 Bytes
ac4334c 9b35d16 71978cc 728ec2a 253a332 ac4334c 4be2e38 ac4334c b658d64 c32c1af 4be2e38 84a5b18 71978cc 84a5b18 9b35d16 84a5b18 ac4334c 71978cc 84a5b18 76bfdc3 bc78641 76bfdc3 a2f1a4d 4be2e38 ac4334c a2f1a4d ac4334c b658d64 1d1bd66 67666ca 1d1bd66 ac4334c a2f1a4d 1d1bd66 84a5b18 5119cc5 51d39c9 5119cc5 ac4334c a2f1a4d 84a5b18 ac4334c 84a5b18 728ec2a 67666ca ac4334c ba57252 ac4334c ba57252 5885c76 ba57252 ac4334c ba57252 ac4334c ba57252 ac4334c ba57252 5885c76 ac4334c ba57252 ac4334c ba57252 ac4334c 5885c76 ba57252 ac4334c 4be2e38 b658d64 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | from flask import Flask, render_template, send_from_directory, request, jsonify
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import os
import logging
logging.basicConfig(level=logging.DEBUG)
load_dotenv()
app = Flask(__name__, template_folder='templates', static_folder='static')
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')
)
return sf
except Exception as e:
print(f"Error connecting to Salesforce: {e}")
return None
sf = get_salesforce_connection()
@app.route('/get_ingredients_for_customization', methods=['POST'])
def get_ingredients_for_customization():
dietary_preference = request.json.get('dietary_preference', '').strip().lower()
logging.debug(f"Received dietary preference for customization: {dietary_preference}")
# Map dietary preference to SOQL condition
if dietary_preference == 'both':
condition = "Category__c = 'both'" # This fetches both vegetarian and non-vegetarian ingredients
else:
preference_map = {
'vegetarian': "Category__c = 'Veg'",
'non-vegetarian': "Category__c = 'Non-Veg'"
}
condition = preference_map.get(dietary_preference)
if not condition:
logging.debug("Invalid dietary preference received.")
return jsonify({"error": "Invalid dietary preference."}), 400
try:
# Fetch ingredients based on dietary preference
soql = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE {condition} LIMIT 200"
result = sf.query(soql)
ingredients = [
{"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
for record in result['records'] if 'Name' in record
]
logging.debug(f"Fetched {len(ingredients)} ingredients for customization.")
return jsonify({"ingredients": ingredients})
except Exception as e:
logging.error(f"Error while fetching ingredients for customization: {str(e)}")
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
@app.route('/')
def index():
return render_template('index.html')
@app.route('/static/<path:filename>')
def serve_static(filename):
return send_from_directory('static', filename)
@app.route('/get_ingredients', methods=['POST'])
def get_ingredients():
dietary_preference = request.json.get('dietary_preference', '').strip().lower()
logging.debug(f"Received dietary preference: {dietary_preference}")
# Map dietary preference to SOQL condition
if dietary_preference == 'both':
condition = "Category__c = 'both'" # This will fetch both vegetarian and non-vegetarian
else:
preference_map = {
'vegetarian': "Category__c = 'Veg'",
'non-vegetarian': "Category__c = 'Non-Veg'"
}
condition = preference_map.get(dietary_preference)
if not condition:
logging.debug("Invalid dietary preference received.")
return jsonify({"error": "Invalid dietary preference."}), 400
try:
soql = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE {condition} LIMIT 200"
result = sf.query(soql)
ingredients = [
{"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
for record in result['records'] if 'Name' in record
]
logging.debug(f"Fetched {len(ingredients)} ingredients.")
return jsonify({"ingredients": ingredients})
except Exception as e:
logging.error(f"Error while fetching ingredients: {str(e)}")
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
@app.route('/get_menu_items', methods=['POST'])
def get_menu_items():
ingredient_names = request.json.get('ingredient_names', '').strip().lower()
logging.debug(f"Received ingredient names: {ingredient_names}")
# Constructing a SOQL query where Name contains any of the ingredient names
# Split the ingredient names into separate words to match each ingredient
ingredients_list = ingredient_names.split()
# Create a dynamic WHERE clause to match the ingredient names in the Menu Item names
condition = " OR ".join([f"Name LIKE '%{ingredient}%'" for ingredient in ingredients_list])
if not condition:
logging.debug("No ingredients received.")
return jsonify({"error": "No valid ingredients provided."}), 400
try:
soql = f"SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c FROM Menu_Item__c WHERE {condition} LIMIT 200"
result = sf.query(soql)
menu_items = [
{"name": record['Name'], "price": record.get('Price__c', ''), "description": record.get('Description__c', ''),
"image_url1": record.get('Image1__c', ''), "image_url2": record.get('Image2__c', ''),
"veg_nonveg": record.get('Veg_NonVeg__c', ''), "section": record.get('Section__c', ''),
"total_ordered": record.get('Total_Ordered__c', '')}
for record in result['records'] if 'Name' in record
]
logging.debug(f"Fetched {len(menu_items)} menu items based on ingredients.")
return jsonify({"menu_items": menu_items})
except Exception as e:
logging.error(f"Error while fetching menu items: {str(e)}")
return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
@app.route('/submit_ingredients', methods=['POST'])
def submit_ingredients():
data = request.json
ingredients = data.get('ingredients', [])
if not ingredients:
return jsonify({'error': 'No ingredients selected'}), 400
logging.debug(f"Ingredients submitted: {ingredients}")
return jsonify({'success': True})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=7860) |