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 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(): category = request.json.get('category', '').strip().lower() logging.debug(f"Received category: {category}") if category == 'fish': logging.debug("Fetching fish-based menu items...") soql = "SELECT Item_Name__c, Image_URL__c FROM Menu_Item__c WHERE Category__c = 'Fish' LIMIT 200" else: logging.debug("Invalid category received.") return jsonify({"error": "Invalid category."}), 400 try: result = sf.query(soql) menu_items = [ {"name": record['Item_Name__c'], "image_url": record.get('Image_URL__c', '')} for record in result['records'] if 'Item_Name__c' in record ] logging.debug(f"Fetched {len(menu_items)} menu items.") 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)