~martijnbraam/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH shipments] carrier: ups: Handle case when package is not found

Details
Message ID
<164286978985.1016.5570105634289761179-0@git.sr.ht>
DKIM signature
missing
Download raw message
Patch: +32 -23
From: Ferenc Géczi <ferenc.gm@gmail.com>

---
 shipments/carrier.py    | 53 ++++++++++++++++++++++++-----------------
 shipments_gtk/window.py |  2 +-
 2 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/shipments/carrier.py b/shipments/carrier.py
index 0dfdf67..948c353 100644
--- a/shipments/carrier.py
+++ b/shipments/carrier.py
@@ -225,40 +225,49 @@ class UPS(Carrier):
            'X-XSRF-TOKEN': token
        })
        payload = response.json()
        result = PackageInfo(code, 'UPS')
        result.carrier_name = 'UPS'
        result.status_category = StatusCategory.ERROR

        if payload['trackDetails'] is None:
            result = PackageInfo(code, 'UPS')
            result.status = payload['statusText']
            result.carrier_name = 'UPS'
            result.status_category = StatusCategory.ERROR
            result.status = payload['statusText'] or ''
            return result

        details = payload['trackDetails'][0]
        result = PackageInfo(code, 'UPS')
        result.carrier_name = 'UPS'

        # errorCode == 504 goes with
        # errorText == 'Tracking number not found in database'
        if details.get('errorCode') and details['errorCode'] == '504':
            result.status = details['errorText']
            return result
            

        result.tracking_url = 'https://www.ups.com/track?loc=en_EN&requester=DAN&tracknum=' + code
        result.status = details['packageStatus']
        result.status = details['packageStatus'] or ''
        result.status_category = StatusCategory.LABEL_CREATED
        if details['progressBarType'] == 'InTransit':
            result.status_category = StatusCategory.IN_TRANSIT
        elif details['progressBarType'] == 'Delivered':
            result.status_category = StatusCategory.DELIVERED

        for event in details['shipmentProgressActivities']:
            better_time = event['time'].replace('.M.', 'M').zfill(8)
            dt = event['date'] + ' ' + better_time
            pdt = datetime.strptime(dt, '%m/%d/%Y %I:%M %p')
            label = 'No details'
            if event['actCode'] == 'MP':
                label = 'Label created'
            elif event['actCode'] == 'OF':
                label = 'Out for delivery'
            if event['milestoneName'] is not None and 'name' in event['milestoneName']:
                label = event['milestoneName']['name']
            elif 'activityScan' in event and event['activityScan'] is not None:
                label = event['activityScan']
            e = PackageEvent(pdt, event['location'], label)
            result.events.append(e)

        # Avoid TypeError: 'NoneType' object is not iterable
        if details['shipmentProgressActivities']:
            for event in details['shipmentProgressActivities']:
                better_time = event['time'].replace('.M.', 'M').zfill(8)
                dt = event['date'] + ' ' + better_time
                pdt = datetime.strptime(dt, '%m/%d/%Y %I:%M %p')
                if event['actCode'] == 'MP':
                    label = 'Label created'
                elif event['actCode'] == 'OF':
                    label = 'Out for delivery'
                if event['milestoneName'] is not None and 'name' in event['milestoneName']:
                    label = event['milestoneName']['name']
                elif 'activityScan' in event and event['activityScan'] is not None:
                    label = event['activityScan']
                e = PackageEvent(pdt, event['location'], label)
                result.events.append(e)


        if details['shipToAddress'] is not None:
            a = details['shipToAddress']
diff --git a/shipments_gtk/window.py b/shipments_gtk/window.py
index 4b0361d..78c39de 100644
--- a/shipments_gtk/window.py
+++ b/shipments_gtk/window.py
@@ -314,7 +314,7 @@ class ShipmentsWindow:

        self.package_label.set_text(package['label'])
        self.package_code.set_text(package['code'])
        self.package_status.set_text(package['status'])
        self.package_status.set_text(package['status'] or '')
        self.package_courier.set_text(package['courier_display'])
        self.package_delivery_note.set_text(package['note'] if package['note'] is not None else 'n/a')
        self.package_description.set_text(package['description'] if package['description'] is not None else '')
-- 
2.32.0
Reply to thread Export thread (mbox)