From 8b9bcf1b7efff6423763f69aad171c7c98dce5bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rahir?= Date: Fri, 9 Feb 2018 15:24:54 +0100 Subject: [PATCH] [FIX] crm_phone_validation: fix lead phone validation Fixes country_id and company_id mismatch in 65832dc. Removes the view added by said commit as it was not a true stable fix since it required users to update the module. This commit uses a workaround to get the missing company_id field which is monkey-patching the fields_view_get of the model, as suggested by rco. This commit also removes the exception catches that were rendered useless by the False value given to the raise_exception parameter of phone_format, introduced in 65832dc as well. This commits fixes the behaviour in the website form as the phone numbers were sanitized but never saved as such on their record. This commit also renames the Phone Validation config setting to suit its true functionality. --- addons/crm/views/res_config_settings_views.xml | 2 +- addons/crm_phone_validation/__manifest__.py | 1 - .../crm_phone_validation/views/crm_lead_views.xml | 13 ------------- .../models/phone_validation_mixin.py | 5 +++-- .../controllers/website_form.py | 14 ++++---------- 5 files changed, 8 insertions(+), 27 deletions(-) delete mode 100644 addons/crm_phone_validation/views/crm_lead_views.xml diff --git a/addons/crm/views/res_config_settings_views.xml b/addons/crm/views/res_config_settings_views.xml index e06b7c9f7ec..43c7207d5d1 100644 --- a/addons/crm/views/res_config_settings_views.xml +++ b/addons/crm/views/res_config_settings_views.xml @@ -61,7 +61,7 @@
diff --git a/addons/crm_phone_validation/__manifest__.py b/addons/crm_phone_validation/__manifest__.py index 72a21dbfcfa..7c729bfae3e 100644 --- a/addons/crm_phone_validation/__manifest__.py +++ b/addons/crm_phone_validation/__manifest__.py @@ -13,7 +13,6 @@ 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', diff --git a/addons/crm_phone_validation/views/crm_lead_views.xml b/addons/crm_phone_validation/views/crm_lead_views.xml deleted file mode 100644 index bcf1840bd7a..00000000000 --- a/addons/crm_phone_validation/views/crm_lead_views.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - crm.lead.form.opportunity.inherit.crm.phone.validation - crm.lead - - - - - - - - diff --git a/addons/phone_validation/models/phone_validation_mixin.py b/addons/phone_validation/models/phone_validation_mixin.py index 4cd9d8eb2bb..02e4c6aa82a 100644 --- a/addons/phone_validation/models/phone_validation_mixin.py +++ b/addons/phone_validation/models/phone_validation_mixin.py @@ -9,11 +9,12 @@ class PhoneValidationMixin(models.AbstractModel): _name = 'phone.validation.mixin' def _phone_get_country(self): - if 'country_id' in self: + if 'country_id' in self and self.country_id: return self.country_id + return self.env.user.company_id.country_id def _phone_get_always_international(self): - if 'company_id' in self and self.country_id: + if 'company_id' in self and self.company_id: return self.company_id.phone_international_format == 'prefix' return self.env.user.company_id.phone_international_format == 'prefix' diff --git a/addons/website_crm_phone_validation/controllers/website_form.py b/addons/website_crm_phone_validation/controllers/website_form.py index 3713fe86f9e..c404bb87724 100644 --- a/addons/website_crm_phone_validation/controllers/website_form.py +++ b/addons/website_crm_phone_validation/controllers/website_form.py @@ -4,8 +4,6 @@ from odoo.addons.website_form.controllers.main import WebsiteForm from odoo.http import request, route -import json - class WebsiteForm(WebsiteForm): @@ -32,17 +30,13 @@ class WebsiteForm(WebsiteForm): else: record = data.get('record', {}) phone_fields = self._get_phone_fields_to_validate() - country = self._get_country() - invalid_fields = {} + country = request.env['res.country'].browse(record.get('country_id')) + contact_country = country.exists() and country or self._get_country() for phone_field in phone_fields: if not record.get(phone_field): continue number = record[phone_field] - try: - request.env[model_name].phone_format(number, country) - except Exception as error: - invalid_fields[phone_field] = error.args[0] - if invalid_fields: - return json.dumps({'error_fields': invalid_fields}) + fmt_number = request.env[model_name].phone_format(number, contact_country) + request.params.update({phone_field: fmt_number}) return super(WebsiteForm, self).website_form(model_name, **kwargs)