You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
  3. # Copyright 2016 Tecnativa - Vicent Cubells
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp import _
  6. from openerp.exceptions import ValidationError
  7. def pre_init_hook(cr):
  8. """Make sure there are no duplicates before installing the module.
  9. If you define a unique key in Odoo that cannot be applied, Odoo will log a
  10. warning and install the module without that constraint. Since this module
  11. is useless without those constraints, we check here if all will work before
  12. installing, and provide a user-friendly message in case of failure.
  13. """
  14. errors = list()
  15. # Search for duplicates in emails
  16. cr.execute("""SELECT c.email, l.name, COUNT(c.id)
  17. FROM
  18. mail_mass_mailing_contact AS c
  19. INNER JOIN mail_mass_mailing_list AS l ON c.list_id = l.id
  20. GROUP BY l.name, l.id, c.email
  21. HAVING COUNT(c.id) > 1""")
  22. for result in cr.fetchall():
  23. errors.append(
  24. _("{0} appears {2} times in list {1}.").format(*result))
  25. # Search for duplicates in list's name
  26. cr.execute("""SELECT name, COUNT(id)
  27. FROM mail_mass_mailing_list
  28. GROUP BY name
  29. HAVING COUNT(id) > 1""")
  30. for result in cr.fetchall():
  31. errors.append(
  32. _("There are {1} lists with name {0}.").format(*result))
  33. # Abort if duplicates are found
  34. if errors:
  35. raise ValidationError(
  36. _("Fix this before installing:") +
  37. "".join("\n" + e for e in errors))