Skip to content

Commit

Permalink
[FIX] phone_validation: brazilian phone numbers
Browse files Browse the repository at this point in the history
Current behavior:
Brazilian phone numbers are not managed correctly
following the 2016 changes in Brazil.
(Adding a 9 to mobile phone numbers)

Fix:
Patched the phonenumbers library, adding a 9
at the right place for mobile phone numbers.

opw-3694150

closes #153282

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
  • Loading branch information
ande-odoo committed Mar 28, 2024
1 parent 9094afe commit a4f8844
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions addons/phone_validation/lib/phonenumbers_patch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,17 @@ def _local_load_region(code):
# loading updated region_CI.py from current directory
# https://github.com/daviddrysdale/python-phonenumbers/blob/v8.12.32/python/phonenumbers/data/region_CI.py
phonenumbers.phonemetadata.PhoneMetadata.register_region_loader('CI', _local_load_region)
# MONKEY PATCHING phonemetadata to fix Brazilian phonenumbers following 2016 changes
def _hook_load_region(code):
phonenumbers.data._load_region(code)
if code == 'BR':
phonenumbers.data.region_BR.PHONE_METADATA_BR.intl_number_format.append(
phonenumbers.phonemetadata.NumberFormat(
pattern='(\\d{2})(\\d{4})(\\d{4})',
format='\\1 9\\2-\\3',
leading_digits_pattern=['(?:[14689][1-9]|2[12478]|3[1-578]|5[13-5]|7[13-579][689])'],
)
)
phonenumbers.phonemetadata.PhoneMetadata.register_region_loader('BR', _hook_load_region)
except ImportError:
pass
16 changes: 16 additions & 0 deletions addons/phone_validation/tests/test_phonenumbers_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
from odoo.addons.phone_validation.lib import phonenumbers_patch

class TestPhonenumbersPatch(BaseCase):
def test_region_BR_monkey_patch(self):
""" Test Brazil phone numbers patch for added 9 in mobile numbers
It should not be added for fixed lines numbers"""
if not phonenumbers:
self.skipTest('Cannot test without phonenumbers module installed.')

# Mobile number => 9 should be added
parsed = phonenumbers.parse('11 6123 4567', region="BR")
formatted = phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
self.assertEqual(formatted, '+55 11 96123-4567')

# Fixed line => 9 should not be added
parsed = phonenumbers.parse('11 2345 6789', region="BR")
formatted = phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
self.assertEqual(formatted, '+55 11 2345-6789')

def test_region_CI_monkey_patch(self):
"""Test if the patch is apply on the good version of the lib
And test some phonenumbers"""
Expand Down

0 comments on commit a4f8844

Please sign in to comment.