Skip to content

Commit

Permalink
[MIG] mass_mailing_unique: Migration to 14.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantodorovich committed Sep 23, 2021
1 parent 2b628b5 commit 917c03b
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 181 deletions.
15 changes: 3 additions & 12 deletions mass_mailing_unique/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@
{
"name": "Unique records for mass mailing",
"summary": "Avoids duplicate mailing lists and contacts",
"version": "12.0.1.0.2",
"version": "14.0.1.0.0",
"category": "Marketing",
"website": "https://github.com/OCA/social",
"author": "Tecnativa, "
"Odoo Community Association (OCA)",
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": ["mass_mailing"],
"pre_init_hook": "pre_init_hook",
"images": [
"images/error-duplicated-email.png",
"images/error-duplicated-list.png",
],
"depends": [
"mass_mailing",
],
}
46 changes: 26 additions & 20 deletions mass_mailing_unique/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,47 @@
# Copyright 2016 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


from odoo import _
from odoo.exceptions import ValidationError


def pre_init_hook(cr):
"""Make sure there are no duplicates before installing the module.
If you define a unique key in Odoo that cannot be applied, Odoo will log a
If you define an unique key in Odoo that cannot be applied, Odoo will log a
warning and install the module without that constraint. Since this module
is useless without those constraints, we check here if all will work before
installing, and provide a user-friendly message in case of failure.
"""
errors = list()

# Search for duplicates in emails
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_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""")
cr.execute(
"""
SELECT email_normalized, COUNT(id) as count
FROM mailing_contact
GROUP BY email_normalized
HAVING COUNT(id) > 1
"""
)
for result in cr.fetchall():
errors.append("{0} appears {2} times in list {1}.".format(*result))

errors.append(
"There are {1} mailing contacts with the same email: {0}".format(*result)
)
# Search for duplicates in list's name
cr.execute("""SELECT name, COUNT(id)
FROM mail_mass_mailing_list
GROUP BY name
HAVING COUNT(id) > 1""")
cr.execute(
"""
SELECT name, COUNT(id) as count
FROM mailing_list
GROUP BY name
HAVING COUNT(id) > 1
"""
)
for result in cr.fetchall():
errors.append("There are {1} lists with name {0}.".format(*result))

errors.append(
"There are {1} mailing lists with the same name: {0}.".format(*result)
)
# Abort if duplicates are found
if errors:
raise ValidationError(
"Fix this before installing:" + "".join("\n" + e for e in errors))
_("Unable to install module mass_mailing_unique:\n%s", "\n".join(errors))
)
10 changes: 2 additions & 8 deletions mass_mailing_unique/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
# 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 . import mail_mass_mailing_contact
from . import mail_mass_mailing_list
from . import mail_mass_mailing_list_contact_rel
from . import mass_mailing
from . import mailing_contact
from . import mailing_list
31 changes: 0 additions & 31 deletions mass_mailing_unique/models/mail_mass_mailing_contact.py

This file was deleted.

12 changes: 0 additions & 12 deletions mass_mailing_unique/models/mail_mass_mailing_list.py

This file was deleted.

12 changes: 0 additions & 12 deletions mass_mailing_unique/models/mail_mass_mailing_list_contact_rel.py

This file was deleted.

17 changes: 17 additions & 0 deletions mass_mailing_unique/models/mailing_contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2018 Tecnativa - Ernesto Tejeda
# Copyright 2021 Camptocamp - Iván Todorovich
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import models


class MailingContact(models.Model):
_inherit = "mailing.contact"

_sql_constraints = [
(
"unique_email",
"UNIQUE(email_normalized)",
"There's already a contact with this email address",
)
]
18 changes: 18 additions & 0 deletions mass_mailing_unique/models/mailing_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
# Copyright 2016 Tecnativa - Vicent Cubells
# Copyright 2021 Camptocamp - Iván Todorovich
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import models


class MailingList(models.Model):
_inherit = "mailing.list"

_sql_constraints = [
(
"unique_name",
"UNIQUE(name)",
"Cannot have more than one lists with the same name.",
)
]
14 changes: 0 additions & 14 deletions mass_mailing_unique/models/mass_mailing.py

This file was deleted.

3 changes: 3 additions & 0 deletions mass_mailing_unique/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
* Vicent Cubells
* Pedro M. Baeza
* Ernesto Tejeda
* `Camptocamp <https://www.camptocamp.com>`_

* Iván Todorovich <[email protected]>
2 changes: 1 addition & 1 deletion mass_mailing_unique/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This module extends the functionality of mass mailing lists to disable
duplicate entries in list names and contact emails per list.
duplicate entries in list names and contact emails.

This way you will avoid conflicts when importing contacts to a list that has a
duplicated name.
2 changes: 1 addition & 1 deletion mass_mailing_unique/readme/INSTALL.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Before installing this module, you need to:

* Remove all duplicated list names.
* Remove all duplicated emails in each list.
* Remove all duplicated emails in mailing contacts.
2 changes: 0 additions & 2 deletions mass_mailing_unique/readme/USAGE.rst

This file was deleted.

Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 917c03b

Please sign in to comment.