~martijnbraam/shipments

7b4f4c61732035448838fdb6280ec0c97d0fb480 — Ferenc Géczi 2 years ago 02c7e4f
carrier: ups: Handle case when package is not found
2 files changed, 32 insertions(+), 23 deletions(-)

M shipments/carrier.py
M shipments_gtk/window.py
M shipments/carrier.py => shipments/carrier.py +31 -22
@@ 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']

M shipments_gtk/window.py => shipments_gtk/window.py +1 -1
@@ 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 '')