File size: 3,620 Bytes
ac4334c
9b35d16
71978cc
 
728ec2a
253a332
ac4334c
4be2e38
ac4334c
b658d64
c32c1af
4be2e38
84a5b18
71978cc
84a5b18
 
 
 
9b35d16
84a5b18
 
 
ac4334c
71978cc
 
84a5b18
a2f1a4d
4be2e38
 
 
 
ac4334c
 
 
 
a2f1a4d
 
ac4334c
 
b658d64
1d1bd66
 
 
 
 
 
 
 
ac4334c
 
a2f1a4d
 
1d1bd66
84a5b18
5119cc5
51d39c9
5119cc5
 
ac4334c
a2f1a4d
84a5b18
ac4334c
84a5b18
728ec2a
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
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/<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
    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)