Files
odoo_source/addons/phone_validation/tools/phone_validation.py
T
Thibault Delavallée 5f37bad75d [FIX] crm: remove unnecessary warning in phone format
In this commit we remove the warning logger when not being able to format
a phone number. Indeed this does not indicate there was a real issue.
It simply indicates that either the phone number is not parsable or not
matching given country format.

It happens in a lot of situation that phone numbers are not valid: bad
encoding, country mismatch due to {lead, task, ticket} / partner not always
being the same, ... This should not raise a server warning.

When using frontend SMS composer for phone number, a warning is displayed
to the user telling the number is probably incorrect. This is considered
as sufficient from UX point of view as it warns users. We do not think
we should warn admins that some data is incoherent in database.

This commit also

  * Revert "[FIX] crm: avoid linking demo leads with an unrelated partner"
    This reverts commit 2c94c4c81ffa171158586cd64828d27c537cd1d6
    -> having "invalid" data should be a valid use case of real life

  * Revert "[FIX] crm: get correct country for phone validation"
    This reverts commit a58f9fc154c038d72031b281e2bec2c0fc2915ba
    -> country of a lead comes from its partner if set but can be different.
    Same for phone numbers. Values on a lead after a compute are considered
    as complete. Sales persons could effectively have incoherent data on their
    form view but that is the result of what has been encoded.

Task ID-2325228

X-original-commit: cecc4ca6f02e599e193c3fa688bfcbd02ac0580b
2020-09-28 10:12:23 +00:00

117 lines
4.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _
from odoo.exceptions import UserError
import logging
_logger = logging.getLogger(__name__)
_phonenumbers_lib_warning = False
try:
import phonenumbers
def phone_parse(number, country_code):
try:
phone_nbr = phonenumbers.parse(number, region=country_code, keep_raw_input=True)
except phonenumbers.phonenumberutil.NumberParseException as e:
raise UserError(_('Unable to parse %(phone)s: %(error)s', phone=number, error=str(e)))
if not phonenumbers.is_possible_number(phone_nbr):
raise UserError(_('Impossible number %s: probably invalid number of digits.', number))
if not phonenumbers.is_valid_number(phone_nbr):
raise UserError(_('Invalid number %s: probably incorrect prefix.', number))
return phone_nbr
def phone_format(number, country_code, country_phone_code, force_format='INTERNATIONAL', 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
:param force_format: stringified version of format globals (see
https://github.com/daviddrysdale/python-phonenumbers/blob/dev/python/phonenumbers/phonenumberutil.py)
'E164' = 0
'INTERNATIONAL' = 1
'NATIONAL' = 2
'RFC3966' = 3
:type force_format: str
:rtype: str
"""
try:
phone_nbr = phone_parse(number, country_code)
except (phonenumbers.phonenumberutil.NumberParseException, UserError) as e:
if raise_exception:
raise
else:
return number
if force_format == 'E164':
phone_fmt = phonenumbers.PhoneNumberFormat.E164
elif force_format == 'RFC3966':
phone_fmt = phonenumbers.PhoneNumberFormat.RFC3966
elif force_format == 'INTERNATIONAL' or phone_nbr.country_code != country_phone_code:
phone_fmt = phonenumbers.PhoneNumberFormat.INTERNATIONAL
else:
phone_fmt = phonenumbers.PhoneNumberFormat.NATIONAL
return phonenumbers.format_number(phone_nbr, phone_fmt)
except ImportError:
def phone_parse(number, country_code):
return False
def phone_format(number, country_code, country_phone_code, force_format='INTERNATIONAL', raise_exception=True):
global _phonenumbers_lib_warning
if not _phonenumbers_lib_warning:
_logger.info(
"The `phonenumbers` Python module is not installed, contact numbers will not be "
"verified. Please install the `phonenumbers` Python module."
)
_phonenumbers_lib_warning = True
return number
def phone_sanitize_numbers(numbers, country_code, country_phone_code, force_format='E164'):
""" Given a list of numbers, return parsezd and sanitized information
:return dict: {number: {
'sanitized': sanitized and formated number or False (if cannot format)
'code': 'empty' (number was a void string), 'invalid' (error) or False (sanitize ok)
'msg': error message when 'invalid'
}}
"""
if not isinstance(numbers, (list)):
raise NotImplementedError()
result = dict.fromkeys(numbers, False)
for number in numbers:
if not number:
result[number] = {'sanitized': False, 'code': 'empty', 'msg': False}
continue
try:
stripped = number.strip()
sanitized = phone_format(
stripped, country_code, country_phone_code,
force_format=force_format, raise_exception=True)
except Exception as e:
result[number] = {'sanitized': False, 'code': 'invalid', 'msg': str(e)}
else:
result[number] = {'sanitized': sanitized, 'code': False, 'msg': False}
return result
def phone_sanitize_numbers_w_record(numbers, record, country=False, record_country_fname='country_id', force_format='E164'):
if not isinstance(numbers, (list)):
raise NotImplementedError()
if not country:
if record and record_country_fname and hasattr(record, record_country_fname) and record[record_country_fname]:
country = record[record_country_fname]
elif record:
country = record.env.company.country_id
country_code = country.code if country else None
country_phone_code = country.phone_code if country else None
return phone_sanitize_numbers(numbers, country_code, country_phone_code, force_format=force_format)