Skip to content

Commit

Permalink
Merge pull request #88 from PurplShip/purplship-2020.12-patch
Browse files Browse the repository at this point in the history
[patch] Purplship 2020.12 patch
  • Loading branch information
danh91 authored Dec 31, 2020
2 parents 8922c6d + 498b26a commit 225a2dc
Show file tree
Hide file tree
Showing 51 changed files with 332 additions and 169 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<a href="https://github.com/Purplship/Purplship/actions"><img src="https://github.com/Purplship/Purplship/workflows/PuprlShip/badge.svg" alt="CI" style="max-width:100%;"></a>
<a href="https://www.gnu.org/licenses/lgpl-3.0" rel="nofollow"><img src="https://img.shields.io/badge/License-LGPL%20v3-blue.svg" alt="License: AGPL v3" data-canonical-src="https://img.shields.io/badge/License-AGPL%20v3-blue.svg" style="max-width:100%;"></a>
<a href="https://github.com/python/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg" alt="Code style: black" style="max-width:100%;"></a>
<a href="https://codecov.io/gh/Purplship/Purplship"><img src="https://codecov.io/gh/Purplship/Purplship/branch/master/graph/badge.svg" alt="codecov" style="max-width:100%;"></a>
<a href="https://codecov.io/gh/Purplship/Purplship"><img src="https://codecov.io/gh/Purplship/Purplship/branch/main/graph/badge.svg" alt="codecov" style="max-width:100%;"></a>
<a href="https://app.codacy.com/manual/DanH91/Purplship?utm_source=github.com&utm_medium=referral&utm_content=Purplship/Purplship&utm_campaign=Badge_Grade_Dashboard"><img src="https://api.codacy.com/project/badge/Grade/a57baa23a1ca4403a37a8b7134609709" alt="Codacy Badge" style="max-width:100%;"></a>
<a href="https://gitter.im/Purplship/Purplship?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge"><img src="https://badges.gitter.im/Purplship/purplship.svg" alt="Join the chat at https://gitter.im/Purplship/Purplship" style="max-width:100%;"></a>
</p>
Expand All @@ -36,7 +36,7 @@ With Purplship you can:
Purplship has two layers

