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.

43 lines
1.6 KiB

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