Skip to content

Commit

Permalink
migrate DPD to new roulier and python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
DylannCordel committed Jan 20, 2021
1 parent ac7b5dc commit 6ef275b
Show file tree
Hide file tree
Showing 21 changed files with 438 additions and 267 deletions.
70 changes: 36 additions & 34 deletions roulier/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def _normalize_coerce_zpl(self, value):
Remove ZPL ctrl caraters
Remove accents
"""
if not isinstance(value, basestring):
if not isinstance(value, str):
return value

ctrl_cars = [
Expand All @@ -28,41 +28,43 @@ def _normalize_coerce_zpl(self, value):

def _normalize_coerce_accents(self, value):
"""Sanitize accents for some WS."""
if not isinstance(value, basestring):
if not isinstance(value, str):
return value
sanitized = (
value
# quick and dirty replacement
# of common accentued chars in french
# because some ws don't handle well utf8
.replace(u"é", "e")
.replace(u"è", "e")
.replace(u"ë", "e")
.replace(u"ê", "e")
.replace(u"ô", "o")
.replace(u"ï", "i")
.replace(u"ö", "o")
.replace(u"à", "a")
.replace(u"â", "a")
.replace(u"ç", "c")
.replace(u"û", "u")
.replace(u"ù", "u")
.replace(u"É", "E")
.replace(u"È", "E")
.replace(u"Ë", "E")
.replace(u"Ê", "E")
.replace(u"Ô", "O")
.replace(u"Ï", "I")
.replace(u"Ö", "O")
.replace(u"À", "A")
.replace(u"Â", "A")
.replace(u"Ç", "C")
.replace(u"Û", "U")
.replace(u"Ù", "U")
.replace(u"œ", "oe")
.replace(u"Œ", "OE")
).encode(
"ascii", "ignore"
(
value
# quick and dirty replacement
# of common accentued chars in french
# because some ws don't handle well utf8
.replace("é", "e")
.replace("è", "e")
.replace("ë", "e")
.replace("ê", "e")
.replace("ô", "o")
.replace("ï", "i")
.replace("ö", "o")
.replace("à", "a")
.replace("â", "a")
.replace("ç", "c")
.replace("û", "u")
.replace("ù", "u")
.replace("É", "E")
.replace("È", "E")
.replace("Ë", "E")
.replace("Ê", "E")
.replace("Ô", "O")
.replace("Ï", "I")
.replace("Ö", "O")
.replace("À", "A")
.replace("Â", "A")
.replace("Ç", "C")
.replace("Û", "U")
.replace("Ù", "U")
.replace("œ", "oe")
.replace("Œ", "OE")
)
.encode("ascii", "ignore")
.decode("ascii")
) # cut remaining chars
return sanitized

Expand Down
2 changes: 1 addition & 1 deletion roulier/carriers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from . import laposte_fr
from . import gls_fr
from . import chronopost_fr
from . import dpd_fr

# from . import geodis
# from . import dpd
6 changes: 0 additions & 6 deletions roulier/carriers/dpd/__init__.py

This file was deleted.

30 changes: 0 additions & 30 deletions roulier/carriers/dpd/dpd.py

This file was deleted.

65 changes: 0 additions & 65 deletions roulier/carriers/dpd/dpd_decoder.py

This file was deleted.

98 changes: 0 additions & 98 deletions roulier/carriers/dpd/dpd_encoder.py

This file was deleted.

5 changes: 5 additions & 0 deletions roulier/carriers/dpd_fr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import carrier_action
from . import decoder
from . import encoder
from . import transport
from . import api
46 changes: 43 additions & 3 deletions roulier/carriers/dpd/dpd_api.py → roulier/carriers/dpd_fr/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
"""Implementation of Dpd Api."""
from roulier.api import Api
from roulier.api import ApiParcel
from roulier.api import MyValidator

DPD_LABEL_FORMAT = (
"PDF",
Expand All @@ -22,7 +23,45 @@
)


class DpdApi(Api):
class DpdValidator(MyValidator):
def _validate_product(self, _, field, value):
"""
Tests some complex constraints relate to the product type and other fields values
The rule's arguments are validated against this schema:
{'type': 'boolean'}
"""
product = self.document.get("product")
pickup_id = self.document.get("pickupLocationId", "").strip()
notifications = self.document.get("notifications")
if pickup_id and product in ("DPD_Predict", "DPD_Classic"):
self._error(
"pickupLocationId", "pickupLocationId can't be used with %s" % product
)
if product == "DPD_Predict":
if notifications and "notifications" != "Predict":
self._error(
"notifications", "must be set to Predict when using Predict"
)
else:
if notifications == "Predict":
self._error(
"notifications",
"Predict notifications can't be used with %s" % product,
)
if product == "DPD_Relais" and not pickup_id:
self._error(
"pickupLocationId", "pickupLocationId is mandatory for Relais"
)


class DpdApi(ApiParcel):
def _validator(self):
v = DpdValidator()
v.allow_unknown = True
# v.purge_unknown = True
return v

def _service(self):
schema = super(DpdApi, self)._service()
schema["labelFormat"]["allowed"] = DPD_LABEL_FORMAT
Expand Down Expand Up @@ -61,10 +100,11 @@ def _service(self):
"default": DPD_PRODUCTS[0],
# 'description': 'Type de produit',
"allowed": DPD_PRODUCTS,
"product": True,
}
)

schema["dropOffLocation"] = {
schema["pickupLocationId"] = {
"default": "",
"empty": True,
"required": False,
Expand Down
Loading

0 comments on commit 6ef275b

Please sign in to comment.