- **Purplship SDK** for an integration as a Python library. *[documentation](#Documentation) bellow*
- **Purplship Server** for an On-prem or private cloud REST API. *documentation and usage can be found at [docs.purplship.com](https://docs.purplship.com)*
- **Purplship Server** for an On-prem or cloud REST API. *documentation and usage can be found at [docs.purplship.com](https://docs.purplship.com)*

## License

Expand Down Expand Up @@ -446,7 +446,7 @@ rates = request.from_(carrier).parse()

| Name | Type | Description
| --- | --- | --- |
| `carrier_name` | `str` | default: carrier name (eg: CanadaPost, Purolator...)
| `carrier_name` | `str` | default: carrier name (eg: canadapost, purolator...)
| `id` | `str` |
| `test` | `boolean` |

Expand Down Expand Up @@ -566,7 +566,7 @@ rates = request.from_(carrier).parse()
| --- | --- | --- |
| `carrier_name` | `str` |
| `carrier_id` | `str` |
| `message` | `str` |
| `message` | Union[str, Any] |
| `code` | `str` |
| `details` | `dict` |

Expand Down Expand Up @@ -710,6 +710,7 @@ rates = request.from_(carrier).parse()
| `doc_images` | List[[Doc](#Doc)] |
| `options` | `dict` |
| `reference` | `str` |
| `label_type` | `str` |


- <a name="TrackingDetails"></a> TrackingDetails
Expand Down Expand Up @@ -738,7 +739,7 @@ rates = request.from_(carrier).parse()
| --- | --- | --- |
| `tracking_numbers` | List[str] |
| `language_code` | `str` |
| `level_of_details` | `str` |
| `level_of_details` | `str` |

</details>

Expand Down
4 changes: 2 additions & 2 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click
import inspect
from jinja2 import Template
from purplship.core.utils import to_dict
from purplship.core.utils import DP

MODELS_TEMPLATE = Template('''
{% for name, cls in classes.items() %}
Expand Down Expand Up @@ -135,7 +135,7 @@ def generate_shipment_options():
@cli.command()
def generate_package_presets():
preset_mappers = {
key: {c.name: to_dict(c.value) for c in list(getattr(mapper['package'], mapper['packagePresets']))}
key: {c.name: DP.to_dict(c.value) for c in list(getattr(mapper['package'], mapper['packagePresets']))}
for key, mapper in PACKAGE_MAPPERS.items()
if mapper.get('packagePresets') is not None
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/boxknight/setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_namespace_packages

setup(name='purplship.boxknight',
version='2020.12-alpha',
version='2020.12.1-alpha',
description='Multi-carrier shipping API integration with python',
url='https://github.com/PurplShip/purplship',
author='Purplship Team',
Expand Down
4 changes: 2 additions & 2 deletions extensions/canadapost/purplship/mappers/canadapost/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ def _non_contract_shipment(job: Job):
def _get_label(job: Job):
label_string = http(
decoder=lambda b: base64.encodebytes(b).decode("utf-8"),
url=job.data,
url=job.data["href"],
headers={
"Accept": "application/pdf",
"Accept": job.data["media"],
"Authorization": f"Basic {self.settings.authorization}",
},
method="GET",
Expand Down
13 changes: 6 additions & 7 deletions extensions/canadapost/purplship/providers/canadapost/rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
service_standardType,
)
from functools import reduce
from datetime import datetime
from typing import List, Tuple, cast
from purplship.core.utils import Serializable, Element, NF, XP
from purplship.providers.canadapost.utils import Settings
from purplship.core.units import Country, Currency, Packages, Services, Options
from purplship.core.errors import OriginNotServicedError
from purplship.core.models import RateDetails, ChargeDetails, Message, RateRequest
from purplship.providers.canadapost.error import parse_error_response
from purplship.providers.canadapost.units import OptionCode, ServiceType, PackagePresets
from purplship.providers.canadapost.units import OptionCode, ServiceType, PackagePresets, MeasurementOptions


def parse_rate_response(
Expand Down Expand Up @@ -97,7 +96,7 @@ def rate_request(
contract_id=None,
promo_code=None,
quote_type=None,
expected_mailing_date=datetime.today().strftime("%Y-%m-%d"),
expected_mailing_date=requested_options.shipment_date,
options=(
optionsType(
option=[
Expand All @@ -108,11 +107,11 @@ def rate_request(
if any(requested_options) else None
),
parcel_characteristics=parcel_characteristicsType(
weight=package.weight.KG,
weight=package.weight.map(MeasurementOptions).KG,
dimensions=dimensionsType(
length=package.length.CM,
width=package.width.CM,
height=package.height.CM,
length=package.length.map(MeasurementOptions).CM,
width=package.width.map(MeasurementOptions).CM,
height=package.height.map(MeasurementOptions).CM,
),
unpackaged=None,
mailing_tube=None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
groupIdOrTransmitShipment,
)
from purplship.core.units import Currency, WeightUnit, Options, Packages
from purplship.core.utils import Serializable, Element, XP, SF
from purplship.core.utils import Serializable, Element, XP, SF, NF
from purplship.core.models import (
Message,
ShipmentDetails,
Expand All @@ -33,9 +33,10 @@
OptionCode,
ServiceType,
PackagePresets,
PrinterType,
PaymentType,
INTERNATIONAL_NON_DELIVERY_OPTION
LabelType,
INTERNATIONAL_NON_DELIVERY_OPTION,
MeasurementOptions,
)
from purplship.providers.canadapost.utils import Settings

Expand Down Expand Up @@ -101,6 +102,8 @@ def compute_amount(code: str, _: Any):
if is_intl and not any(key in special_services for key in INTERNATIONAL_NON_DELIVERY_OPTION):
special_services['canadapost_return_to_sender'] = OptionCode.canadapost_return_to_sender.value

label_encoding, label_format = LabelType[payload.label_type or 'PDF_4x6'].value

request = ShipmentType(
customer_request_id=None,
groupIdOrTransmitShipment=groupIdOrTransmitShipment(),
Expand Down Expand Up @@ -145,11 +148,11 @@ def compute_amount(code: str, _: Any):
),
),
parcel_characteristics=ParcelCharacteristicsType(
weight=package.weight.KG,
weight=package.weight.map(MeasurementOptions).KG,
dimensions=dimensionsType(
length=package.length.CM,
width=package.width.CM,
height=package.height.CM,
length=package.length.map(MeasurementOptions).CM,
width=package.width.map(MeasurementOptions).CM,
height=package.height.map(MeasurementOptions).CM,
),
unpackaged=None,
mailing_tube=None,
Expand Down Expand Up @@ -178,8 +181,8 @@ def compute_amount(code: str, _: Any):
if options.notification_email else None
),
print_preferences=PrintPreferencesType(
output_format=PrinterType[options.label_printing or "regular"].value,
encoding=None,
output_format=label_format,
encoding=label_encoding,
),
preferences=PreferencesType(
service_code=None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def _create_shipment(payload: ShipmentRequest, settings: Settings) -> Job:

def _get_shipment_label(shipement_response: str) -> Job:
links = XP.to_xml(shipement_response).xpath(".//*[local-name() = $name]", name="link")
label_url = next(
(link.get("href") for link in links if link.get("rel") == "label"),
None,
href, media = next(
((link.get("href"), link.get("media-type")) for link in links if link.get("rel") == "label"),
(None, None),
)
return Job(id="shipment_label", data=label_url, fallback="")
return Job(id="shipment_label", data=dict(href=href, media=media), fallback="")
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
PreferencesType,
)
from purplship.core.units import Currency, WeightUnit, Options, Packages
from purplship.core.utils import Serializable, Element, SF, XP
from purplship.core.utils import Serializable, Element, SF, XP, NF
from purplship.core.models import (
Message,
ShipmentDetails,
Expand Down Expand Up @@ -138,11 +138,11 @@ def compute_amount(code: str, _: Any):
if len(special_services) > 0 else None
),
parcel_characteristics=ParcelCharacteristicsType(
weight=package.weight.KG,
weight=NF.decimal(package.weight.KG, .1),
dimensions=dimensionsType(
length=package.length.CM,
width=package.width.CM,
height=package.height.CM,
length=NF.decimal(package.length.CM, .1),
width=NF.decimal(package.width.CM, .1),
height=NF.decimal(package.height.CM, .1),
),
unpackaged=None,
mailing_tube=None,
Expand All @@ -157,7 +157,7 @@ def compute_amount(code: str, _: Any):
if options.notification_email is not None else None
),
preferences=PreferencesType(
show_packing_instructions=True,
show_packing_instructions=False,
show_postage_rate=True,
show_insured_value=("insurance" in payload.options),
),
Expand Down
16 changes: 10 additions & 6 deletions extensions/canadapost/purplship/providers/canadapost/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ class PackagePresets(Flag):
)


class PrinterType(Enum):
regular = "8.5x11"
thermal = "4x6"
class MeasurementOptions(Enum):
quant = 0.1


class LablelEncoding(Enum):
pdf = "PDF"
zpl = "ZPL"
class LabelType(Enum):
PDF_4x6 = ("PDF", "4x6")
PDF_8_5x11 = ("PDF", "8.5x11")
ZPL_4x6 = ("ZPL", "4x6")

""" Unified Label type mapping """
PDF = PDF_4x6
ZPL = ZPL_4x6


class PaymentType(Flag):
Expand Down
2 changes: 1 addition & 1 deletion extensions/canadapost/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="purplship.canadapost",
version="2020.12",
version="2020.12.1",
description="Multi-carrier shipping API integration with python",
url="https://github.com/PurplShip/purplship",
author="Purplship Team",
Expand Down
2 changes: 1 addition & 1 deletion extensions/canpar/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="purplship.canpar",
version="2020.12",
version="2020.12.1",
description="Multi-carrier shipping API integration with python",
url="https://github.com/Purplship/purplship",
author="Purplship Team",
Expand Down
12 changes: 10 additions & 2 deletions extensions/dhl_express/purplship/providers/dhl_express/rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
)
from pydhl.dct_requestdatatypes_global import DCTDutiable
from pydhl.dct_response_global_2_0 import QtdShpType as ResponseQtdShpType

from purplship.core.errors import DestinationNotServicedError
from purplship.core.utils import Serializable, Element, NF, XP, DF
from purplship.core.units import Packages, Options, Package, WeightUnit, DimensionUnit, Services
from purplship.core.units import Packages, Options, Package, WeightUnit, DimensionUnit, Services, CountryCurrency
from purplship.core.models import RateDetails, Message, ChargeDetails, RateRequest
from purplship.providers.dhl_express.units import (
Product,
Expand Down Expand Up @@ -107,6 +109,9 @@ def rate_request(payload: RateRequest, settings: Settings) -> Serializable[DCTRe
is_document = all([parcel.is_document for parcel in payload.parcels])
is_dutiable = not is_document

if not is_international and payload.shipper.country_code in ["CA"]:
raise DestinationNotServicedError(payload.shipper.country_code)

if is_international and is_dutiable:
special_services.append(SpecialServiceCode.dhl_paperless_trade)

Expand Down Expand Up @@ -185,7 +190,10 @@ def rate_request(payload: RateRequest, settings: Settings) -> Serializable[DCTRe
],
),
Dutiable=(
DCTDutiable(DeclaredValue=1.0, DeclaredCurrency=options.currency)
DCTDutiable(
DeclaredValue=options.insurance or 1.0,
DeclaredCurrency=options.currency or CountryCurrency[payload.shipper.country_code].value
)
if is_international and is_dutiable else None
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SpecialService,
WeightUnit,
DimensionUnit,
Label,
)
from pydhl.ship_val_global_res_6_2 import ShipmentResponse, LabelImage
from purplship.core.utils import Serializable, SF, XP
Expand All @@ -38,6 +39,7 @@
SpecialServiceCode,
DeliveryType,
PackagePresets,
LabelType,
)
from purplship.providers.dhl_express.utils import Settings
from purplship.providers.dhl_express.error import parse_error_response
Expand Down Expand Up @@ -95,6 +97,7 @@ def shipment_request(
special_services.append(SpecialServiceCode.dhl_paperless_trade.value)
has_payment_config = payload.payment is not None
has_customs_config = payload.customs is not None
label_format, label_template = LabelType[payload.label_type or 'PDF_6x4'].value

request = DHLShipmentRequest(
schemaVersion=6.2,
Expand Down Expand Up @@ -225,7 +228,6 @@ def shipment_request(
Notification(EmailAddress=options.notification_email or payload.recipient.email)
if options.notification_email is None else None
),
LabelImageFormat="PDF",
DocImages=(
DocImages(
DocImage=[
Expand All @@ -241,7 +243,8 @@ def shipment_request(
),
RequestArchiveDoc=None,
NumberOfArchiveDoc=None,
Label=None,
LabelImageFormat=label_format,
Label=Label(LabelTemplate=label_template),
ODDLinkReq=None,
DGs=None,
)
Expand Down
16 changes: 16 additions & 0 deletions extensions/dhl_express/purplship/providers/dhl_express/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ class PackagePresets(Flag):
)


class LabelType(Flag):
PDF_6x4 = ('PDF', '6X4_PDF')
PDF_8x4 = ('PDF', '8X4_PDF')
PDF_8x4_A4 = ('PDF', '8X4_A4_PDF')
PDF_6x4_A4 = ('PDF', '6X4_A4_PDF')
PDF_8x4_CI = ('PDF', '8X4_CI_PDF')
PDF_8x4_RU_A4 = ('PDF', '8X4_RU_A4_PDF')
ZPL_8x4 = ('ZPL2', '8X4_thermal')
ZPL_6x4 = ('ZPL2', '6X4_thermal')
ZPL_8x4_CI = ('ZPL2', '8X4_CI_thermal')

""" Unified Label type mapping """
PDF = PDF_6x4
ZPL = ZPL_6x4


class Incoterm(Enum):
EXW = "Ex Works"
FCA = "Free Carrier"
Expand Down
2 changes: 1 addition & 1 deletion extensions/dhl_express/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="purplship.dhl_express",
version="2020.12",
version="2020.12.1",
description="Multi-carrier shipping API integration with python",
url="https://github.com/PurplShip/purplship",
author="Purplship Team",
Expand Down
2 changes: 1 addition & 1 deletion extensions/dicom/setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_namespace_packages

setup(name='purplship.dicom',
version='2020.12-alpha',
version='2020.12.1-alpha',
description='Multi-carrier shipping API integration with python',
url='https://github.com/PurplShip/purplship',
author='Purplship',
Expand Down
Loading

0 comments on commit 225a2dc

Please sign in to comment.