Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from flask.sessions import SecureCookieSessionInterface # Import the class
|
|
| 4 |
from salesforce import get_salesforce_connection
|
| 5 |
from datetime import timedelta
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
# Initialize Flask app and Salesforce connection
|
| 9 |
print("Starting app...")
|
|
@@ -248,72 +249,6 @@ def update_profile():
|
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
-
from datetime import datetime
|
| 252 |
-
import pytz # Library to handle timezone conversions
|
| 253 |
-
|
| 254 |
-
@app.route("/order-history", methods=["GET"])
|
| 255 |
-
def order_history():
|
| 256 |
-
email = session.get('user_email') # Get logged-in user's email
|
| 257 |
-
if not email:
|
| 258 |
-
return redirect(url_for("login"))
|
| 259 |
-
|
| 260 |
-
try:
|
| 261 |
-
# Fetch past orders for the user
|
| 262 |
-
result = sf.query(f"""
|
| 263 |
-
SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c,
|
| 264 |
-
Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c, CreatedDate
|
| 265 |
-
FROM Order__c
|
| 266 |
-
WHERE Customer_Email__c = '{email}'
|
| 267 |
-
ORDER BY CreatedDate DESC
|
| 268 |
-
""")
|
| 269 |
-
|
| 270 |
-
print(f"Salesforce query result: {result}") # Debugging line
|
| 271 |
-
|
| 272 |
-
orders = result.get("records", []) # Fetch all orders
|
| 273 |
-
|
| 274 |
-
if not orders:
|
| 275 |
-
print("No orders found for this email.") # Debugging line
|
| 276 |
-
|
| 277 |
-
# Format the order details for better readability
|
| 278 |
-
for order in orders:
|
| 279 |
-
order_details = order.get("Order_Details__c", "")
|
| 280 |
-
items = order_details.split("\n") # Assuming each item is separated by a new line
|
| 281 |
-
formatted_items = []
|
| 282 |
-
|
| 283 |
-
# Loop through the items and format them as "item name * quantity"
|
| 284 |
-
for item in items:
|
| 285 |
-
item_details = item.split(" | ")
|
| 286 |
-
if len(item_details) > 1:
|
| 287 |
-
name = item_details[0].strip()
|
| 288 |
-
quantity = item_details[1].strip()
|
| 289 |
-
formatted_items.append(f"{name} * {quantity}")
|
| 290 |
-
|
| 291 |
-
# Join the formatted items into a single string
|
| 292 |
-
order['formatted_items'] = ", ".join(formatted_items)
|
| 293 |
-
|
| 294 |
-
# Get the order date and time from CreatedDate
|
| 295 |
-
created_date = order.get("CreatedDate", "")
|
| 296 |
-
if created_date:
|
| 297 |
-
# Convert CreatedDate to datetime object in UTC
|
| 298 |
-
utc_datetime = datetime.strptime(created_date, '%Y-%m-%dT%H:%M:%S.000+0000')
|
| 299 |
-
utc_datetime = utc_datetime.replace(tzinfo=pytz.UTC)
|
| 300 |
-
|
| 301 |
-
# Convert UTC datetime to the desired timezone (e.g., IST)
|
| 302 |
-
local_timezone = pytz.timezone('Asia/Kolkata') # Replace with your timezone
|
| 303 |
-
local_datetime = utc_datetime.astimezone(local_timezone)
|
| 304 |
-
|
| 305 |
-
# Format the date and time in the desired format
|
| 306 |
-
order['formatted_date'] = local_datetime.strftime('%B %d, %I:%M %p')
|
| 307 |
-
|
| 308 |
-
order_status = order.get("Order_Status__c", "N/A") # Default to "N/A" if no status
|
| 309 |
-
order['order_status'] = order_status
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
return render_template("order_history.html", orders=orders)
|
| 313 |
-
|
| 314 |
-
except Exception as e:
|
| 315 |
-
print(f"Error fetching order history: {str(e)}")
|
| 316 |
-
return render_template("order_history.html", orders=[], error=str(e))
|
| 317 |
|
| 318 |
|
| 319 |
app.permanent_session_lifetime = timedelta(minutes=5)
|
|
|
|
| 4 |
from salesforce import get_salesforce_connection
|
| 5 |
from datetime import timedelta
|
| 6 |
import os
|
| 7 |
+
import orderhistory
|
| 8 |
|
| 9 |
# Initialize Flask app and Salesforce connection
|
| 10 |
print("Starting app...")
|
|
|
|
| 249 |
|
| 250 |
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
|
| 254 |
app.permanent_session_lifetime = timedelta(minutes=5)
|