Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ load_dotenv()
|
|
10 |
|
11 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
12 |
|
|
|
13 |
def get_salesforce_connection():
|
14 |
try:
|
15 |
sf = Salesforce(
|
@@ -23,6 +24,7 @@ def get_salesforce_connection():
|
|
23 |
print(f"Error connecting to Salesforce: {e}")
|
24 |
return None
|
25 |
|
|
|
26 |
sf = get_salesforce_connection()
|
27 |
|
28 |
@app.route('/')
|
@@ -33,85 +35,39 @@ def index():
|
|
33 |
def serve_static(filename):
|
34 |
return send_from_directory('static', filename)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
logging.debug(f"Received dietary preference: {dietary_preference}")
|
40 |
-
|
41 |
-
# Map dietary preference to SOQL condition
|
42 |
-
if dietary_preference == 'both':
|
43 |
-
condition = "Category__c = 'both'" # This will fetch both vegetarian and non-vegetarian
|
44 |
-
else:
|
45 |
-
preference_map = {
|
46 |
-
'vegetarian': "Category__c = 'Veg'",
|
47 |
-
'non-vegetarian': "Category__c = 'Non-Veg'"
|
48 |
-
}
|
49 |
-
condition = preference_map.get(dietary_preference)
|
50 |
-
|
51 |
-
if not condition:
|
52 |
-
logging.debug("Invalid dietary preference received.")
|
53 |
-
return jsonify({"error": "Invalid dietary preference."}), 400
|
54 |
-
|
55 |
try:
|
56 |
-
|
|
|
57 |
result = sf.query(soql)
|
58 |
-
ingredients = [
|
59 |
-
{"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
|
60 |
-
for record in result['records'] if 'Name' in record
|
61 |
-
]
|
62 |
-
logging.debug(f"Fetched {len(ingredients)} ingredients.")
|
63 |
-
return jsonify({"ingredients": ingredients})
|
64 |
-
except Exception as e:
|
65 |
-
logging.error(f"Error while fetching ingredients: {str(e)}")
|
66 |
-
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
67 |
-
|
68 |
-
|
69 |
-
@app.route('/get_menu_items', methods=['POST'])
|
70 |
-
def get_menu_items():
|
71 |
-
ingredient_names = request.json.get('ingredient_names', '').strip().lower()
|
72 |
-
logging.debug(f"Received ingredient names: {ingredient_names}")
|
73 |
-
|
74 |
-
# Constructing a SOQL query where Name contains any of the ingredient names
|
75 |
-
# Split the ingredient names into separate words to match each ingredient
|
76 |
-
ingredients_list = ingredient_names.split()
|
77 |
-
|
78 |
-
# Create a dynamic WHERE clause to match the ingredient names in the Menu Item names
|
79 |
-
condition = " OR ".join([f"Name LIKE '%{ingredient}%'" for ingredient in ingredients_list])
|
80 |
-
|
81 |
-
if not condition:
|
82 |
-
logging.debug("No ingredients received.")
|
83 |
-
return jsonify({"error": "No valid ingredients provided."}), 400
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
menu_items = [
|
90 |
-
{"name": record['Name'], "price": record.get('Price__c', ''), "description": record.get('Description__c', ''),
|
91 |
-
"image_url1": record.get('Image1__c', ''), "image_url2": record.get('Image2__c', ''),
|
92 |
-
"veg_nonveg": record.get('Veg_NonVeg__c', ''), "section": record.get('Section__c', ''),
|
93 |
-
"total_ordered": record.get('Total_Ordered__c', '')}
|
94 |
-
for record in result['records'] if 'Name' in record
|
95 |
-
]
|
96 |
-
logging.debug(f"Fetched {len(menu_items)} menu items based on ingredients.")
|
97 |
-
return jsonify({"menu_items": menu_items})
|
98 |
-
|
99 |
except Exception as e:
|
100 |
-
logging.error(f"Error while fetching
|
101 |
-
return jsonify({"error": f"Failed to fetch
|
102 |
-
|
103 |
-
|
104 |
|
105 |
-
|
106 |
-
|
|
|
107 |
data = request.json
|
108 |
-
|
109 |
|
110 |
-
if not
|
111 |
-
return jsonify({'error': 'No
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
if __name__ == '__main__':
|
117 |
-
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
10 |
|
11 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
12 |
|
13 |
+
# Salesforce Connection
|
14 |
def get_salesforce_connection():
|
15 |
try:
|
16 |
sf = Salesforce(
|
|
|
24 |
print(f"Error connecting to Salesforce: {e}")
|
25 |
return None
|
26 |
|
27 |
+
# Initialize Salesforce connection
|
28 |
sf = get_salesforce_connection()
|
29 |
|
30 |
@app.route('/')
|
|
|
35 |
def serve_static(filename):
|
36 |
return send_from_directory('static', filename)
|
37 |
|
38 |
+
# Sample endpoint for Salesforce queries (non-restaurant)
|
39 |
+
@app.route('/get_salesforce_data', methods=['GET'])
|
40 |
+
def get_salesforce_data():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
try:
|
42 |
+
# Example SOQL query for retrieving data from a Salesforce object
|
43 |
+
soql = "SELECT Id, Name FROM Account LIMIT 10"
|
44 |
result = sf.query(soql)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Format the result as needed
|
47 |
+
records = [{"id": record['Id'], "name": record['Name']} for record in result['records']]
|
48 |
+
logging.debug(f"Fetched {len(records)} records from Salesforce.")
|
49 |
+
return jsonify({"data": records})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
except Exception as e:
|
51 |
+
logging.error(f"Error while fetching data from Salesforce: {str(e)}")
|
52 |
+
return jsonify({"error": f"Failed to fetch data: {str(e)}"}), 500
|
|
|
|
|
53 |
|
54 |
+
# Example of a POST endpoint for submitting data (non-restaurant)
|
55 |
+
@app.route('/submit_salesforce_data', methods=['POST'])
|
56 |
+
def submit_salesforce_data():
|
57 |
data = request.json
|
58 |
+
record_data = data.get('record', {})
|
59 |
|
60 |
+
if not record_data:
|
61 |
+
return jsonify({'error': 'No data provided'}), 400
|
62 |
+
|
63 |
+
try:
|
64 |
+
# Example of creating a new Salesforce record
|
65 |
+
account = sf.Account.create(record_data)
|
66 |
+
logging.debug(f"Created new Salesforce account with Id: {account['id']}")
|
67 |
+
return jsonify({'success': True, 'account_id': account['id']})
|
68 |
+
except Exception as e:
|
69 |
+
logging.error(f"Error while submitting data to Salesforce: {str(e)}")
|
70 |
+
return jsonify({"error": f"Failed to submit data: {str(e)}"}), 500
|
71 |
|
72 |
if __name__ == '__main__':
|
73 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|