Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] phone_validation: remove phone.validation.mixin and inline it #63333

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions addons/crm/models/crm_lead.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
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
Expand Down Expand Up @@ -82,7 +82,7 @@ class Lead(models.Model):
'mail.activity.mixin',
'utm.mixin',
'format.address.mixin',
'phone.validation.mixin']
]
_primary_email = 'email_from'

# Description
Expand Down Expand Up @@ -330,8 +330,8 @@ def _inverse_phone(self):
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

Expand Down Expand Up @@ -414,8 +414,8 @@ def _compute_ribbon_message(self):
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

Expand Down Expand Up @@ -456,12 +456,12 @@ def _search_phone_mobile_search(self, operator, value):
@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
Expand Down
10 changes: 3 additions & 7 deletions addons/phone_validation/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
'name': 'Phone Numbers Validation',
'version': '2.0',
'version': '2.1',
'summary': 'Validate and format phone numbers',
'sequence': '9999',
'category': 'Hidden',
Expand All @@ -17,12 +17,8 @@
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',
Expand Down
1 change: 0 additions & 1 deletion addons/phone_validation/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 0 additions & 27 deletions addons/phone_validation/models/phone_validation_mixin.py

This file was deleted.

19 changes: 16 additions & 3 deletions addons/phone_validation/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
awa-odoo marked this conversation as resolved.
Show resolved Hide resolved
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
)
18 changes: 14 additions & 4 deletions addons/website_crm/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -20,7 +21,11 @@ def _get_phone_fields_to_validate(self):
# 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'):
tde-banana-odoo marked this conversation as resolved.
Show resolved Hide resolved
# 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:
Expand All @@ -30,12 +35,17 @@ def _handle_website_form(self, model_name, **kwargs):
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'):
Expand Down