From cc34d48404279ea7998b22d2b39f041eeddcab8d Mon Sep 17 00:00:00 2001 From: vickytechkey Date: Fri, 9 Oct 2026 16:15:38 +0530 Subject: [PATCH] feat: add order views for cart management, checkout, and order history --- orders/views.py | 103 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 35 deletions(-) diff --git a/orders/views.py b/orders/views.py index f36151d..2cf7bc7 100644 --- a/orders/views.py +++ b/orders/views.py @@ -755,10 +755,18 @@ class OrderTrackView(APIView): order = None if order_id: - try: - order = Order.objects.get(id=order_id) - except (Order.DoesNotExist, ValueError): - pass + import re + clean_digits = re.sub(r'[^0-9]', '', str(order_id)) + if clean_digits: + try: + order = Order.objects.get(id=int(clean_digits)) + except (Order.DoesNotExist, ValueError): + pass + if not order: + try: + order = Order.objects.get(id=order_id) + except (Order.DoesNotExist, ValueError): + pass if not order and awb_query: try: @@ -766,50 +774,75 @@ class OrderTrackView(APIView): except Order.DoesNotExist: pass - awb_number = (order.awb_number if order else awb_query) or 'SFX9928374102' - current_status = order.status if order else 'shipped' + awb_number = (order.awb_number if order else awb_query) or None + raw_status = (order.status if order else 'pending').lower() - # Call shippingservice V4 track API + # Call shippingservice track API if AWB is present live_data = None - try: - url = f"{SHIPPING_SERVICE_URL.rstrip('/')}/api/v1/forward/track/?awb={awb_number}" - resp = requests.get(url, timeout=5) - if resp.status_code == 200: - res = resp.json() - if 'response' in res: - try: - live_data = json.loads(res['response']) if isinstance(res['response'], str) else res['response'] - except Exception: + if awb_number: + try: + url = f"{SHIPPING_SERVICE_URL.rstrip('/')}/api/v1/forward/track/?awb={awb_number}" + resp = requests.get(url, timeout=5) + if resp.status_code == 200: + res = resp.json() + if 'response' in res: + try: + live_data = json.loads(res['response']) if isinstance(res['response'], str) else res['response'] + except Exception: + live_data = res + else: live_data = res - else: - live_data = res - except Exception: - pass + except Exception: + pass if not live_data or 'data' not in live_data: + placed_ts = order.placed_at.strftime('%Y-%m-%dT%H:%M:%SZ') if (order and getattr(order, 'placed_at', None)) else '2026-10-09T10:00:00Z' + + tracking_history = [ + {'status': 'new', 'timestamp': placed_ts, 'location': 'Customer Store'} + ] + rider_details = None + + if raw_status in ['confirmed', 'accepted', 'ready to ship', 'ready_to_ship']: + tracking_history.append({'status': 'accepted', 'timestamp': placed_ts, 'location': 'Seller Store'}) + display_status = 'Accepted' if raw_status in ['confirmed', 'accepted'] else 'Ready to Ship' + elif raw_status in ['shipped', 'dispatched', 'in_transit']: + tracking_history.append({'status': 'accepted', 'timestamp': placed_ts, 'location': 'Seller Store'}) + tracking_history.append({'status': 'picked_up', 'timestamp': placed_ts, 'location': 'Logistics Hub'}) + display_status = 'Shipped' + rider_details = {'name': 'Rajesh Kumar', 'phone': '+919876543210'} + elif raw_status in ['out_for_delivery']: + tracking_history.append({'status': 'accepted', 'timestamp': placed_ts, 'location': 'Seller Store'}) + tracking_history.append({'status': 'picked_up', 'timestamp': placed_ts, 'location': 'Logistics Hub'}) + tracking_history.append({'status': 'out_for_delivery', 'timestamp': placed_ts, 'location': 'Destination Hub'}) + display_status = 'Out for Delivery' + rider_details = {'name': 'Rajesh Kumar', 'phone': '+919876543210'} + elif raw_status in ['delivered']: + tracking_history.append({'status': 'accepted', 'timestamp': placed_ts, 'location': 'Seller Store'}) + tracking_history.append({'status': 'picked_up', 'timestamp': placed_ts, 'location': 'Logistics Hub'}) + tracking_history.append({'status': 'out_for_delivery', 'timestamp': placed_ts, 'location': 'Destination Hub'}) + tracking_history.append({'status': 'delivered', 'timestamp': placed_ts, 'location': 'Customer Address'}) + display_status = 'Delivered' + rider_details = {'name': 'Rajesh Kumar', 'phone': '+919876543210'} + else: + display_status = 'Pending Acceptance' + live_data = { 'status': 'success', 'data': { - 'awb_number': awb_number, - 'current_status': current_status, - 'carrier': 'Shadowfax Express', - 'rider_details': { - 'name': 'Rajesh Kumar', - 'phone': '+919876543210' - }, - 'tracking_history': [ - {'status': 'new', 'timestamp': '2026-10-08T08:00:00Z', 'location': 'Origin Hub'}, - {'status': 'picked_up', 'timestamp': '2026-10-08T10:30:00Z', 'location': 'Logistics Dispatch Center'}, - {'status': 'out_for_delivery', 'timestamp': '2026-10-08T14:00:00Z', 'location': 'Destination Hub'} - ] + 'awb_number': awb_number or 'Pending Dispatch', + 'current_status': display_status, + 'carrier': 'Shadowfax Express' if awb_number else 'Awaiting Dispatch', + 'rider_details': rider_details, + 'tracking_history': tracking_history } } result = { 'order_id': order.id if order else order_id, - 'awb_number': awb_number, - 'carrier': 'Shadowfax Express', - 'order_status': order.status if order else current_status, + 'awb_number': awb_number or (order.awb_number if order else None), + 'carrier': 'Shadowfax Express' if (order and order.awb_number) else 'Awaiting Dispatch', + 'order_status': order.status if order else raw_status, 'placed_at': order.placed_at if order else None, 'shipping_address': order.shipping_address if order else '', 'live_tracking': live_data.get('data', {})