Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FW][FIX] phone_validation: brazilian phone numbers #159624

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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