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.

54 lines
2.2 KiB

  1. # Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import models, api, fields
  4. class ResPartner(models.Model):
  5. _inherit = 'res.partner'
  6. # tracking_emails_count and email_score are non-store fields in order
  7. # to improve performance
  8. # email_bounced is store=True and index=True field in order to filter
  9. # in tree view for processing bounces easier
  10. tracking_emails_count = fields.Integer(
  11. compute='_compute_tracking_emails_count', readonly=True)
  12. email_bounced = fields.Boolean(index=True)
  13. email_score = fields.Float(compute='_compute_email_score', readonly=True)
  14. @api.depends('email')
  15. def _compute_email_score(self):
  16. for partner in self.filtered('email'):
  17. partner.email_score = self.env['mail.tracking.email'].\
  18. email_score_from_email(partner.email)
  19. @api.multi
  20. @api.depends('email')
  21. def _compute_tracking_emails_count(self):
  22. for partner in self:
  23. count = 0
  24. if partner.email:
  25. count = self.env['mail.tracking.email'].search_count([
  26. ('recipient_address', '=', partner.email.lower())
  27. ])
  28. partner.tracking_emails_count = count
  29. @api.multi
  30. def email_bounced_set(self, tracking_emails, reason, event=None):
  31. """Inherit this method to make any other actions to partners"""
  32. partners = self.filtered(lambda r: not r.email_bounced)
  33. return partners.write({'email_bounced': True})
  34. def write(self, vals):
  35. if 'email' not in vals:
  36. return super(ResPartner, self).write(vals)
  37. email = vals['email'].lower() if vals['email'] else False
  38. mte_obj = self.env['mail.tracking.email']
  39. if not mte_obj.email_is_bounced(email):
  40. vals['email_bounced'] = False
  41. return super(ResPartner, self).write(vals)
  42. res = mte_obj._email_last_tracking_state(email)
  43. tracking = mte_obj.browse(res[0].get('id'))
  44. event = tracking.tracking_event_ids[:1]
  45. self.email_bounced_set(tracking, event.error_details, event)
  46. return super(ResPartner, self).write(vals)