diff --git a/mass_mailing_unique/README.rst b/mass_mailing_unique/README.rst index b45e9353..303882d1 100644 --- a/mass_mailing_unique/README.rst +++ b/mass_mailing_unique/README.rst @@ -1,65 +1 @@ -.. 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 - -=============================== -Unique records for mass mailing -=============================== - -This module extends the functionality of mass mailing lists to disable -duplicate entries in list names and contact emails per list. - -This way you will avoid sending the same message more than once to the same -contact when selecting a mailing list, and you will avoid conflicts when -importing contacts to a list that has a duplicated name. - -Installation -============ - -Before installing this module, you need to: - -* Remove all duplicated list names. -* Remove all duplicated emails in each list. - -Usage -===== - -To use this module, you need to try to create a duplicated mailing list, or a -duplicated email inside one. You will not can. - -.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas - :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/205/10.0 - -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 -======= - -Contributors ------------- - -* Jairo Llopis -* Vicent Cubells -* Pedro M. Baeza - -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. +**This file is going to be generated by oca-gen-addon-readme.** \ No newline at end of file diff --git a/mass_mailing_unique/__init__.py b/mass_mailing_unique/__init__.py index 70227430..268d80e5 100644 --- a/mass_mailing_unique/__init__.py +++ b/mass_mailing_unique/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis # Copyright 2016 Tecnativa - Vicent Cubells # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). diff --git a/mass_mailing_unique/__manifest__.py b/mass_mailing_unique/__manifest__.py index fe5bf11d..beaeae84 100644 --- a/mass_mailing_unique/__manifest__.py +++ b/mass_mailing_unique/__manifest__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis # Copyright 2016 Tecnativa - Vicent Cubells # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). @@ -6,9 +5,9 @@ { "name": "Unique records for mass mailing", "summary": "Avoids duplicate mailing lists and contacts", - "version": "10.0.1.0.0", + "version": "11.0.1.0.0", "category": "Marketing", - "website": "https://tecnativa.com", + "website": "https://github.com/OCA/social", "author": "Tecnativa, " "Odoo Community Association (OCA)", "license": "AGPL-3", diff --git a/mass_mailing_unique/hooks.py b/mass_mailing_unique/hooks.py index d06d92e3..8927e114 100644 --- a/mass_mailing_unique/hooks.py +++ b/mass_mailing_unique/hooks.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis # Copyright 2016 Tecnativa - Vicent Cubells # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). @@ -19,11 +18,13 @@ def pre_init_hook(cr): errors = list() # Search for duplicates in emails - cr.execute("""SELECT c.email, l.name, COUNT(c.id) + cr.execute("""SELECT LOWER(c.email) AS e, l.name, COUNT(c.id) FROM mail_mass_mailing_contact AS c - INNER JOIN mail_mass_mailing_list AS l ON c.list_id = l.id - GROUP BY l.name, l.id, c.email + INNER JOIN mail_mass_mailing_contact_list_rel AS cl + ON cl.contact_id = c.id + INNER JOIN mail_mass_mailing_list AS l ON cl.list_id = l.id + GROUP BY l.name, e HAVING COUNT(c.id) > 1""") for result in cr.fetchall(): errors.append( diff --git a/mass_mailing_unique/models/__init__.py b/mass_mailing_unique/models/__init__.py index f4f8574f..8251b7ac 100644 --- a/mass_mailing_unique/models/__init__.py +++ b/mass_mailing_unique/models/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis # Copyright 2016 Tecnativa - Vicent Cubells # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). diff --git a/mass_mailing_unique/models/mass_mailing.py b/mass_mailing_unique/models/mass_mailing.py index 1b05ba3c..849591b6 100644 --- a/mass_mailing_unique/models/mass_mailing.py +++ b/mass_mailing_unique/models/mass_mailing.py @@ -1,18 +1,25 @@ -# -*- coding: utf-8 -*- # Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis # Copyright 2016 Tecnativa - Vicent Cubells # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import models +from odoo import models, api, _, tools +from odoo.exceptions import ValidationError class MailMassMailingContact(models.Model): _inherit = "mail.mass_mailing.contact" - _sql_constraints = [ - ("unique_mail_per_list", "UNIQUE(list_id, email)", - "Cannot have the same email more than once in the same list.") - ] + + @api.constrains('email', 'list_ids') + def _check_email_list_ids(self): + for contact in self: + other_contact = self.search([ + ('email', '=ilike', tools.escape_psql(contact.email)), + ('id', '!=', contact.id) + ]) + if contact.list_ids & other_contact.mapped('list_ids'): + raise ValidationError(_("Cannot have the same email more " + "than once in the same list")) class MailMassMailingList(models.Model): diff --git a/mass_mailing_unique/readme/CONTRIBUTORS.rst b/mass_mailing_unique/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..1311ad5b --- /dev/null +++ b/mass_mailing_unique/readme/CONTRIBUTORS.rst @@ -0,0 +1,6 @@ +* `Tecnativa `_: + + * Jairo Llopis + * Vicent Cubells + * Pedro M. Baeza + * Ernesto Tejeda diff --git a/mass_mailing_unique/readme/DESCRIPTION.rst b/mass_mailing_unique/readme/DESCRIPTION.rst new file mode 100644 index 00000000..5e7fae79 --- /dev/null +++ b/mass_mailing_unique/readme/DESCRIPTION.rst @@ -0,0 +1,6 @@ +This module extends the functionality of mass mailing lists to disable +duplicate entries in list names and contact emails per list. + +This way you will avoid sending the same message more than once to the same +contact when selecting a mailing list, and you will avoid conflicts when +importing contacts to a list that has a duplicated name. diff --git a/mass_mailing_unique/readme/INSTALL.rst b/mass_mailing_unique/readme/INSTALL.rst new file mode 100644 index 00000000..b9110603 --- /dev/null +++ b/mass_mailing_unique/readme/INSTALL.rst @@ -0,0 +1,4 @@ +Before installing this module, you need to: + +* Remove all duplicated list names. +* Remove all duplicated emails in each list. diff --git a/mass_mailing_unique/readme/USAGE.rst b/mass_mailing_unique/readme/USAGE.rst new file mode 100644 index 00000000..70eaba15 --- /dev/null +++ b/mass_mailing_unique/readme/USAGE.rst @@ -0,0 +1,2 @@ +To use this module, you need to try to create a duplicated mailing list, or a +duplicated email inside one. You will not can. diff --git a/mass_mailing_unique/tests/__init__.py b/mass_mailing_unique/tests/__init__.py index e876756a..8df8686a 100644 --- a/mass_mailing_unique/tests/__init__.py +++ b/mass_mailing_unique/tests/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from . import test_mass_mailing_unique diff --git a/mass_mailing_unique/tests/test_mass_mailing_unique.py b/mass_mailing_unique/tests/test_mass_mailing_unique.py index 8b29a172..f244fbbe 100644 --- a/mass_mailing_unique/tests/test_mass_mailing_unique.py +++ b/mass_mailing_unique/tests/test_mass_mailing_unique.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2016 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). @@ -17,6 +16,7 @@ class TestMassMailingUnique(common.SavepointCase): cls.contact1 = cls.env['mail.mass_mailing.contact'].create({ 'name': 'Contact 1', 'email': 'email1@test.com', + 'list_ids': [(6, 0, [cls.list.id])] }) def test_init_hook_list(self): @@ -32,14 +32,9 @@ class TestMassMailingUnique(common.SavepointCase): pre_init_hook(self.env.cr) def test_init_hook_contact(self): - # Disable temporarily the constraint - self.env.cr.execute(""" - ALTER TABLE mail_mass_mailing_contact - DROP CONSTRAINT mail_mass_mailing_contact_unique_mail_per_list - """) - self.env['mail.mass_mailing.contact'].create({ - 'name': 'Contact 2', - 'email': 'email1@test.com', - }) with self.assertRaises(exceptions.ValidationError): - pre_init_hook(self.env.cr) + self.env['mail.mass_mailing.contact'].create({ + 'name': 'Contact 2', + 'email': 'email1@test.com', + 'list_ids': [(6, 0, [self.list.id])] + })