Skip to content

Commit

Permalink
Merge pull request #99 from akretion/feat/dpd_coerce
Browse files Browse the repository at this point in the history
Add coerce on dpd
  • Loading branch information
hparfr authored Nov 26, 2018
2 parents b840d2f + 38f677a commit 90a96e5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Roadmap / TODO:
- Support additionnal methods of api
- Write tests

# 0.3.4 2018-11-26
- DPD convert characters to ASCII (because issues on labels)

# 0.3.3 2018-02-19
- Geodis force ASCII conversion for EDI.

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.3
0.3.4
36 changes: 36 additions & 0 deletions roulier/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ def _normalize_coerce_zpl(self, value):
val = val.replace("%c" % ctrl, "")
return val

def _normalize_coerce_accents(self, value):
"""Sanitize accents for some WS."""
if not isinstance(value, basestring):
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")
).encode('ascii', 'ignore') # cut remaining chars
return sanitized


class Api(object):
"""Define expected fields of carriers.
Expand Down
5 changes: 4 additions & 1 deletion roulier/carriers/dpd/dpd_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ def _address(self):

def _to_address(self):
schema = super(DpdApi, self)._to_address()
schema['firstName'] = {'default': '', 'description': """First name"""}
schema['firstName'] = {'default': '', 'description': """First name""",
'coerce': 'accents'}
schema['door1'] = {'default': '', 'description': """Door code 1"""}
schema['door2'] = {'default': '', 'description': """Door code 2"""}
schema['intercom'] = {'default': '', 'description': """Intercom"""}
for field in ['city', 'company', 'name', 'street1', 'street2']:
schema[field].update({ 'coerce': 'accents'})
return schema

def _from_address(self):
Expand Down
6 changes: 5 additions & 1 deletion roulier/carriers/geodis/geodis_api_edi.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ def _shipments(self):
}

def _schemas(self):
return {
schemas = {
'service': self._service(),
'shipments': self._shipments(),
'agency_address': self._from_address(),
'from_address': self._from_address(),
}
for schema in schemas:
for field in schemas[schema]:
schemas[schema][field].update({'coerce': 'accents'})
return schemas
28 changes: 0 additions & 28 deletions roulier/carriers/geodis/geodis_transport_edi.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,6 @@ def sanitize(token):
.replace("'", " ")
.replace("+", " ")
.replace(":", " ")
# quick and dirty replacement
# of common accentued chars in french
# because geodis don't handle well utf8
# TODO: put it in api coerce
.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")
).encode('ascii', 'ignore') # cut remaining chars
return sanitized

Expand Down

0 comments on commit 90a96e5

Please sign in to comment.