From 99cbec4f7ea2194f8d28af08dbac36e5079da731 Mon Sep 17 00:00:00 2001 From: denislour Date: Mon, 18 Dec 2017 18:31:01 +0700 Subject: [PATCH] Add new module partner-email-check (#496) * Add new module partner-email-check Validate email input --- partner_email_check/README.rst | 56 +++++++++++++++++++ partner_email_check/__init__.py | 5 ++ partner_email_check/__manifest__.py | 19 +++++++ partner_email_check/models/__init__.py | 5 ++ partner_email_check/models/res_partner.py | 36 ++++++++++++ partner_email_check/tests/__init__.py | 5 ++ .../tests/test_partner_email_check.py | 24 ++++++++ 7 files changed, 150 insertions(+) create mode 100644 partner_email_check/README.rst create mode 100644 partner_email_check/__init__.py create mode 100644 partner_email_check/__manifest__.py create mode 100644 partner_email_check/models/__init__.py create mode 100644 partner_email_check/models/res_partner.py create mode 100644 partner_email_check/tests/__init__.py create mode 100644 partner_email_check/tests/test_partner_email_check.py diff --git a/partner_email_check/README.rst b/partner_email_check/README.rst new file mode 100644 index 000000000..98e714b1b --- /dev/null +++ b/partner_email_check/README.rst @@ -0,0 +1,56 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +=================== +Partner Email Check +=================== + +This module validate the field ``email`` in the module ``res.partner``. + +Configuration +============= + +Install python package validate_email: ``sudo pip install validate_email``. + +Usage +===== + +This module integrate automatically in all of the view ``res.partner`` + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Vo Hoang Dat +* Jean-Charles Drubay + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/partner_email_check/__init__.py b/partner_email_check/__init__.py new file mode 100644 index 000000000..0ba704c16 --- /dev/null +++ b/partner_email_check/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Komit +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/partner_email_check/__manifest__.py b/partner_email_check/__manifest__.py new file mode 100644 index 000000000..74b6ac7a5 --- /dev/null +++ b/partner_email_check/__manifest__.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Komit +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'Email Format Checker', + 'version': '10.0.1.0.0', + 'summary': 'Validate email address field', + 'author': "Komit, Odoo Community Association (OCA)", + 'website': 'http://komit-consulting.com', + 'category': 'Tools', + 'depends': ['base'], + 'installable': True, + 'application': False, + 'license': 'AGPL-3', + 'external_dependencies': { + 'python': ['validate_email'] + }, +} diff --git a/partner_email_check/models/__init__.py b/partner_email_check/models/__init__.py new file mode 100644 index 000000000..280addc7c --- /dev/null +++ b/partner_email_check/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Komit +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import res_partner diff --git a/partner_email_check/models/res_partner.py b/partner_email_check/models/res_partner.py new file mode 100644 index 000000000..afb3d47ae --- /dev/null +++ b/partner_email_check/models/res_partner.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Komit +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import logging +from odoo import api, models, _ +from odoo.exceptions import UserError + +_logger = logging.getLogger(__name__) + +try: + from validate_email import validate_email +except ImportError: + _logger.error('Cannot import "validate_email".') + + def validate_email(email): + _logger.warning( + 'Can not validate email, ' + 'python dependency required "validate_email"') + return True + + +class ResPartner(models.Model): + _inherit = 'res.partner' + + @api.constrains('email') + def constrains_email(self): + for rec in self.filtered("email"): + self.email_check(rec.email) + + @api.model + def email_check(self, email): + if validate_email(email): + return True + else: + raise UserError(_('Invalid e-mail!')) diff --git a/partner_email_check/tests/__init__.py b/partner_email_check/tests/__init__.py new file mode 100644 index 000000000..413174026 --- /dev/null +++ b/partner_email_check/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Komit +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_partner_email_check diff --git a/partner_email_check/tests/test_partner_email_check.py b/partner_email_check/tests/test_partner_email_check.py new file mode 100644 index 000000000..6050f3ad4 --- /dev/null +++ b/partner_email_check/tests/test_partner_email_check.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Komit +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase + + +class TestPartnerEmailCheck(TransactionCase): + def setUp(self): + super(TestPartnerEmailCheck, self).setUp() + self.test_partner = self.env['res.partner'].create({ + 'name': 'test', + }) + + def test_bad_email(self): + """Test rejection of bad emails.""" + with self.assertRaises(ValidationError): + self.test_partner.email = 'bad@email@domain..com' + + def test_good_email(self): + """Test acceptance of good""" + self.test_partner.email = 'goodemail@domain.com' + self.assertTrue(self.test_partner.email)