[FIX] phone_validation, crm_phone_validation: Phone number parsing

Before this commit, when filing a phone number field on a model form (typically on website_form),
the module phonenumbers would try to parse the number against the country of the model.

However, this call fails when the number starts with 00 instead of + and that the model
doesn't have a country_id field or if said field is not set.

For this specific case, we chose to skip the number parsing altogether as providing a default
country to the parser can lead to a wrong parsing. i.e, phonenumbers.parse(+32477111111, 'US') will
find the number as US-based when it is a belgian number.

solves issue #22513

Also corrected a faulty behaviour where the option to have a number parsed in its international format
field `phone_international_format` on `res.company` was never properly assessed when the field comany_id of a
model was not present in the model's form view. Added company_id field on opportunity's form view for a proper fix.

Also fixed a syntax error in a logger.
This commit is contained in:
Rémi Rahir
2018-02-07 14:22:46 +01:00
parent 2c6bad6b75
commit 376415052c
6 changed files with 23 additions and 9 deletions
@@ -13,6 +13,7 @@ CRM Phone Numbers Validation
This module allows for validate and format phone numbers for leads and contacts.""",
'data': [
'views/res_config_settings_views.xml',
'views/crm_lead_views.xml'
],
'depends': [
'phone_validation',
@@ -8,13 +8,12 @@ class Lead(models.Model):
_name = 'crm.lead'
_inherit = ['crm.lead', 'phone.validation.mixin']
@api.onchange('phone', 'country_id')
@api.onchange('phone', 'country_id', 'company_id')
def _onchange_phone_validation(self):
if self.phone:
self.phone = self.phone_format(self.phone)
@api.onchange('mobile', 'country_id')
@api.onchange('mobile', 'country_id', 'company_id')
def _onchange_mobile_validation(self):
if self.mobile:
self.mobile = self.phone_format(self.mobile)
@@ -8,12 +8,12 @@ class Contact(models.Model):
_name = 'res.partner'
_inherit = ['res.partner', 'phone.validation.mixin']
@api.onchange('phone', 'country_id')
@api.onchange('phone', 'country_id', 'company_id')
def _onchange_phone_validation(self):
if self.phone:
self.phone = self.phone_format(self.phone)
@api.onchange('mobile', 'country_id')
@api.onchange('mobile', 'country_id', 'company_id')
def _onchange_mobile_validation(self):
if self.mobile:
self.mobile = self.phone_format(self.mobile)
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="crm_lead_opportunity_view_form" model="ir.ui.view">
<field name="name">crm.lead.form.opportunity.inherit.crm.phone.validation</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_form_view_oppor" />
<field name="arch" type="xml">
<xpath expr="//group[@name='Misc']" position="inside">
<field name="company_id" invisible="1"/>
</xpath>
</field>
</record>
</odoo>
@@ -11,20 +11,21 @@ class PhoneValidationMixin(models.AbstractModel):
def _phone_get_country(self):
if 'country_id' in self:
return self.country_id
return self.env.user.company_id.country_id
def _phone_get_always_international(self):
if 'company_id' in self:
if 'company_id' in self and self.country_id:
return self.company_id.phone_international_format == 'prefix'
return self.env.user.company_id.phone_international_format == 'prefix'
def phone_format(self, number, country=None, company=None):
country = country or self._phone_get_country()
if not country:
return number
always_international = company.phone_international_format == 'prefix' if company else self._phone_get_always_international()
return phone_validation.phone_format(
number,
country.code if country else None,
country.phone_code if country else None,
always_international=always_international,
raise_exception=True
raise_exception=False
)
@@ -41,7 +41,7 @@ try:
if raise_exception:
raise
else:
_logger.warning(_('Unable to format %s:\n%s') % number, e)
_logger.warning(_('Unable to format %s:\n%s'), number, e)
return number
if always_international or phone_nbr.country_code != country_phone_code:
phone_fmt = phonenumbers.PhoneNumberFormat.INTERNATIONAL