diff --git a/addons/website_crm_phone_validation/__init__.py b/addons/website_crm_phone_validation/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..dd15b5c32e51cc3cbbc1d9a657a64763d71e22b9 --- /dev/null +++ b/addons/website_crm_phone_validation/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import controllers diff --git a/addons/website_crm_phone_validation/__manifest__.py b/addons/website_crm_phone_validation/__manifest__.py new file mode 100644 index 0000000000000000000000000000000000000000..2462a69b45599619889cf5c0800e8121a69604e7 --- /dev/null +++ b/addons/website_crm_phone_validation/__manifest__.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +{ + 'name': 'Contact Form Number Validation', + 'summary': 'Validate and format contact form numbers', + 'sequence': '9999', + 'category': 'Hidden', + 'description': """ +Contact Number Validation on Website +==================================== + +Validate contact (phone,mobile,fax) numbers and normalize them on leads and contacts: +- use the national format for your company country +- use the international format for all others + """, + 'data': [], + 'depends': [ + 'crm_phone_validation', + 'website_crm', + 'website_form', + ], + 'auto_install': True, +} diff --git a/addons/website_crm_phone_validation/controllers/__init__.py b/addons/website_crm_phone_validation/controllers/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..25803fb18873177df134c2b6a8936c7686a50602 --- /dev/null +++ b/addons/website_crm_phone_validation/controllers/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import website_form diff --git a/addons/website_crm_phone_validation/controllers/website_form.py b/addons/website_crm_phone_validation/controllers/website_form.py new file mode 100644 index 0000000000000000000000000000000000000000..73dd2ebff5724bbfbde034bd0cc184f7f859e212 --- /dev/null +++ b/addons/website_crm_phone_validation/controllers/website_form.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.addons.website_form.controllers.main import WebsiteForm +from odoo.http import request, route + +import json + + +class WebsiteForm(WebsiteForm): + + def _get_country(self): + country_code = request.session.geoip and request.session.geoip.get('country_code') or False + if country_code: + return request.env['res.country'].sudo().search([('code', '=', country_code)], limit=1) + return request.env['res.country'] + + def _get_phone_fields_to_validate(self): + return ['phone', 'mobile', 'fax'] + + @route('/website_form/<string:model_name>', type='http', auth="public", methods=['POST'], website=True) + def website_form(self, model_name, **kwargs): + model_record = request.env['ir.model'].sudo().search([('model', '=', model_name), ('website_form_access', '=', True)]) + if not model_record or not hasattr(request.env[model_name], 'phone_format'): + return super(WebsiteForm, self).website_form(model_name, **kwargs) + + try: + data = self.extract_data(model_record, request.params) + except: + # no specific management, super will do it + pass + else: + record = data.get('record', {}) + phone_fields = self._get_phone_fields_to_validate() + country = self._get_country() + invalid_fields = {} + 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}) + + return super(WebsiteForm, self).website_form(model_name, **kwargs)