def _extract_details(shipment_node: Element, settings: Settings) -> TrackingDetails: track_detail = XP.build(ShipmentType, shipment_node) activities = [ XP.build(ActivityType, node) for node in shipment_node.xpath(".//*[local-name() = $name]", name="Activity") ] delivered = any(a.Status.Type == 'D' for a in activities) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=track_detail.InquiryNumber.Value, events=list( map( lambda a: TrackingEvent( date=DF.fdate(a.Date, "%Y%m%d"), description=a.Status.Description if a.Status else None, location=(_format_location(a.ActivityLocation.Address) if a.ActivityLocation is not None and a. ActivityLocation.Address is not None else None), time=DF.ftime(a.Time, "%H%M%S"), code=a.Status.Code if a.Status else None, ), activities, )), delivered=delivered)
def _extract_tracking(shipment_node: Element, settings: Settings) -> TrackingDetails: track_detail = ShipmentType() track_detail.build(shipment_node) activity_nodes = shipment_node.xpath(".//*[local-name() = $name]", name="Activity") def build_activity(node) -> ActivityType: activity = ActivityType() activity.build(node) return activity activities: List[ActivityType] = list(map(build_activity, activity_nodes)) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=track_detail.InquiryNumber.Value, events=list( map( lambda a: TrackingEvent( date=format_date(a.Date, "%Y%m%d"), time=format_time(a.Time, "%H%M%S"), code=a.Status.Code if a.Status else None, location=(a.ActivityLocation.Address.City if a.ActivityLocation and a.ActivityLocation. Address else None), description=a.Status.Description if a.Status else None, ), activities, )), )
def _extract_details(node: Element, settings) -> TrackingDetails: info = XP.to_object(TrackInfoType, node) details: List[TrackDetailType] = [ *([info.TrackSummary] or []), *info.TrackDetail ] delivered = info.StatusCategory.lower() == "delivered" return TrackingDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=info.ID, events=[ TrackingEvent( code=str(event.EventCode), date=DF.fdate(event.EventDate, "%B %d, %Y"), time=DF.ftime(event.EventTime, "%H:%M %p"), description=event.Event, location=", ".join([ location for location in [ event.EventCity, event.EventState, event.EventCountry, str(event.EventZIPCode), ] if location is not None ]), ) for event in details ], delivered=delivered)
def _extract_tracking( track_detail_node: Element, settings: Settings ) -> Optional[TrackingDetails]: track_detail = TrackDetail() track_detail.build(track_detail_node) if track_detail.Notification.Severity == "ERROR": return None delivered = any(e.EventType == 'DL' for e in track_detail.Events) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=track_detail.TrackingNumber, events=list( map( lambda e: TrackingEvent( date=DF.fdate(e.Timestamp, "%Y-%m-%d %H:%M:%S%z"), time=DF.ftime(e.Timestamp, "%Y-%m-%d %H:%M:%S%z"), code=e.EventType, location=e.ArrivalLocation, description=e.EventDescription, ), track_detail.Events, ) ), delivered=delivered )
def _extract_tracking_details(node: Element, settings: Settings) -> TrackingDetails: result = XP.build(TrackingResult, node) is_en = settings.language == "en" events = [ TrackingEvent( date=DF.fdate(event.local_date_time, '%Y%m%d %H%M%S'), description=(event.code_description_en if is_en else event.code_description_fr), location=SF.concat_str(event.address.address_line_1, event.address.address_line_2, event.address.city, event.address.province, event.address.country, join=True, separator=", "), code=event.code, time=DF.ftime(event.local_date_time, '%Y%m%d %H%M%S'), ) for event in cast(List[CanparTrackingEvent], result.events) ] return TrackingDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=result.barcode, events=events, delivered=any(event.code == 'DEL' for event in events))
def _extract_tracking(info_node: Element, settings: Settings) -> Optional[TrackingDetails]: info = AWBInfo() info.build(info_node) if info.ShipmentInfo is None: return None delivered = any(e.ServiceEvent.EventCode == 'OK' for e in info.ShipmentInfo.ShipmentEvent) return TrackingDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=info.AWBNumber, events=list( map( lambda e: TrackingEvent( date=DF.fdate(e.Date), time=DF.ftime(e.Time), code=e.ServiceEvent.EventCode, location=e.ServiceArea.Description, description=e.ServiceEvent.Description, ), reversed(info.ShipmentInfo.ShipmentEvent or []), )), delivered=delivered)
def _extract_tracking( info_node: Element, settings: Settings ) -> Optional[TrackingDetails]: info = AWBInfo() info.build(info_node) if info.ShipmentInfo is None: return None return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=info.AWBNumber, events=list( map( lambda e: TrackingEvent( date=format_date(e.Date), time=format_time(e.Time), signatory=e.Signatory, code=e.ServiceEvent.EventCode, location=e.ServiceArea.Description, description=e.ServiceEvent.Description, ), info.ShipmentInfo.ShipmentEvent, ) ), )
def _extract_tracking(tracking_node: Element, settings) -> TrackingDetails: tracking: TrackInfoType = TrackInfoType() tracking.build(tracking_node) track_detail_nodes = tracking_node.xpath(".//*[local-name() = $name]", name="TrackDetail") details: List[TrackDetailType] = [ (lambda t: (t, t.build(detail)))(TrackDetailType())[0] for detail in track_detail_nodes ] return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=tracking.TrackInfoID, events=[ TrackingEvent( code=str(event.EventCode), date=format_date(event.EventDate, "%B %d, %Y"), time=format_time(event.EventTime, "%H:%M %p"), description=event.ActionCode, location=", ".join([ location for location in [ event.EventCity, event.EventState, event.EventCountry, str(event.EventZIPCode), ] if location is not None ]), ) for event in details ], )
def _extract_detail(detail: dict, settings: Settings) -> TrackingDetails: order_info = DP.to_object(OrderInfo, detail) return TrackingDetails( # context info carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=order_info.TrackingNumber, events=[ TrackingEvent(date=time.strftime('%Y-%m-%d'), description=order_info.msg) ], )
def _extract_detail(detail: Tracking, settings: Settings) -> TrackingDetails: return TrackingDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=detail.trackingNumber, events=[ TrackingEvent( date=DF.fdate(event.activityDate, '%Y-%m-%dT%H:%M:%SZ'), description=event.statusDetail, location=event.terminal, code=event.status, time=DF.ftime(event.activityDate, '%Y-%m-%dT%H:%M:%SZ'), ) for event in detail.activities ])
def _extract_detail(detail: MailPieces, settings: Settings) -> TrackingDetails: return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=detail.mailPieceId, events=[ TrackingEvent( date=DF.fdate(event.eventDateTime, '%Y-%m-%dT%H:%M:%S%z'), description=event.eventName, location=event.locationName, code=event.eventCode, time=DF.ftime(event.eventDateTime, '%Y-%m-%dT%H:%M:%S%z'), ) for event in detail.events ], )
def _extract_detail(detail: RouteResp, settings: Settings) -> TrackingDetails: return TrackingDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=detail.mailNo, events=[ TrackingEvent( date=DF.fdate(event.acceptTime, "%Y-%m-%d %H:%M:%S"), description=event.remark, location=event.acceptAddress, code=event.opCode, time=DF.ftime(event.acceptTime, "%Y-%m-%d %H:%M:%S"), ) for event in detail.routes ])
def _extract_detail(detail: dict, settings: Settings) -> TrackingDetails: result = DP.to_object(Result, detail) return TrackingDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=result.tracking_number, events=[ TrackingEvent( date=DF.fdate(event.time_stamp, '2019-08-15T18:52:19'), description=event.message, location=event.location, code=event.tracking_status, time=DF.ftime(event.time_stamp, '2019-08-15T18:52:19'), ) for event in result.checkpoints ], delivered=(result.tracking_status == 'LM40'))
def _extract_detail(detail: Shipment, settings: Settings) -> TrackingDetails: return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=str(detail.id), events=[ TrackingEvent( date=DF.fdate(event.timestamp, '%Y-%m-%dT%H:%M:%S'), description=event.description, location=(event.location.address.addressLocality if event.location is not None and event.location.address is not None else None), code=event.statusCode, time=DF.ftime(event.timestamp, '%Y-%m-%dT%H:%M:%S'), ) for event in detail.events ], delivered=detail.status.status.lower() == "delivered")
def _extract_detail(node: Element, settings: Settings) -> TrackingDetails: detail = XP.build(TrackingResult, node) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=detail.WaybillNumber, events=[ TrackingEvent( date=DF.date(detail.UpdateDateTime, '%Y-%m-%dT%H:%M:%S'), description=detail.UpdateDescription, location=detail.UpdateLocation, code=detail.UpdateCode, time=DF.ftime(detail.UpdateDateTime, '%Y-%m-%dT%H:%M:%S'), ) ], )
def _extract_tracking(node: Element, settings: Settings) -> TrackingDetails: track = TrackingInformation() track.build(node) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=str(track.PIN.Value), events=[ TrackingEvent( date=format_date(cast(Scan, scan).ScanDate), time=format_timestamp(cast(Scan, scan).ScanTime), description=cast(Scan, scan).Description, location=cast(Depot, cast(Scan, scan).Depot).Name, code=cast(Scan, scan).ScanType, ) for scan in track.Scans.Scan ], )
def _extract_detail(detail: Tuple[str, Tracking], settings: Settings) -> TrackingDetails: tracking_number, tracking_details = detail return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=tracking_number, events=[ TrackingEvent( date=DF.fdate(event.scan_time, '%Y-%m-%dT%H:%M:%SZ'), description=event.description, location=event.location, code=event.event_type, time=DF.ftime(event.scan_time, '%Y-%m-%dT%H:%M:%SZ'), ) for event in reversed(tracking_details.tracking_events) ], delivered=(tracking_details.state == 'Delivered'))
def _extract_detail(detail: TrackingResult, settings: Settings) -> TrackingDetails: item = detail.trackable_items[0] return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=detail.tracking_id, events=[ TrackingEvent( date=DF.fdate(event.date, '%Y-%m-%dT%H:%M:%S%z'), description=event.description, location=event.location, time=DF.ftime(event.date, '%Y-%m-%dT%H:%M:%S%z'), ) for event in item.events ], delivered=(detail.status == 'Delivered') )
def _extract_tracking(pin_summary_node: Element, settings: Settings) -> TrackingDetails: pin_summary_ = pin_summary() pin_summary_.build(pin_summary_node) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=pin_summary_.pin, events=[ TrackingEvent( date=format_date(pin_summary_.event_date_time, "%Y%m%d:%H%M%S"), time=format_time(pin_summary_.event_date_time, "%Y%m%d:%H%M%S"), signatory=pin_summary_.signatory_name, code=pin_summary_.event_type, location=pin_summary_.event_location, description=pin_summary_.event_description, ) ], )
def _extract_tracking(node: Element, settings: Settings) -> TrackingDetails: track = XP.to_object(TrackingInformation, node) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=str(track.PIN.Value), events=[ TrackingEvent( date=DF.fdate(scan.ScanDate), time=DF.ftime(scan.ScanTime, '%H%M%S'), description=scan.Description, location=scan.Depot.Name, code=scan.ScanType, ) for scan in track.Scans.Scan ], delivered=any(scan.ScanType == "Delivery" for scan in track.Scans.Scan) )
def _extract_detail(node: Element, settings: Settings) -> TrackingDetails: detail = XP.build(ConsignmentType, node) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=detail.ConsignmentNumber, events=[ TrackingEvent( date=DF.fdate(status.LocalEventDate.valueOf_, '%Y%m%d'), description=status.StatusDescription, location=SF.concat_str(status.Depot, status.DepotName, join=True, separator='-'), code=status.StatusCode, time=DF.ftime(status.LocalEventTime.valueOf_, '%H%M') ) for status in cast(List[StatusStructure], detail.StatusData) ], delivered=(detail.SummaryCode == "DEL") )
def _extract_detail(detail: dict, settings: Settings) -> TrackingDetails: tracking = DP.to_object(Tracking, detail) delivered = (tracking.trackingMilestoneEvents[0].eventCode == 'Delivered' if len(tracking.trackingMilestoneEvents) > 0 else False) return TrackingDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=tracking.trackingNumberVendor, events=[ TrackingEvent( date=DF.fdate(event.eventOn, _date_format(event.eventOn)), time=DF.ftime(event.eventOn, _date_format(event.eventOn)), description=event.eventDescription, location=event.eventLocation, code=event.eventCode, ) for event in tracking.trackingMilestoneEvents ], delivered=delivered)
def _extract_tracking(detail_node: Element, settings: Settings) -> TrackingDetails: pin = XP.find("pin", detail_node, first=True) events: List[occurrenceType] = XP.find("occurrence", detail_node, occurrenceType) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=pin.text, events=[ TrackingEvent( date=DF.fdate(event.event_date, "%Y-%m-%d"), time=DF.ftime(event.event_time, "%H:%M:%S"), code=event.event_identifier, location=SF.concat_str(event.event_site, event.event_province, join=True, separator=', '), description=event.event_description ) for event in events ], delivered=any(e.event_identifier in TRACKING_DELIVERED_EVENT_CODES for e in events) )
def _extract_tracking_details(infos: Tuple[str, TrackingInfo], settings: Settings) -> TrackingDetails: tracking_number, events = infos ordered_events = [ *reversed( sorted(events, key=lambda e: DF.date(e.StatusDate, "%Y-%m-%dT%H:%M:%S"))) ] return TrackingDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=tracking_number, events=[ TrackingEvent( date=DF.fdate(event.StatusDate, "%Y-%m-%dT%H:%M:%S"), time=DF.ftime(event.StatusDate, "%Y-%m-%dT%H:%M:%S"), description=event.Status, ) for event in ordered_events ], delivered=('Delivered' in ordered_events[0].Status))
def _extract_tracking(info_node: Element, settings: Settings) -> Optional[TrackingDetails]: tracking_number = XP.find('AWBNumber', info_node, first=True).text events: List[ShipmentEvent] = XP.find('ShipmentEvent', info_node, ShipmentEvent) delivered = any(e.ServiceEvent.EventCode == 'OK' for e in events) return TrackingDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=tracking_number, events=[ TrackingEvent( date=DF.fdate(e.Date), time=DF.ftime(e.Time), code=e.ServiceEvent.EventCode, location=e.ServiceArea.Description, description=f'{e.ServiceEvent.Description} {e.Signatory}'. strip(), ) for e in reversed(events) ], delivered=delivered)
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertDictEqual(response_data, TRACKING_RESPONSE) self.assertEqual(len(self.user.tracking_set.all()), 1) RETURNED_VALUE = ( [ TrackingDetails( carrier_id="ups_package", carrier_name="ups", tracking_number="1Z12345E6205277936", delivered=False, events=[ TrackingEvent( code="KB", date="2010-08-30", description="UPS INTERNAL ACTIVITY CODE", location="BONN", time="10:39", ) ], ) ], [], ) TRACKING_RESPONSE = { "id": ANY, "carrier_id":
response_data = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertDictEqual(response_data, UPDATED_TRACKERS_LIST) RETURNED_VALUE = ( [ TrackingDetails( carrier_id="ups_package", carrier_name="ups", tracking_number="1Z12345E6205277936", delivered=False, events=[ TrackingEvent( code="KB", date="2010-08-30", description="UPS INTERNAL ACTIVITY CODE", location="BONN", time="10:39", ) ], ) ], [], ) TRACKING_RESPONSE = { "id": ANY, "carrier_id":