From 92c087e9d981a49f42409f53443ed7da3712ee6f Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Fri, 29 Jan 2016 11:35:07 +0100 Subject: [PATCH] Follow guidelines in hooks, remove redundant credits. --- mass_mailing_unique/README.rst | 5 ---- mass_mailing_unique/__init__.py | 40 +-------------------------- mass_mailing_unique/__openerp__.py | 2 +- mass_mailing_unique/hooks.py | 43 ++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 45 deletions(-) create mode 100644 mass_mailing_unique/hooks.py diff --git a/mass_mailing_unique/README.rst b/mass_mailing_unique/README.rst index c97cabcf..ea59209d 100644 --- a/mass_mailing_unique/README.rst +++ b/mass_mailing_unique/README.rst @@ -46,11 +46,6 @@ mass_mailing_unique%0Aversion:%20 Credits ======= -Images ------- - -* Odoo Community Association: `Icon `_. - Contributors ------------ diff --git a/mass_mailing_unique/__init__.py b/mass_mailing_unique/__init__.py index b434bd97..baa4dd41 100644 --- a/mass_mailing_unique/__init__.py +++ b/mass_mailing_unique/__init__.py @@ -2,43 +2,5 @@ # © 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from openerp import _ -from openerp.exceptions import ValidationError from . import models - - -def require_no_duplicates(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 - 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 c.email, l.name AS list, COUNT(c.id) AS duplicates - 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 - HAVING COUNT(c.id) > 1""") - for result in cr.dictfetchall(): - errors.append( - _("%(email)s appears %(duplicates)d times in list %(list)s") % - result) - - # Search for duplicates in list's name - cr.execute("""SELECT name, COUNT(id) as duplicates - FROM mail_mass_mailing_list - GROUP BY name - HAVING COUNT(id) > 1""") - for result in cr.dictfetchall(): - errors.append( - _("there are %(duplicates)d lists with name %(name)s") % result) - - # Abort if duplicates are found - if errors: - raise ValidationError( - _("Fix this before installing: %s.") % ", ".join(errors)) +from .hooks import pre_init_hook diff --git a/mass_mailing_unique/__openerp__.py b/mass_mailing_unique/__openerp__.py index 30b87728..5b7917d5 100644 --- a/mass_mailing_unique/__openerp__.py +++ b/mass_mailing_unique/__openerp__.py @@ -12,7 +12,7 @@ "license": "AGPL-3", "application": False, "installable": True, - "pre_init_hook": "require_no_duplicates", + "pre_init_hook": "pre_init_hook", "images": [ "images/error-duplicated-email.png", "images/error-duplicated-list.png", diff --git a/mass_mailing_unique/hooks.py b/mass_mailing_unique/hooks.py new file mode 100644 index 00000000..7c8de0c9 --- /dev/null +++ b/mass_mailing_unique/hooks.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# © 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp import _ +from openerp.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 + 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 c.email, l.name AS list, COUNT(c.id) AS duplicates + 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 + HAVING COUNT(c.id) > 1""") + for result in cr.dictfetchall(): + errors.append( + _("%(email)s appears %(duplicates)d times in list %(list)s") % + result) + + # Search for duplicates in list's name + cr.execute("""SELECT name, COUNT(id) as duplicates + FROM mail_mass_mailing_list + GROUP BY name + HAVING COUNT(id) > 1""") + for result in cr.dictfetchall(): + errors.append( + _("there are %(duplicates)d lists with name %(name)s") % result) + + # Abort if duplicates are found + if errors: + raise ValidationError( + _("Fix this before installing: %s.") % ", ".join(errors))