def post(self, request: Request, pk: str): """ Add one or many options to your shipment.<br/> **eg:**<br/> - add shipment **insurance** - specify the preferred transaction **currency** - setup a **cash collected on delivery** option ```json { "insurance": 120, "currency": "USD" } ``` And many more, check additional options available in the [reference](#operation/all_references). """ shipment = request.user.shipment_set.get(pk=pk) if shipment.status == ShipmentStatus.purchased.value: raise PurplShipApiException("Shipment already 'purchased'", code='state_error', status_code=status.HTTP_409_CONFLICT) payload: dict = DP.to_dict(dict(options=request.data)) SerializerDecorator[ShipmentSerializer](shipment, data=payload).save() reset_related_shipment_rates(shipment) return Response(Shipment(shipment).data)
def patch(self, request: Request, pk: str): """ modify an existing parcel's details. """ parcel = request.user.parcel_set.get(pk=pk) shipment = parcel.shipment_parcels.first() if shipment is not None and shipment.status == ShipmentStatus.purchased.value: raise PurplShipApiException( "The shipment related to this parcel has been 'purchased' and can no longer be modified", status_code=status.HTTP_409_CONFLICT, code='state_error') SerializerDecorator[ParcelSerializer](parcel, data=request.data).save() reset_related_shipment_rates(shipment) return Response(Parcel(parcel).data)
def patch(self, request: Request, pk: str): """ update an address. """ address = request.user.address_set.get(pk=pk) shipment = address.shipper.first() or address.recipient.first() if shipment is not None and shipment.status == ShipmentStatus.purchased.value: raise PurplShipApiException( "The shipment related to this address has been 'purchased' and can no longer be modified", status_code=status.HTTP_409_CONFLICT, code='state_error' ) SerializerDecorator[AddressSerializer](address, data=request.data).save() reset_related_shipment_rates(shipment) return Response(Address(address).data)
def patch(self, request: Request, pk: str): """ modify an existing customs declaration. """ customs = request.user.customs_set.get(pk=pk) shipment = customs.shipment_set.first() if shipment is not None and shipment.status == ShipmentStatus.purchased.value: raise PurplShipApiException( "The shipment related to this customs info has been 'purchased' and can no longer be modified", status_code=status.HTTP_409_CONFLICT, code='state_error') SerializerDecorator[CustomsSerializer]( customs, data=request.data).save(user=request.user) reset_related_shipment_rates(shipment) return Response(Customs(customs).data)
def post(self, request: Request, pk: str): """ Add a parcel to an existing shipment for a multi-parcel shipment. """ shipment = request.user.shipment_set.get(pk=pk) if shipment.status == ShipmentStatus.purchased.value: raise PurplShipApiException("Shipment already 'purchased'", code='state_error', status_code=status.HTTP_409_CONFLICT) parcel = SerializerDecorator[ParcelSerializer](data=request.data).save( user=request.user).instance shipment.shipment_parcels.add(parcel) reset_related_shipment_rates(shipment) return Response(Shipment(shipment).data)
def delete(self, request: Request, pk: str): """ Discard a customs declaration. """ customs = request.user.customs_set.get(pk=pk) shipment = customs.shipment_set.first() if shipment is not None and shipment.status == ShipmentStatus.purchased.value: raise PurplShipApiException( "The shipment related to this customs info has been 'purchased' and cannot be discarded", status_code=status.HTTP_409_CONFLICT, code='state_error') customs.delete(keep_parents=True) shipment.customs = None serializer = Operation( dict(operation="Discard customs info", success=True)) reset_related_shipment_rates(shipment) return Response(serializer.data)
def delete(self, request: Request, pk: str): """ Remove a parcel. """ parcel = request.user.parcel_set.get(pk=pk) shipment = parcel.shipment_parcels.first() if shipment is not None and ( shipment.status == ShipmentStatus.purchased.value or len(shipment.shipment_parcels.all()) == 1): raise PurplShipApiException( "A shipment attached to this parcel is purchased or has only one parcel. The parcel cannot be removed!", status_code=status.HTTP_409_CONFLICT, code='state_error') parcel.delete(keep_parents=True) shipment.shipment_parcels.set( shipment.shipment_parcels.exclude(id=parcel.id)) serializer = Operation(dict(operation="Remove parcel", success=True)) reset_related_shipment_rates(shipment) return Response(serializer.data)
def post(self, request: Request, pk: str): """ Add the customs declaration for the shipment if non existent. """ shipment = request.user.shipment_set.get(pk=pk) if shipment.status == ShipmentStatus.purchased.value: raise PurplShipApiException("Shipment already 'purchased'", code='state_error', status_code=status.HTTP_409_CONFLICT) if shipment.customs is not None: raise PurplShipApiException( "Shipment customs declaration already defined", code='state_error', status_code=status.HTTP_409_CONFLICT) SerializerDecorator[ShipmentSerializer]( shipment, data=dict(customs=request.data)).save() reset_related_shipment_rates(shipment) return Response(Shipment(shipment).data)