예제 #1
0
    def update(self, instance: models.Pickup, validated_data: dict,
               context: dict, **kwargs) -> models.Tracking:
        request_data = PickupUpdateRequest({
            **PickupUpdateRequest(instance).data,
            **validated_data,
            "address":
            AddressData({
                **AddressData(instance.address).data,
                **validated_data["address"]
            }).data,
        }).data

        Pickups.update(payload=request_data, carrier=instance.pickup_carrier)

        data = validated_data.copy()
        for key, val in data.items():
            if key in models.Pickup.DIRECT_PROPS:
                setattr(instance, key, val)
                validated_data.pop(key)

        save_one_to_one_data(
            "address",
            AddressSerializer,
            instance,
            payload=validated_data,
            context=context,
        )

        instance.save()
        return instance
예제 #2
0
    def update(self, instance: models.Shipment, validated_data: dict,
               context: dict) -> models.Shipment:
        changes = []
        data = validated_data.copy()
        carriers = validated_data.get("carriers") or []

        for key, val in data.items():
            if key in models.Shipment.DIRECT_PROPS:
                setattr(instance, key, val)
                changes.append(key)
                validated_data.pop(key)

            if key in models.Shipment.RELATIONAL_PROPS and val is None:
                prop = getattr(instance, key)
                changes.append(key)
                # Delete related data from database if payload set to null
                if hasattr(prop, "delete"):
                    prop.delete()
                    setattr(instance, key, None)
                    validated_data.pop(key)

        if validated_data.get("customs") is not None:
            changes.append("customs")
            save_one_to_one_data(
                "customs",
                CustomsSerializer,
                instance,
                payload=validated_data,
                context=context,
            )

        if "selected_rate" in validated_data:
            selected_rate = validated_data.get("selected_rate", {})
            carrier = Carrier.objects.filter(
                carrier_id=selected_rate.get("carrier_id")).first()
            instance.test_mode = selected_rate.get("test_mode",
                                                   instance.test_mode)

            instance.selected_rate = {
                **selected_rate,
                **({
                    "carrier_ref": carrier.id
                } if carrier is not None else {}),
            }
            instance.selected_rate_carrier = carrier
            changes += ["selected_rate", "selected_rate_carrier"]

        instance.save(update_fields=changes)

        if "carrier_ids" in validated_data:
            instance.carriers.set(carriers)

        return instance
예제 #3
0
    def update(self, instance, validated_data: dict, **kwargs):
        name = next((k for k in validated_data.keys() if 'settings' in k), '')
        serializer = CARRIER_MODEL_SERIALIZERS.get(name)
        settings = save_one_to_one_data(name,
                                        serializer,
                                        instance,
                                        payload=validated_data)

        return getattr(settings, 'carrier_ptr', None)
예제 #4
0
    def create(self, validated_data: dict, context: dict,
               **kwargs) -> graph.Template:
        data = {
            **validated_data, 'address':
            save_one_to_one_data('address',
                                 AddressModelSerializer,
                                 payload=validated_data,
                                 context=context),
            'customs':
            save_one_to_one_data('customs',
                                 CustomsModelSerializer,
                                 payload=validated_data,
                                 context=context),
            'parcel':
            save_one_to_one_data('parcel',
                                 ParcelModelSerializer,
                                 payload=validated_data,
                                 context=context)
        }

        ensure_unique_default_related_data(validated_data, context=context)

        return super().create(data)
예제 #5
0
    def create(self, validated_data: dict, context: Context,
               **kwargs) -> models.Pickup:
        carrier_filter = validated_data["carrier_filter"]
        carrier = Carriers.first(
            context=context,
            **{
                "raise_not_found": True,
                **DEFAULT_CARRIER_FILTER,
                **carrier_filter
            },
        )
        request_data = PickupRequest({
            **validated_data,
            "parcels":
            sum([list(s.parcels.all()) for s in self._shipments], []),
        }).data

        response = Pickups.schedule(payload=request_data, carrier=carrier)
        payload = {
            key: value
            for key, value in Pickup(response.pickup).data.items()
            if key in models.Pickup.DIRECT_PROPS
        }
        address = save_one_to_one_data("address",
                                       AddressSerializer,
                                       payload=validated_data,
                                       context=context)

        pickup = models.Pickup.objects.create(
            **{
                **payload,
                "address": address,
                "pickup_carrier": carrier,
                "created_by": context.user,
                "test_mode": response.pickup.test_mode,
                "confirmation_number": response.pickup.confirmation_number,
            })
        pickup.shipments.set(self._shipments)

        return pickup
예제 #6
0
    def update(self, instance: graph.Template, validated_data: dict,
               **kwargs) -> graph.Template:
        data = {
            key: value
            for key, value in validated_data.items()
            if key not in ['address', 'customs', 'parcel']
        }

        save_one_to_one_data('address',
                             AddressModelSerializer,
                             instance,
                             payload=validated_data)
        save_one_to_one_data('customs',
                             CustomsModelSerializer,
                             instance,
                             payload=validated_data)
        save_one_to_one_data('parcel',
                             ParcelModelSerializer,
                             instance,
                             payload=validated_data)

        ensure_unique_default_related_data(validated_data, instance)

        return super().update(instance, data)
예제 #7
0
    def create(self, validated_data: dict, context: dict,
               **kwargs) -> models.Shipment:
        carrier_filter = validated_data.get("carrier_filter") or {}
        carrier_ids = validated_data.get("carrier_ids") or []
        carriers = Carriers.list(
            context=context,
            carrier_ids=carrier_ids,
            **{
                "raise_not_found": True,
                **DEFAULT_CARRIER_FILTER,
                **carrier_filter
            },
        )

        # Get live rates
        rate_response: datatypes.RateResponse = (
            SerializerDecorator[RateSerializer](
                data=validated_data,
                context=context).save(carriers=carriers).instance)

        shipment_data = {
            **{
                key: value
                for key, value in validated_data.items() if key in models.Shipment.DIRECT_PROPS and value is not None
            },
            "customs":
            save_one_to_one_data("customs",
                                 CustomsSerializer,
                                 payload=validated_data,
                                 context=context),
            "shipper":
            save_one_to_one_data("shipper",
                                 AddressSerializer,
                                 payload=validated_data,
                                 context=context),
            "recipient":
            save_one_to_one_data("recipient",
                                 AddressSerializer,
                                 payload=validated_data,
                                 context=context),
        }

        shipment = models.Shipment.objects.create(
            **{
                **shipment_data,
                "rates": DP.to_dict(rate_response.rates),
                "messages": DP.to_dict(rate_response.messages),
                "test_mode": all([r.test_mode for r in rate_response.rates]),
            })

        shipment.carriers.set(carriers if any(carrier_ids) else [])

        save_many_to_many_data(
            "parcels",
            ParcelSerializer,
            shipment,
            payload=validated_data,
            context=context,
        )

        return shipment