From 75f23a6a8984065deb88ecfbf5863b93fcccf60b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 14 Dec 2020 13:55:04 +0000 Subject: [PATCH] [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) --- addons/crm/models/crm_lead.py | 16 +++++------ addons/phone_validation/__manifest__.py | 10 +++---- addons/phone_validation/models/__init__.py | 1 - .../models/phone_validation_mixin.py | 27 ------------------- addons/phone_validation/models/res_partner.py | 19 ++++++++++--- addons/website_crm/controllers/main.py | 18 ++++++++++--- 6 files changed, 41 insertions(+), 50 deletions(-) delete mode 100644 addons/phone_validation/models/phone_validation_mixin.py diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 482b715a600..1c8cf04fe38 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -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 diff --git a/addons/phone_validation/__manifest__.py b/addons/phone_validation/__manifest__.py index a87d866101e..4f59487be94 100644 --- a/addons/phone_validation/__manifest__.py +++ b/addons/phone_validation/__manifest__.py @@ -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', diff --git a/addons/phone_validation/models/__init__.py b/addons/phone_validation/models/__init__.py index 1402c4c2c28..6001c915ce8 100644 --- a/addons/phone_validation/models/__init__.py +++ b/addons/phone_validation/models/__init__.py @@ -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 diff --git a/addons/phone_validation/models/phone_validation_mixin.py b/addons/phone_validation/models/phone_validation_mixin.py deleted file mode 100644 index a430a537c21..00000000000 --- a/addons/phone_validation/models/phone_validation_mixin.py +++ /dev/null @@ -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 - ) diff --git a/addons/phone_validation/models/res_partner.py b/addons/phone_validation/models/res_partner.py index d0b125e6d64..99f1ff13cf4 100644 --- a/addons/phone_validation/models/res_partner.py +++ b/addons/phone_validation/models/res_partner.py @@ -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 + ) diff --git a/addons/website_crm/controllers/main.py b/addons/website_crm/controllers/main.py index 21645139bc4..445dc0a83d8 100644 --- a/addons/website_crm/controllers/main.py +++ b/addons/website_crm/controllers/main.py @@ -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 + 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'):