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.9 KiB

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