diff --git a/partner_email_unique/README.rst b/partner_email_unique/README.rst new file mode 100644 index 000000000..e36c6eaa1 --- /dev/null +++ b/partner_email_unique/README.rst @@ -0,0 +1,80 @@ +==================== +Partner unique email +==================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github + :target: https://github.com/OCA/partner-contact/tree/9.0/partner_email_unique + :alt: OCA/partner-contact +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/partner-contact-9-0/partner-contact-9-0-partner_email_unique + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/134/9.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Add an unique constraint to partner email field + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +* Try to create two partners with the same email. + +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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Coop IT Easy SC + +Contributors +~~~~~~~~~~~~ + +* Robin Keunen + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/partner-contact `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/partner_email_unique/__init__.py b/partner_email_unique/__init__.py new file mode 100644 index 000000000..cb1db7273 --- /dev/null +++ b/partner_email_unique/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2019 Coop IT Easy SC +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import models diff --git a/partner_email_unique/__manifest__.py b/partner_email_unique/__manifest__.py new file mode 100644 index 000000000..2e1ff83a4 --- /dev/null +++ b/partner_email_unique/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2019 Coop IT Easy SC +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +{ + "name": "Partner unique email", + "summary": "Add an unique constraint to email field", + "version": "12.0.1.0.0", + "category": "Customer Relationship Management", + "website": "https://github.com/oca/partner-contact", + "author": "Coop IT Easy SC, " "Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": [ + "base", + ], +} diff --git a/partner_email_unique/i18n/partner_email_unique.pot b/partner_email_unique/i18n/partner_email_unique.pot new file mode 100644 index 000000000..bd4ee029c --- /dev/null +++ b/partner_email_unique/i18n/partner_email_unique.pot @@ -0,0 +1,26 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * partner_email_unique +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 9.0c\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: partner_email_unique +#: model:ir.model,name:partner_email_unique.model_res_partner +msgid "Partner" +msgstr "" + +#. module: partner_email_unique +#: code:addons/partner_email_unique/models/res_partner.py:35 +#, python-format +msgid "This email is already set to partner '%s'" +msgstr "" + diff --git a/partner_email_unique/models/__init__.py b/partner_email_unique/models/__init__.py new file mode 100644 index 000000000..e83b06b2e --- /dev/null +++ b/partner_email_unique/models/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2019 Coop IT Easy SC +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import res_partner diff --git a/partner_email_unique/models/res_partner.py b/partner_email_unique/models/res_partner.py new file mode 100644 index 000000000..87705cd68 --- /dev/null +++ b/partner_email_unique/models/res_partner.py @@ -0,0 +1,39 @@ +# Copyright 2019 Coop IT Easy SC +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +import os + +from odoo import models, api, _, fields +from odoo.exceptions import ValidationError +from odoo.tools import config + + +class ResPartner(models.Model): + _inherit = "res.partner" + + email = fields.Char(copy=False) + + @api.multi + @api.constrains("email") + def _check_email(self): + if ( + config["test_enable"] or os.environ.get("TESTS") + ) and not self.env.context.get("test_partner_email_unique"): + return + + for partner in self: + domain = [ + ("id", "!=", partner.id), + ("email", "=", partner.email), + ("email", "!=", False), + ] + other_partners = self.search(domain) + + # active_test is False when called from + # base.partner.merge.automatic.wizard + if other_partners and self.env.context.get("active_test", True): + raise ValidationError( + _("This email is already set to partner '%s'") + % other_partners[0].display_name + ) diff --git a/partner_email_unique/readme/CONTRIBUTORS.rst b/partner_email_unique/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..0ecb06105 --- /dev/null +++ b/partner_email_unique/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Robin Keunen diff --git a/partner_email_unique/readme/DESCRIPTION.rst b/partner_email_unique/readme/DESCRIPTION.rst new file mode 100644 index 000000000..703b3c996 --- /dev/null +++ b/partner_email_unique/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Add an unique constraint to partner email field diff --git a/partner_email_unique/readme/USAGE.rst b/partner_email_unique/readme/USAGE.rst new file mode 100644 index 000000000..73f852924 --- /dev/null +++ b/partner_email_unique/readme/USAGE.rst @@ -0,0 +1,3 @@ +To use this module, you need to: + +* Try to create two partners with the same email. diff --git a/partner_email_unique/static/description/icon.png b/partner_email_unique/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/partner_email_unique/static/description/icon.png differ diff --git a/partner_email_unique/static/description/index.html b/partner_email_unique/static/description/index.html new file mode 100644 index 000000000..68e83a9dd --- /dev/null +++ b/partner_email_unique/static/description/index.html @@ -0,0 +1,427 @@ + + + + + + +Partner unique email + + + +
+

Partner unique email

+ + +

Beta License: AGPL-3 OCA/partner-contact Translate me on Weblate Try me on Runbot

+

Add an unique constraint to partner email field

+

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  • Try to create two partners with the same email.
  • +
+
+
+

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.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Coop IT Easy SC
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

This module is part of the OCA/partner-contact project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/partner_email_unique/tests/__init__.py b/partner_email_unique/tests/__init__.py new file mode 100644 index 000000000..958451c68 --- /dev/null +++ b/partner_email_unique/tests/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2019 Coop IT Easy SC +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import test_res_partner_email diff --git a/partner_email_unique/tests/test_res_partner_email.py b/partner_email_unique/tests/test_res_partner_email.py new file mode 100644 index 000000000..308716473 --- /dev/null +++ b/partner_email_unique/tests/test_res_partner_email.py @@ -0,0 +1,44 @@ +# Copyright 2019 Coop IT Easy SC +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo.tests import common +from odoo.exceptions import ValidationError + + +class TestResPartnerEmailUnique(common.SavepointCase): + @classmethod + def setUpClass(cls): + super(TestResPartnerEmailUnique, cls).setUpClass() + partner_obj = cls.env["res.partner"].with_context( + {"test_partner_email_unique": True} + ) + cls.partner1 = partner_obj.create( + { + "name": "Partner1", + } + ) + cls.partner2 = partner_obj.create( + { + "name": "Partner2", + } + ) + + def test_check_email(self): + + # Test can create/modify partners with different email + self.partner1.email = "same_email@test.com" + self.partner2.email = "different_email@test.com" + self.assertNotEqual(self.partner1.email, self.partner2.email) + self.partner2.ref = False + + # Test can't create/modify partner with same email + with self.assertRaises(ValidationError): + self.partner2.email = "same_email@test.com" + + # Empty email addresses don't raise + self.partner1.email = False + self.partner2.email = False + + def test_copy_does_not_raise_duplicate_email_error(self): + self.partner1.copy()