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.7 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 AS list, COUNT(c.id) AS duplicates
  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.dictfetchall():
  22. errors.append(
  23. _("%(email)s appears %(duplicates)d times in list %(list)s") %
  24. result)
  25. # Search for duplicates in list's name
  26. cr.execute("""SELECT name, COUNT(id) as duplicates
  27. FROM mail_mass_mailing_list
  28. GROUP BY name
  29. HAVING COUNT(id) > 1""")
  30. for result in cr.dictfetchall():
  31. errors.append(
  32. _("there are %(duplicates)d lists with name %(name)s") % result)
  33. # Abort if duplicates are found
  34. if errors:
  35. raise ValidationError(
  36. _("Fix this before installing: %s.") % ", ".join(errors))