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('/') def index(): return render_template('index.html') @app.route('/static/') 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)