From 24546d77d8588c185283f432ab4dd0cd4a1ea3a1 Mon Sep 17 00:00:00 2001 From: Damien Crier Date: Fri, 2 Jun 2017 13:40:21 +0200 Subject: [PATCH] fix unicode/ascii error --- partner_firstname/models/res_partner.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/partner_firstname/models/res_partner.py b/partner_firstname/models/res_partner.py index d576e6729..e27997841 100644 --- a/partner_firstname/models/res_partner.py +++ b/partner_firstname/models/res_partner.py @@ -3,12 +3,10 @@ # © 2014 Agile Business Group () # © 2015 Grupo ESOC () # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - import logging from odoo import api, fields, models from .. import exceptions - _logger = logging.getLogger(__name__) @@ -133,11 +131,20 @@ class ResPartner(models.Model): Removes leading, trailing and duplicated whitespace. """ - if name: - name = u" ".join(name.split(None)) - if comma: - name = name.replace(" ,", ",") - name = name.replace(", ", ",") + try: + name = u" ".join(name.split()) if name else name + except UnicodeDecodeError: + # with users coming from LDAP, name can be a str encoded as utf-8 + # this happens with ActiveDirectory for instance, and in that case + # we get a UnicodeDecodeError during the automatic ASCII -> Unicode + # conversion that Python does for us. + # In that case we need to manually decode the string to get a + # proper unicode string. + name = u' '.join(name.decode('utf-8').split()) if name else name + + if comma: + name = name.replace(" ,", ",") + name = name.replace(", ", ",") return name @api.model