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.

52 lines
1.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import copy
  5. from odoo import fields, models
  6. def event_filtered_ids(model, mass_mailing_id, domain, field='email'):
  7. field = field or 'email'
  8. domain = domain or []
  9. exclude_emails = []
  10. mass_mailing = model.env['mail.mass_mailing'].browse(mass_mailing_id)
  11. if mass_mailing.event_id:
  12. exclude = mass_mailing.exclude_event_state_ids.mapped('code')
  13. reg_domain = False
  14. registrations = model.env['event.registration']
  15. if exclude:
  16. reg_domain = [
  17. ('event_id', '=', mass_mailing.event_id.id),
  18. ('state', 'in', exclude)
  19. ]
  20. if reg_domain:
  21. registrations = registrations.search(reg_domain)
  22. if registrations:
  23. exclude_emails = registrations.mapped('email')
  24. apply_domain = copy.deepcopy(domain)
  25. if exclude_emails:
  26. apply_domain.append((field, 'not in', exclude_emails))
  27. rows = model.search(apply_domain)
  28. return rows.ids
  29. class MailMassMailing(models.Model):
  30. _inherit = 'mail.mass_mailing'
  31. def _default_exclude_event_state_ids(self):
  32. return self.env['event.registration.state'].search([])
  33. event_id = fields.Many2one(
  34. string="Event related", comodel_name='event.event')
  35. exclude_event_state_ids = fields.Many2many(
  36. comodel_name='event.registration.state',
  37. string="Exclude", default=_default_exclude_event_state_ids)
  38. def get_recipients(self):
  39. res_ids = super(MailMassMailing, self).get_recipients()
  40. if res_ids:
  41. domain = [('id', 'in', res_ids)]
  42. res_ids = event_filtered_ids(
  43. self.env[self.mailing_model], self.id, domain, field='email')
  44. return res_ids