| from flask import Blueprint, request, jsonify, redirect, url_for, session |
| import random |
| from salesforce import get_salesforce_connection |
| sf = get_salesforce_connection() |
| |
| customdish_blueprint = Blueprint('customdish', __name__) |
|
|
| @customdish_blueprint.route("/generate_custom_dish", methods=["POST"]) |
| def generate_custom_dish(): |
| try: |
| data = request.form |
| dish_name = data.get("name") |
| description = data.get("description") |
| item_image_url = "https://huggingface.co/spaces/nagasurendra/BiryaniHubflask30/resolve/main/static/customized.jpg" |
| item_image_url2 = "https://huggingface.co/spaces/nagasurendra/BiryaniHubflask30/resolve/main/static/customized1.jpg" |
|
|
| if not dish_name or not description: |
| return jsonify({"success": False, "error": "Both fields are required"}), 400 |
| dish_name = dish_name.replace("'", "+") |
|
|
|
|
| |
| price = random.randint(10, 30) |
|
|
| |
| veg_keywords = ["paneer", "vegetable", "mushroom", "cheese"] |
| non_veg_keywords = ["chicken", "mutton", "fish", "egg"] |
| |
| category = "Veg" if any(word in description.lower() for word in veg_keywords) else \ |
| "Non veg" if any(word in description.lower() for word in non_veg_keywords) else \ |
| "both" |
|
|
| |
| existing_dish_query = f"SELECT Id, Name, Price__c, Image1__c, Image2__c, Description__c, Veg_NonVeg__c FROM Custom_Dish__c WHERE Name = '{dish_name}'" |
| existing_dish_result = sf.query(existing_dish_query) |
|
|
| if existing_dish_result['totalSize'] > 0: |
| |
| existing_dish = existing_dish_result['records'][0] |
| price = existing_dish['Price__c'] |
| item_image_url = existing_dish['Image1__c'] |
| item_image_url2 = existing_dish['Image2__c'] |
| category = existing_dish['Veg_NonVeg__c'] |
| else: |
| |
| custom_dish = { |
| 'Name': dish_name, |
| 'Price__c': price, |
| 'Image1__c': item_image_url, |
| 'Image2__c': item_image_url2, |
| 'Description__c': description, |
| 'Veg_NonVeg__c': category, |
| 'Section__c': 'Customized dish', |
| 'Total_Ordered__c': 0 |
| } |
|
|
| |
| result = sf.Custom_Dish__c.create(custom_dish) |
|
|
| if not result.get('success'): |
| return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500 |
|
|
| |
| email = session.get('user_email') |
| |
| |
| cart_item_query = f"SELECT Id, Quantity__c, Price__c, Base_Price__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}' AND Name = '{dish_name}'" |
| cart_item_result = sf.query(cart_item_query) |
|
|
| if cart_item_result['totalSize'] > 0: |
| |
| cart_item = cart_item_result['records'][0] |
| new_quantity = cart_item['Quantity__c'] + 1 |
| new_price = price * new_quantity |
| |
| |
| updated_cart_item = { |
| 'Quantity__c': new_quantity, |
| 'Price__c': new_price |
| } |
| |
| cart_item_update = sf.Cart_Item__c.update(cart_item['Id'], updated_cart_item) |
|
|
| else: |
| |
| cart_item = { |
| 'Name': dish_name, |
| 'Price__c': price, |
| 'Base_Price__c': price, |
| 'Image1__c': item_image_url, |
| 'Quantity__c': 1, |
| 'Add_Ons__c': '', |
| 'Add_Ons_Price__c': 0, |
| 'Customer_Email__c': email |
| } |
|
|
| |
| cart_result = sf.Cart_Item__c.create(cart_item) |
|
|
| |
| return redirect(url_for('cart.cart')) |
|
|
|
|
| except Exception as e: |
| return jsonify({"success": False, "error": str(e)}), 500 |
|
|
|
|
|
|