Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,7 +46,7 @@ def get_menu_items():
|
|
| 46 |
|
| 47 |
dietary_preference = request.json.get('dietary_preference', 'both').lower()
|
| 48 |
try:
|
| 49 |
-
soql = "SELECT Name, Image_URL__c, Category__c FROM
|
| 50 |
if dietary_preference == 'vegetarian':
|
| 51 |
soql += " WHERE Category__c = 'Veg'"
|
| 52 |
elif dietary_preference == 'non-vegetarian':
|
|
@@ -83,7 +83,7 @@ def suggest_items():
|
|
| 83 |
return jsonify({"error": "Search term is required"}), 400
|
| 84 |
|
| 85 |
try:
|
| 86 |
-
soql = f"SELECT Name, Image_URL__c FROM
|
| 87 |
logger.info(f"Executing SOQL query: {soql}")
|
| 88 |
result = sf.query(soql)
|
| 89 |
suggestions = [
|
|
@@ -96,7 +96,7 @@ def suggest_items():
|
|
| 96 |
logger.error(f"Failed to fetch suggestions: {str(e)}")
|
| 97 |
return jsonify({"error": "Failed to fetch suggestions from Salesforce"}), 500
|
| 98 |
|
| 99 |
-
# Fetch item details
|
| 100 |
@app.route('/get_item_details', methods=['POST'])
|
| 101 |
def get_item_details():
|
| 102 |
global sf
|
|
@@ -110,14 +110,12 @@ def get_item_details():
|
|
| 110 |
return jsonify({"error": "Item name is required"}), 400
|
| 111 |
|
| 112 |
try:
|
| 113 |
-
|
| 114 |
-
soql = f"SELECT Name, Description__c, Ingredients__c, Image_URL__c FROM Sector_Detail__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
| 115 |
logger.info(f"Executing SOQL query: {soql}")
|
| 116 |
result = sf.query(soql)
|
| 117 |
|
| 118 |
if result['totalSize'] == 0:
|
| 119 |
-
|
| 120 |
-
soql_fallback = f"SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
| 121 |
result_fallback = sf.query(soql_fallback)
|
| 122 |
if result_fallback['totalSize'] > 0:
|
| 123 |
record = result_fallback['records'][0]
|
|
@@ -144,5 +142,31 @@ def get_item_details():
|
|
| 144 |
logger.error(f"Failed to fetch item details: {str(e)}")
|
| 145 |
return jsonify({"error": "Failed to fetch item details from Salesforce"}), 500
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
if __name__ == '__main__':
|
| 148 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
| 46 |
|
| 47 |
dietary_preference = request.json.get('dietary_preference', 'both').lower()
|
| 48 |
try:
|
| 49 |
+
soql = "SELECT Name, Image_URL__c, Category__c FROM Ingredient_Object__c"
|
| 50 |
if dietary_preference == 'vegetarian':
|
| 51 |
soql += " WHERE Category__c = 'Veg'"
|
| 52 |
elif dietary_preference == 'non-vegetarian':
|
|
|
|
| 83 |
return jsonify({"error": "Search term is required"}), 400
|
| 84 |
|
| 85 |
try:
|
| 86 |
+
soql = f"SELECT Name, Image_URL__c FROM Ingredient_Object__c WHERE Name LIKE '%{search_term}%' OR Ingredients__c LIKE '%{search_term}%' LIMIT 10"
|
| 87 |
logger.info(f"Executing SOQL query: {soql}")
|
| 88 |
result = sf.query(soql)
|
| 89 |
suggestions = [
|
|
|
|
| 96 |
logger.error(f"Failed to fetch suggestions: {str(e)}")
|
| 97 |
return jsonify({"error": "Failed to fetch suggestions from Salesforce"}), 500
|
| 98 |
|
| 99 |
+
# Fetch item details
|
| 100 |
@app.route('/get_item_details', methods=['POST'])
|
| 101 |
def get_item_details():
|
| 102 |
global sf
|
|
|
|
| 110 |
return jsonify({"error": "Item name is required"}), 400
|
| 111 |
|
| 112 |
try:
|
| 113 |
+
soql = f"SELECT Name, Description__c, Ingredients__c, Image_URL__c FROM Ingredient_Object__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
|
|
|
| 114 |
logger.info(f"Executing SOQL query: {soql}")
|
| 115 |
result = sf.query(soql)
|
| 116 |
|
| 117 |
if result['totalSize'] == 0:
|
| 118 |
+
soql_fallback = f"SELECT Name, Image_URL__c FROM Ingredient_Object__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
|
|
|
| 119 |
result_fallback = sf.query(soql_fallback)
|
| 120 |
if result_fallback['totalSize'] > 0:
|
| 121 |
record = result_fallback['records'][0]
|
|
|
|
| 142 |
logger.error(f"Failed to fetch item details: {str(e)}")
|
| 143 |
return jsonify({"error": "Failed to fetch item details from Salesforce"}), 500
|
| 144 |
|
| 145 |
+
# Submit selected items to Salesforce
|
| 146 |
+
@app.route('/submit_ingredients', methods=['POST'])
|
| 147 |
+
def submit_ingredients():
|
| 148 |
+
global sf
|
| 149 |
+
if not sf:
|
| 150 |
+
sf = get_salesforce_connection()
|
| 151 |
+
if not sf:
|
| 152 |
+
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 153 |
+
|
| 154 |
+
ingredients = request.json.get('ingredients', [])
|
| 155 |
+
if not ingredients:
|
| 156 |
+
return jsonify({"error": "No ingredients provided"}), 400
|
| 157 |
+
|
| 158 |
+
try:
|
| 159 |
+
# Create a new record in Ingredient_Object__c with the selected ingredients
|
| 160 |
+
ingredient_names = ", ".join(ingredients)
|
| 161 |
+
sf.Ingredient_Object__c.create({
|
| 162 |
+
'Name': f"User Selection {ingredient_names[:50]}", # Truncate if too long
|
| 163 |
+
'Ingredients__c': ingredient_names
|
| 164 |
+
})
|
| 165 |
+
logger.info(f"Submitted ingredients to Salesforce: {ingredient_names}")
|
| 166 |
+
return jsonify({"success": "Ingredients submitted successfully"})
|
| 167 |
+
except Exception as e:
|
| 168 |
+
logger.error(f"Failed to submit ingredients: {str(e)}")
|
| 169 |
+
return jsonify({"error": "Failed to submit ingredients to Salesforce"}), 500
|
| 170 |
+
|
| 171 |
if __name__ == '__main__':
|
| 172 |
app.run(debug=True, host='0.0.0.0', port=7860)
|