diff options
| author | Jérome Maes <jem@openerp.com> | 2017-06-19 13:22:30 +0200 |
|---|---|---|
| committer | Jérome Maes <jem@openerp.com> | 2017-06-20 09:00:47 +0200 |
| commit | 800d54e846b34f350bb4713c7c3cfc9e7a895c5b (patch) | |
| tree | 83b726c2407059e296fbcd7188da94128fa99dc3 /addons | |
| parent | c124cb71c2d570a75a7e48d098a31b042d41bde4 (diff) | |
[FIX] phone_validation: bad code comparison
During the formating method, the country code
was compared to the country phone code. Indeed,
phonenumbers python lib return a `PhoneNumber`
object having a `country_code` attribute
refering to the country_phone_code, and not
the country_code (like odoo means).
Consequence: changing the country on a lead
did not make the phone validation work.
Thus, it was required to give both code (country
and country phone codes) to the phonenumbers API,
in order to make a correct verification.
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/phone_validation/models/phone_validation_mixin.py | 13 | ||||
| -rw-r--r-- | addons/phone_validation/tools/phone_validation.py | 15 |
2 files changed, 18 insertions, 10 deletions
diff --git a/addons/phone_validation/models/phone_validation_mixin.py b/addons/phone_validation/models/phone_validation_mixin.py index 8391e89cea3..6f8b3e18902 100644 --- a/addons/phone_validation/models/phone_validation_mixin.py +++ b/addons/phone_validation/models/phone_validation_mixin.py @@ -8,10 +8,10 @@ from odoo.addons.phone_validation.tools import phone_validation class PhoneValidationMixin(models.AbstractModel): _name = 'phone.validation.mixin' - def _phone_get_country_code(self): + def _phone_get_country(self): if 'country_id' in self: - return self.country_id.code - return self.env.user.company_id.country_id.code + return self.country_id + return self.env.user.company_id.country_id def _phone_get_always_international(self): if 'company_id' in self: @@ -19,11 +19,12 @@ class PhoneValidationMixin(models.AbstractModel): return self.env.user.company_id.phone_international_format def phone_format(self, number, country=None, company=None): - country_code = country.code if country else self._phone_get_country_code() + country = country or self._phone_get_country() always_international = company.phone_international_format if company else self._phone_get_always_international() - return phone_validation.phone_format( - number, country_code if country_code else None, + number, + country.code if country else None, + country.phone_code if country else None, always_international=always_international, raise_exception=True ) diff --git a/addons/phone_validation/tools/phone_validation.py b/addons/phone_validation/tools/phone_validation.py index 92b12cea1e0..e59af32f0e6 100644 --- a/addons/phone_validation/tools/phone_validation.py +++ b/addons/phone_validation/tools/phone_validation.py @@ -26,7 +26,15 @@ try: return phone_nbr - def phone_format(number, country_code, always_international=True, raise_exception=True): + def phone_format(number, country_code, country_phone_code, always_international=True, raise_exception=True): + """ Format the given phone number according to the localisation and international options. + :param number: number to convert + :param country_code: the ISO country code in two chars + :type country_code: str + :param country_phone_code: country dial in codes, defined by the ITU-T (Ex: 32 for Belgium) + :type country_phone_code: int + :rtype: str + """ try: phone_nbr = phone_parse(number, country_code) except (phonenumbers.phonenumberutil.NumberParseException, UserError) as e: @@ -35,8 +43,7 @@ try: else: _logger.warning(_('Unable to format %s:\n%s') % number, e) return number - - if always_international and phone_nbr.country_code != country_code: + if always_international and phone_nbr.country_code != country_phone_code: phone_fmt = phonenumbers.PhoneNumberFormat.INTERNATIONAL else: phone_fmt = phonenumbers.PhoneNumberFormat.NATIONAL @@ -48,7 +55,7 @@ except ImportError: def phone_parse(number, country_code): return False - def phone_format(number, country_code, always_international=True, raise_exception=True): + def phone_format(number, country_code, country_phone_code, always_international=True, raise_exception=True): global _phonenumbers_lib_warning if not _phonenumbers_lib_warning: _logger.warning( |
