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.

48 lines
1.8 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. if self.env.context.get('write_loop'):
  20. # We avoid with the context an infinite recursion calling write
  21. # method from other write method.
  22. return True
  23. partners = self.filtered(lambda r: not r.email_bounced)
  24. return partners.write({'email_bounced': True})
  25. def write(self, vals):
  26. [email_field] = self._primary_email
  27. if email_field not in vals:
  28. return super().write(vals)
  29. email = vals[email_field].lower() if vals[email_field] else False
  30. mte_obj = self.env['mail.tracking.email']
  31. vals['email_bounced'] = mte_obj.email_is_bounced(email)
  32. if vals['email_bounced']:
  33. res = mte_obj._email_last_tracking_state(email)
  34. tracking = mte_obj.browse(res[0].get('id'))
  35. event = tracking.tracking_event_ids[:1]
  36. self.with_context(
  37. write_loop=True,
  38. ).email_bounced_set(tracking, event.error_details, event)
  39. return super().write(vals)