geethareddy commited on
Commit
1c8a763
·
verified ·
1 Parent(s): b09682f

Update customdish.py

Browse files
Files changed (1) hide show
  1. customdish.py +22 -32
customdish.py CHANGED
@@ -2,6 +2,7 @@ from flask import Blueprint, request, jsonify, redirect, url_for, session
2
  import random
3
  from salesforce import get_salesforce_connection
4
  sf = get_salesforce_connection()
 
5
  # Create a Blueprint for custom dish-related routes
6
  customdish_blueprint = Blueprint('customdish', __name__)
7
 
@@ -18,14 +19,12 @@ def generate_custom_dish():
18
  return jsonify({"success": False, "error": "Both fields are required"}), 400
19
  dish_name = dish_name.replace("'", "+")
20
 
21
-
22
  # Generate a random price for the custom dish
23
- price = random.randint(10, 30) # Example logic for price setting
24
 
25
  # Determine Veg/Non-Veg
26
  veg_keywords = ["paneer", "vegetable", "mushroom", "cheese"]
27
  non_veg_keywords = ["chicken", "mutton", "fish", "egg"]
28
-
29
  category = "Veg" if any(word in description.lower() for word in veg_keywords) else \
30
  "Non veg" if any(word in description.lower() for word in non_veg_keywords) else \
31
  "both"
@@ -53,55 +52,46 @@ def generate_custom_dish():
53
  'Section__c': 'Customized dish',
54
  'Total_Ordered__c': 0
55
  }
56
-
57
- # Insert the custom dish into Salesforce (Custom_Dish__c object)
58
  result = sf.Custom_Dish__c.create(custom_dish)
59
-
60
  if not result.get('success'):
61
  return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500
62
 
63
- # After ensuring the dish exists, check if it's already in the Cart_Item__c
64
- email = session.get('user_email') # Assuming you have the user's email in session
65
-
66
- # Query to check if the custom dish already exists in the cart for the logged-in user
 
 
67
  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}'"
68
  cart_item_result = sf.query(cart_item_query)
69
 
70
  if cart_item_result['totalSize'] > 0:
71
- # If the custom dish is already in the cart, update the quantity and price
72
  cart_item = cart_item_result['records'][0]
73
- new_quantity = cart_item['Quantity__c'] + 1 # Increase quantity by 1
74
- new_price = price * new_quantity # Update price based on new quantity
75
-
76
- # Update the cart item in Salesforce
77
  updated_cart_item = {
78
  'Quantity__c': new_quantity,
79
  'Price__c': new_price
80
  }
81
-
82
- cart_item_update = sf.Cart_Item__c.update(cart_item['Id'], updated_cart_item)
83
-
84
  else:
85
- # If the custom dish is not in the cart, create a new cart item
86
  cart_item = {
87
  'Name': dish_name,
88
  'Price__c': price,
89
  'Base_Price__c': price,
90
  'Image1__c': item_image_url,
91
- 'Quantity__c': 1, # Default quantity is 1
92
- 'Add_Ons__c': '', # Set Add_ons__c to empty
93
- 'Add_Ons_Price__c': 0, # Set Add_ons_Price__c to 0
94
- 'Customer_Email__c': email # Associate the custom dish with the logged-in user
 
 
95
  }
 
96
 
97
- # Insert the custom dish as a Cart_Item__c record in Salesforce
98
- cart_result = sf.Cart_Item__c.create(cart_item)
99
-
100
- # Redirect to the cart page after successfully adding or updating the cart item
101
- return redirect(url_for('cart.cart'))
102
-
103
 
104
  except Exception as e:
105
- return jsonify({"success": False, "error": str(e)}), 500
106
-
107
-
 
2
  import random
3
  from salesforce import get_salesforce_connection
4
  sf = get_salesforce_connection()
5
+
6
  # Create a Blueprint for custom dish-related routes
7
  customdish_blueprint = Blueprint('customdish', __name__)
8
 
 
19
  return jsonify({"success": False, "error": "Both fields are required"}), 400
20
  dish_name = dish_name.replace("'", "+")
21
 
 
22
  # Generate a random price for the custom dish
23
+ price = random.randint(10, 30)
24
 
25
  # Determine Veg/Non-Veg
26
  veg_keywords = ["paneer", "vegetable", "mushroom", "cheese"]
27
  non_veg_keywords = ["chicken", "mutton", "fish", "egg"]
 
28
  category = "Veg" if any(word in description.lower() for word in veg_keywords) else \
29
  "Non veg" if any(word in description.lower() for word in non_veg_keywords) else \
30
  "both"
 
52
  'Section__c': 'Customized dish',
53
  'Total_Ordered__c': 0
54
  }
 
 
55
  result = sf.Custom_Dish__c.create(custom_dish)
 
56
  if not result.get('success'):
57
  return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500
58
 
59
+ # Add or update the custom dish in the cart
60
+ email = session.get('user_email')
61
+ if not email:
62
+ return jsonify({"success": False, "error": "User not logged in"}), 401
63
+
64
+ # Query to check if the custom dish already exists in the cart
65
  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}'"
66
  cart_item_result = sf.query(cart_item_query)
67
 
68
  if cart_item_result['totalSize'] > 0:
69
+ # Update existing cart item
70
  cart_item = cart_item_result['records'][0]
71
+ new_quantity = cart_item['Quantity__c'] + 1
72
+ new_price = price * new_quantity
 
 
73
  updated_cart_item = {
74
  'Quantity__c': new_quantity,
75
  'Price__c': new_price
76
  }
77
+ sf.Cart_Item__c.update(cart_item['Id'], updated_cart_item)
 
 
78
  else:
79
+ # Create new cart item
80
  cart_item = {
81
  'Name': dish_name,
82
  'Price__c': price,
83
  'Base_Price__c': price,
84
  'Image1__c': item_image_url,
85
+ 'Quantity__c': 1,
86
+ 'Add_Ons__c': '',
87
+ 'Add_Ons_Price__c': 0,
88
+ 'Customer_Email__c': email,
89
+ 'Category__c': category,
90
+ 'Section__c': 'Customized dish'
91
  }
92
+ sf.Cart_Item__c.create(cart_item)
93
 
94
+ return jsonify({"success": True, "message": "Custom dish added to cart successfully"})
 
 
 
 
 
95
 
96
  except Exception as e:
97
+ return jsonify({"success": False, "error": str(e)}), 500