[FIX] phone_validation: fallback if no region provided

Before this commit if you would call phone_parse() but not pass along a
country code it would crash. This is reproducable by calling the phone_parse
function and not setting a country on the contact. In this case you would get
the following traceback:

  AttributeError: 'bool' object has no attribute 'upper'.

This happens because the phonenumbers library tries to upper() the region but
if Odoo doesn't have a country set it passes along False. By doing a fallback
to 'None' the phonenumbers library will natively handle this and the user in
the UI does not get a traceback.

Task-2852953

closes odoo/odoo#112699

X-original-commit: 9b75d7ac1a912ff3d15c393a6a23018a18b3b9bb
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Co-authored-by: Thibault Delavallee <tde@odoo.com>
This commit is contained in:
root
2023-02-14 18:11:52 +01:00
committed by Thibault Delavallée
co-authored by Thibault Delavallee
parent f8464c01bd
commit bd5f307c66
3 changed files with 25 additions and 1 deletions
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import test_phonenumbers
from . import test_phonenumbers_patch
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.phone_validation.tools import phone_validation
from odoo.exceptions import UserError
from odoo.tests import tagged
from odoo.tests.common import BaseCase
@tagged('phone_validation')
class TestPhonenumbers(BaseCase):
def test_country_code_falsy(self):
self.assertEqual(
phone_validation.phone_format('0456998877', 'BE', '32', force_format='E164'),
'+32456998877'
)
# no country code -> UserError, no internal traceback
with self.assertRaises(UserError):
self.assertEqual(
phone_validation.phone_format('0456998877', None, '32', force_format='E164'),
'+32456998877'
)
@@ -15,7 +15,7 @@ try:
def phone_parse(number, country_code):
try:
phone_nbr = phonenumbers.parse(number, region=country_code, keep_raw_input=True)
phone_nbr = phonenumbers.parse(number, region=country_code or None, keep_raw_input=True)
except phonenumbers.phonenumberutil.NumberParseException as e:
raise UserError(_('Unable to parse %(phone)s: %(error)s', phone=number, error=str(e)))