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.

42 lines
1.7 KiB

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