Skip to content

Commit

Permalink
[MIG] mass_mailing_unique: Migration to 11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestotejeda committed Jun 20, 2018
1 parent fc23d65 commit 5682580
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 92 deletions.
66 changes: 1 addition & 65 deletions mass_mailing_unique/README.rst
Original file line number Diff line number Diff line change
@@ -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
<https://github.com/OCA/social/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 <jairo.llopis@tecnativa.com>
* Vicent Cubells <vicent.cubells@tecnativa.com>
* Pedro M. Baeza <pedro.baeza@tecnativa.com>

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.**
1 change: 0 additions & 1 deletion mass_mailing_unique/__init__.py
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
5 changes: 2 additions & 3 deletions mass_mailing_unique/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# -*- 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).

{
"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",
Expand Down
9 changes: 5 additions & 4 deletions mass_mailing_unique/hooks.py
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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(
Expand Down
1 change: 0 additions & 1 deletion mass_mailing_unique/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
19 changes: 13 additions & 6 deletions mass_mailing_unique/models/mass_mailing.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
6 changes: 6 additions & 0 deletions mass_mailing_unique/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* Jairo Llopis
* Vicent Cubells
* Pedro M. Baeza
* Ernesto Tejeda
6 changes: 6 additions & 0 deletions mass_mailing_unique/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions mass_mailing_unique/readme/INSTALL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Before installing this module, you need to:

* Remove all duplicated list names.
* Remove all duplicated emails in each list.
2 changes: 2 additions & 0 deletions mass_mailing_unique/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -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.
1 change: 0 additions & 1 deletion mass_mailing_unique/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 6 additions & 11 deletions mass_mailing_unique/tests/test_mass_mailing_unique.py
Original file line number Diff line number Diff line change
@@ -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).

Expand All @@ -17,6 +16,7 @@ def setUpClass(cls):
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):
Expand All @@ -32,14 +32,9 @@ def test_init_hook_list(self):
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])]
})

0 comments on commit 5682580

Please sign in to comment.