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.

46 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 import _
  5. from odoo.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 LOWER(c.email) AS e, l.name, COUNT(c.id)
  16. FROM
  17. mail_mass_mailing_contact AS c
  18. INNER JOIN mail_mass_mailing_contact_list_rel AS cl
  19. ON cl.contact_id = c.id
  20. INNER JOIN mail_mass_mailing_list AS l ON cl.list_id = l.id
  21. GROUP BY l.name, e
  22. HAVING COUNT(c.id) > 1""")
  23. for result in cr.fetchall():
  24. errors.append(
  25. _("{0} appears {2} times in list {1}.").format(*result))
  26. # Search for duplicates in list's name
  27. cr.execute("""SELECT name, COUNT(id)
  28. FROM mail_mass_mailing_list
  29. GROUP BY name
  30. HAVING COUNT(id) > 1""")
  31. for result in cr.fetchall():
  32. errors.append(
  33. _("There are {1} lists with name {0}.").format(*result))
  34. # Abort if duplicates are found
  35. if errors:
  36. raise ValidationError(
  37. _("Fix this before installing:") +
  38. "".join("\n" + e for e in errors))