File size: 2,625 Bytes
6a477c5
 
 
 
 
71978cc
6a477c5
 
4be2e38
6a477c5
 
 
540f4a3
6a477c5
4be2e38
6a477c5
 
 
 
 
 
 
 
 
 
 
 
 
 
71978cc
6a477c5
 
a2f1a4d
6a477c5
 
 
4be2e38
6a477c5
 
 
 
 
 
 
71978cc
6a477c5
 
 
 
 
 
c9872f2
c4b46b3
6a477c5
a2f1a4d
6a477c5
 
c9872f2
6a477c5
 
 
540f4a3
6a477c5
 
 
c9872f2
6a477c5
 
 
 
 
 
 
a2f1a4d
6a477c5
 
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
from flask import Flask, render_template, request, jsonify
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import os
import logging

# Load environment variables from .env file
load_dotenv()

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(__name__, template_folder='templates', static_folder='static')

# Function to get Salesforce connection
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()

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/get_ingredients', methods=['POST'])
def get_ingredients():
    global sf
    if not sf:
        sf = get_salesforce_connection()
        if not sf:
            return jsonify({"error": "Failed to connect to Salesforce"}), 500

    dietary_preference = request.json.get('dietary_preference', '').lower()
    
    # Map dietary preference to SOQL condition
    preference_map = {
        'vegetarian': "Category__c = 'Veg'",
        'non-vegetarian': "Category__c = 'Non-Veg'",
        'both': None  # No condition to fetch all records, including "Both" category
    }
    condition = preference_map.get(dietary_preference)

    try:
        # Construct the base query
        soql = "SELECT Name, Image_URL__c, Category__c FROM Sector_Detail__c"  # Added Category__c for debugging
        if condition:
            soql += f" WHERE {condition}"
        soql += " LIMIT 200"

        logger.info(f"Executing SOQL query: {soql}")
        result = sf.query(soql)
        ingredients = [
            {"name": record['Name'], "image_url": record.get('Image_URL__c', ''), "category": record.get('Category__c', 'Unknown')}
            for record in result['records'] if 'Name' in record
        ]
        logger.info(f"Fetched {len(ingredients)} ingredients: {ingredients}")
        return jsonify({"ingredients": ingredients})
    except Exception as e:
        logger.error(f"Failed to fetch ingredients: {str(e)}")
        return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=7860)