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.

46 lines
1.8 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_email_ids = fields.Many2many(
  8. string="Tracking emails", comodel_name="mail.tracking.email",
  9. readonly=True)
  10. tracking_emails_count = fields.Integer(
  11. string="Tracking emails count", store=True, readonly=True,
  12. compute="_compute_tracking_emails_count")
  13. email_score = fields.Float(
  14. string="Email score", readonly=True, default=50.0)
  15. @api.multi
  16. def email_score_calculate(self):
  17. # This is not a compute method because is causing a inter-block
  18. # in mail_tracking_email PostgreSQL table
  19. # We suspect that tracking_email write to state field block that
  20. # table and then inside write ORM try to read from DB
  21. # tracking_email_ids because it's not in cache.
  22. # PostgreSQL blocks read because we have not committed yet the write
  23. for partner in self:
  24. partner.email_score = partner.tracking_email_ids.email_score()
  25. @api.one
  26. @api.depends('tracking_email_ids')
  27. def _compute_tracking_emails_count(self):
  28. self.tracking_emails_count = self.env['mail.tracking.email'].\
  29. search_count([
  30. ('recipient_address', '=ilike', self.email)
  31. ])
  32. @api.multi
  33. def write(self, vals):
  34. email = vals.get('email')
  35. if email is not None:
  36. m_track = self.env['mail.tracking.email']
  37. vals['tracking_email_ids'] = m_track._tracking_ids_to_write(email)
  38. vals['email_score'] = m_track.email_score_from_email(email)
  39. return super(ResPartner, self).write(vals)