Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -36,21 +36,24 @@ def create_salesforce_record(sf, name, email, phone_number):
|
|
36 |
except Exception as e:
|
37 |
raise Exception(f"Failed to create record: {str(e)}")
|
38 |
|
39 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
try:
|
41 |
order = sf.Order__c.create({
|
42 |
'Total_Price__c': total_price,
|
43 |
-
'Cart_Items__c': json.dumps(cart_items) # Storing cart items as JSON (you can refine this as needed)
|
|
|
44 |
})
|
45 |
return order
|
46 |
except Exception as e:
|
47 |
raise Exception(f"Failed to create order: {str(e)}")
|
48 |
|
49 |
-
def get_menu_items(sf):
|
50 |
-
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
51 |
-
result = sf.query(query)
|
52 |
-
return result['records']
|
53 |
-
|
54 |
# Voice-related functions
|
55 |
def generate_audio_prompt(text, filename):
|
56 |
try:
|
@@ -85,40 +88,49 @@ def index():
|
|
85 |
|
86 |
@app.route("/dashboard", methods=["GET"])
|
87 |
def dashboard():
|
|
|
88 |
return render_template("dashboard.html") # Render the dashboard template
|
89 |
|
90 |
@app.route("/menu", methods=["GET"])
|
91 |
def menu_page():
|
|
|
92 |
menu_items = get_menu_items(sf) # Fetch menu items from Salesforce
|
93 |
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
|
94 |
return render_template("menu_page.html", menu_items=menu_data)
|
95 |
|
96 |
@app.route("/cart", methods=["GET"])
|
97 |
def cart():
|
|
|
98 |
cart_items = session.get('cart_items', [])
|
99 |
return render_template("cart_page.html", cart_items=cart_items)
|
100 |
|
101 |
@app.route("/order-summary", methods=["GET"])
|
102 |
def order_summary():
|
|
|
103 |
order_details = session.get('cart_items', [])
|
104 |
total_price = sum(item['price'] * item['quantity'] for item in order_details)
|
105 |
return render_template("order_summary.html", order_details=order_details, total_price=total_price)
|
106 |
|
107 |
@app.route("/final_order", methods=["GET"])
|
108 |
def final_order():
|
|
|
109 |
cart_items = session.get('cart_items', [])
|
110 |
total_price = sum(item['price'] * item['quantity'] for item in cart_items)
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
119 |
|
120 |
@app.route("/add_to_cart", methods=["POST"])
|
121 |
def add_to_cart():
|
|
|
122 |
item_name = request.json.get('item_name')
|
123 |
quantity = request.json.get('quantity')
|
124 |
|
|
|
36 |
except Exception as e:
|
37 |
raise Exception(f"Failed to create record: {str(e)}")
|
38 |
|
39 |
+
def get_menu_items(sf):
|
40 |
+
# Query to get menu items from Salesforce (assuming Menu_Item__c is the Salesforce object)
|
41 |
+
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
42 |
+
result = sf.query(query)
|
43 |
+
return result['records']
|
44 |
+
|
45 |
+
def create_salesforce_order(sf, cart_items, total_price, customer_id):
|
46 |
+
# Creating an order record in Salesforce after the customer places the final order
|
47 |
try:
|
48 |
order = sf.Order__c.create({
|
49 |
'Total_Price__c': total_price,
|
50 |
+
'Cart_Items__c': json.dumps(cart_items), # Storing cart items as JSON (you can refine this as needed)
|
51 |
+
'Customer__c': customer_id # Linking to the customer record
|
52 |
})
|
53 |
return order
|
54 |
except Exception as e:
|
55 |
raise Exception(f"Failed to create order: {str(e)}")
|
56 |
|
|
|
|
|
|
|
|
|
|
|
57 |
# Voice-related functions
|
58 |
def generate_audio_prompt(text, filename):
|
59 |
try:
|
|
|
88 |
|
89 |
@app.route("/dashboard", methods=["GET"])
|
90 |
def dashboard():
|
91 |
+
# This route will render the dashboard page
|
92 |
return render_template("dashboard.html") # Render the dashboard template
|
93 |
|
94 |
@app.route("/menu", methods=["GET"])
|
95 |
def menu_page():
|
96 |
+
# Fetch the menu items from Salesforce
|
97 |
menu_items = get_menu_items(sf) # Fetch menu items from Salesforce
|
98 |
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
|
99 |
return render_template("menu_page.html", menu_items=menu_data)
|
100 |
|
101 |
@app.route("/cart", methods=["GET"])
|
102 |
def cart():
|
103 |
+
# Retrieve cart items from the session
|
104 |
cart_items = session.get('cart_items', [])
|
105 |
return render_template("cart_page.html", cart_items=cart_items)
|
106 |
|
107 |
@app.route("/order-summary", methods=["GET"])
|
108 |
def order_summary():
|
109 |
+
# Retrieve order details from session
|
110 |
order_details = session.get('cart_items', [])
|
111 |
total_price = sum(item['price'] * item['quantity'] for item in order_details)
|
112 |
return render_template("order_summary.html", order_details=order_details, total_price=total_price)
|
113 |
|
114 |
@app.route("/final_order", methods=["GET"])
|
115 |
def final_order():
|
116 |
+
# Process the final order and create an order record in Salesforce
|
117 |
cart_items = session.get('cart_items', [])
|
118 |
total_price = sum(item['price'] * item['quantity'] for item in cart_items)
|
119 |
+
customer_id = session.get('customer_id', None) # Get customer ID from session
|
120 |
+
|
121 |
+
if customer_id:
|
122 |
+
try:
|
123 |
+
order = create_salesforce_order(sf, cart_items, total_price, customer_id)
|
124 |
+
session.pop('cart_items', None) # Clear cart after placing the order
|
125 |
+
return render_template("final_order.html", order_details=cart_items, total_price=total_price)
|
126 |
+
except Exception as e:
|
127 |
+
return jsonify({'error': f'Failed to create order in Salesforce: {str(e)}'}), 500
|
128 |
+
else:
|
129 |
+
return jsonify({'error': 'Customer not logged in'}), 400
|
130 |
|
131 |
@app.route("/add_to_cart", methods=["POST"])
|
132 |
def add_to_cart():
|
133 |
+
# Add item to the cart (example for cart item data: item_name, quantity)
|
134 |
item_name = request.json.get('item_name')
|
135 |
quantity = request.json.get('quantity')
|
136 |
|