[REF] phone_validation, various: remove phone.validation.mixin and inline it

PURPOSE

Remove complex or technical code adding few value.

RATIONALE

phone.validation.mixin is used in two models: crm.lead and res.partner. It only
add a phone_format method used in onchange. We can inline this code in those
two models and remove the whole mixin itself. It lessens inherit and class
complexity.

SPECIFICATIONS

Remove phone.validation.mixin. Inline code in crm.lead (CRM application)
and res.partner (phone validation module). Make phone_format private as
it is now some internal tool method on those two models.

Everything should behave as before this commit. No functional change is
intended.

LINKS

Task ID-2416789
COM PR odoo/odoo#63333
ENT PR odoo/enterprise#15296
UPG PR odoo/upgrade#2025

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
Thibault Delavallée
2021-01-15 08:14:01 +00:00
parent 32f28ff948
commit 75f23a6a89
6 changed files with 41 additions and 50 deletions
+8 -8
View File
@@ -7,11 +7,11 @@ from datetime import date, datetime, timedelta
from psycopg2 import sql
from odoo import api, fields, models, tools, SUPERUSER_ID
from odoo.addons.phone_validation.tools import phone_validation
from odoo.osv import expression
from odoo.tools.translate import _
from odoo.tools import email_re, email_split
from odoo.exceptions import UserError, AccessError
from odoo.addons.phone_validation.tools import phone_validation
from collections import OrderedDict, defaultdict
from . import crm_stage
@@ -82,7 +82,7 @@ class Lead(models.Model):
'mail.activity.mixin',
'utm.mixin',
'format.address.mixin',
'phone.validation.mixin']
]
_primary_email = 'email_from'
# Description
@@ -330,8 +330,8 @@ class Lead(models.Model):
lead.partner_id.phone = lead.phone
# compare formatted values as we may have encoding differences between equivalent numbers
else:
lead_phone_formatted = lead.phone_format(lead.phone)
partner_phone_formatted = lead.phone_format(lead.partner_id.phone)
lead_phone_formatted = lead.phone_get_sanitized_number(number_fname='phone')
partner_phone_formatted = lead.partner_id.phone_get_sanitized_number(number_fname='phone')
if lead_phone_formatted != partner_phone_formatted:
lead.partner_id.phone = lead.phone
@@ -414,8 +414,8 @@ class Lead(models.Model):
will_write_phone = True
# otherwise compare formatted values as we may have encoding differences
else:
lead_phone_formatted = lead.phone_format(lead.phone)
partner_phone_formatted = lead.phone_format(lead.partner_id.phone)
lead_phone_formatted = lead.phone_get_sanitized_number(number_fname='phone')
partner_phone_formatted = lead.partner_id.phone_get_sanitized_number(number_fname='phone')
if lead_phone_formatted != partner_phone_formatted:
will_write_phone = True
@@ -456,12 +456,12 @@ class Lead(models.Model):
@api.onchange('phone', 'country_id', 'company_id')
def _onchange_phone_validation(self):
if self.phone:
self.phone = self.phone_format(self.phone)
self.phone = self.phone_get_sanitized_number(number_fname='phone', force_format='INTERNATIONAL') or self.phone
@api.onchange('mobile', 'country_id', 'company_id')
def _onchange_mobile_validation(self):
if self.mobile:
self.mobile = self.phone_format(self.mobile)
self.mobile = self.phone_get_sanitized_number(number_fname='mobile', force_format='INTERNATIONAL') or self.mobile
def _prepare_values_from_partner(self, partner):
""" Get a dictionary with values coming from partner information to
+3 -7
View File
@@ -3,7 +3,7 @@
{
'name': 'Phone Numbers Validation',
'version': '2.0',
'version': '2.1',
'summary': 'Validate and format phone numbers',
'sequence': '9999',
'category': 'Hidden',
@@ -17,12 +17,8 @@ according to a destination country.
It also adds phone blacklist management through a specific model storing
blacklisted phone numbers.
It adds two mixins :
* phone.validation.mixin: parsing / formatting helpers on records, to be
used for example in number fields onchange;
* mail.thread.phone: handle sanitation and blacklist of records numbers;
""",
It adds mail.thread.phone mixin that handles sanitation and blacklist of
records numbers. """,
'data': [
'security/ir.model.access.csv',
'views/phone_blacklist_views.xml',
@@ -2,6 +2,5 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import phone_blacklist
from . import phone_validation_mixin
from . import mail_thread_phone
from . import res_partner
@@ -1,27 +0,0 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models
from odoo.addons.phone_validation.tools import phone_validation
class PhoneValidationMixin(models.AbstractModel):
_name = 'phone.validation.mixin'
_description = 'Phone Validation Mixin'
def _phone_get_country(self):
if 'country_id' in self and self.country_id:
return self.country_id
return self.env.company.country_id
def phone_format(self, number, country=None, company=None):
country = country or self._phone_get_country()
if not country:
return number
return phone_validation.phone_format(
number,
country.code if country else None,
country.phone_code if country else None,
force_format='INTERNATIONAL',
raise_exception=False
)
+16 -3
View File
@@ -2,18 +2,31 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, models
from odoo.addons.phone_validation.tools import phone_validation
class Partner(models.Model):
_name = 'res.partner'
_inherit = ['res.partner', 'phone.validation.mixin']
_inherit = ['res.partner']
@api.onchange('phone', 'country_id', 'company_id')
def _onchange_phone_validation(self):
if self.phone:
self.phone = self.phone_format(self.phone)
self.phone = self._phone_format(self.phone)
@api.onchange('mobile', 'country_id', 'company_id')
def _onchange_mobile_validation(self):
if self.mobile:
self.mobile = self.phone_format(self.mobile)
self.mobile = self._phone_format(self.mobile)
def _phone_format(self, number, country=None, company=None):
country = country or self.country_id or self.env.company.country_id
if not country:
return number
return phone_validation.phone_format(
number,
country.code if country else None,
country.phone_code if country else None,
force_format='INTERNATIONAL',
raise_exception=False
)
+14 -4
View File
@@ -2,8 +2,9 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import http
from odoo.http import request
from odoo.addons.phone_validation.tools import phone_validation
from odoo.addons.website_form.controllers import main
from odoo.http import request
class WebsiteForm(main.WebsiteForm):
@@ -20,7 +21,11 @@ class WebsiteForm(main.WebsiteForm):
# Check and insert values from the form on the model <model> + validation phone fields
def _handle_website_form(self, model_name, **kwargs):
model_record = request.env['ir.model'].sudo().search([('model', '=', model_name), ('website_form_access', '=', True)])
if model_record and hasattr(request.env[model_name], 'phone_format'):
if model_record and hasattr(request.env[model_name], '_phone_format') or hasattr(request.env[model_name], 'phone_get_sanitized_number'):
# filter on either custom _phone_format method, either phone_get_sanitized_number but directly
# call phone_format from phone validation herebelow to simplify things as we don't have real
# records but a dictionary of value at this point (record.phone_get_sanitized_number would
# not work)
try:
data = self.extract_data(model_record, request.params)
except:
@@ -30,12 +35,17 @@ class WebsiteForm(main.WebsiteForm):
record = data.get('record', {})
phone_fields = self._get_phone_fields_to_validate()
country = request.env['res.country'].browse(record.get('country_id'))
contact_country = country.exists() and country or self._get_country()
contact_country = country if country.exists() else self._get_country()
for phone_field in phone_fields:
if not record.get(phone_field):
continue
number = record[phone_field]
fmt_number = request.env[model_name].phone_format(number, contact_country)
fmt_number = phone_validation.phone_format(
number, contact_country.code if contact_country else None,
contact_country.phone_code if contact_country else None,
force_format='INTERNATIONAL',
raise_exception=False
)
request.params.update({phone_field: fmt_number})
if model_name == 'crm.lead' and not request.params.get('state_id'):