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. # Copyright 2018 Tecnativa - Ernesto Tejeda
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. import logging
  4. from odoo import api, fields, models
  5. _logger = logging.getLogger(__name__)
  6. class MailBouncedMixin(models.AbstractModel):
  7. """ A mixin class to use if you want to add is_bounced flag on a model.
  8. The field '_primary_email' must be overridden in the model that inherit
  9. the mixin and must contain the email field of the model.
  10. """
  11. _name = 'mail.bounced.mixin'
  12. _description = 'Mail bounced mixin'
  13. _primary_email = ['email']
  14. email_bounced = fields.Boolean(index=True)
  15. @api.multi
  16. def email_bounced_set(self, tracking_emails, reason, event=None):
  17. """Inherit this method to make any other actions to the model that
  18. inherit the mixin"""
  19. partners = self.filtered(lambda r: not r.email_bounced)
  20. return partners.write({'email_bounced': True})
  21. def write(self, vals):
  22. [email_field] = self._primary_email
  23. if email_field not in vals:
  24. return super(MailBouncedMixin, self).write(vals)
  25. email = vals[email_field].lower() if vals[email_field] else False
  26. mte_obj = self.env['mail.tracking.email']
  27. if not mte_obj.email_is_bounced(email):
  28. vals['email_bounced'] = False
  29. return super(MailBouncedMixin, self).write(vals)
  30. res = mte_obj._email_last_tracking_state(email)
  31. tracking = mte_obj.browse(res[0].get('id'))
  32. event = tracking.tracking_event_ids[:1]
  33. self.email_bounced_set(tracking, event.error_details, event)
  34. return super(MailBouncedMixin, self).write(vals)