Update cart.py
Browse files
cart.py
CHANGED
|
@@ -413,4 +413,66 @@ def submit_order():
|
|
| 413 |
|
| 414 |
except Exception as e:
|
| 415 |
print(f"Error during order submission: {str(e)}")
|
| 416 |
-
return jsonify({"success": False, "error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
except Exception as e:
|
| 415 |
print(f"Error during order submission: {str(e)}")
|
| 416 |
+
return jsonify({"success": False, "error": str(e)}), 500
|
| 417 |
+
|
| 418 |
+
@cart_blueprint.route("/order", methods=["GET"])
|
| 419 |
+
def order():
|
| 420 |
+
email = session.get('user_email')
|
| 421 |
+
if not email:
|
| 422 |
+
return redirect(url_for("login"))
|
| 423 |
+
|
| 424 |
+
try:
|
| 425 |
+
sanitized_email = sanitize_input(email)
|
| 426 |
+
# Fetch the latest order for the user
|
| 427 |
+
result = sf.query(f"""
|
| 428 |
+
SELECT Id, Order_Details__c, Total_Bill__c, CreatedDate
|
| 429 |
+
FROM Order__c
|
| 430 |
+
WHERE Customer_Email__c = '{sanitized_email}'
|
| 431 |
+
ORDER BY CreatedDate DESC
|
| 432 |
+
LIMIT 1
|
| 433 |
+
""")
|
| 434 |
+
orders = result.get("records", [])
|
| 435 |
+
if not orders:
|
| 436 |
+
return render_template("order.html", order=None, message="No orders found.")
|
| 437 |
+
|
| 438 |
+
order = orders[0]
|
| 439 |
+
order_id = order['Id']
|
| 440 |
+
total = order['Total_Bill__c']
|
| 441 |
+
order_details_raw = order['Order_Details__c'] or ""
|
| 442 |
+
|
| 443 |
+
# Parse order details into a list of items
|
| 444 |
+
order_items = []
|
| 445 |
+
for line in order_details_raw.split("\n"):
|
| 446 |
+
if not line.strip():
|
| 447 |
+
continue
|
| 448 |
+
# Example line: "Chicken Biryani x2 | Add-Ons: Extra Raita | Instructions: Less spicy | Price: $24.00 | Image: <url>"
|
| 449 |
+
try:
|
| 450 |
+
parts = line.split(" | ")
|
| 451 |
+
name_quantity = parts[0].split(" x") # "Chicken Biryani x2" -> ["Chicken Biryani", "2"]
|
| 452 |
+
name = name_quantity[0]
|
| 453 |
+
quantity = int(name_quantity[1])
|
| 454 |
+
addons = parts[1].replace("Add-Ons: ", "") if len(parts) > 1 else "None"
|
| 455 |
+
instructions = parts[2].replace("Instructions: ", "") if len(parts) > 2 else "None"
|
| 456 |
+
price = float(parts[3].replace("Price: $", "")) if len(parts) > 3 else 0
|
| 457 |
+
image = parts[4].replace("Image: ", "") if len(parts) > 4 else "/static/placeholder.jpg"
|
| 458 |
+
order_items.append({
|
| 459 |
+
"name": name,
|
| 460 |
+
"quantity": quantity,
|
| 461 |
+
"addons": addons,
|
| 462 |
+
"instructions": instructions,
|
| 463 |
+
"price": price,
|
| 464 |
+
"image": image
|
| 465 |
+
})
|
| 466 |
+
except Exception as e:
|
| 467 |
+
print(f"Error parsing order line '{line}': {e}")
|
| 468 |
+
continue
|
| 469 |
+
|
| 470 |
+
return render_template(
|
| 471 |
+
"order.html",
|
| 472 |
+
order={"id": order_id, "items": order_items, "total": total},
|
| 473 |
+
message="Thank you for your order! It has been placed successfully."
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
+
except Exception as e:
|
| 477 |
+
print(f"Error fetching order: {e}")
|
| 478 |
+
return render_template("order.html", order=None, message="Error fetching your order. Please contact support.")
|