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.

44 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 odoo 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. def email_score_calculate(self):
  16. # This is not a compute method because is causing a inter-block
  17. # in mail_tracking_email PostgreSQL table
  18. # We suspect that tracking_email write to state field block that
  19. # table and then inside write ORM try to read from DB
  20. # tracking_email_ids because it's not in cache.
  21. # PostgreSQL blocks read because we have not committed yet the write
  22. for partner in self:
  23. partner.email_score = partner.tracking_email_ids.email_score()
  24. @api.depends('tracking_email_ids')
  25. def _compute_tracking_emails_count(self):
  26. for partner in self:
  27. partner.tracking_emails_count = self.env['mail.tracking.email'].\
  28. search_count([
  29. ('recipient_address', '=ilike', partner.email)
  30. ])
  31. def write(self, vals):
  32. email = vals.get('email')
  33. if email is not None:
  34. m_track = self.env['mail.tracking.email']
  35. vals['tracking_email_ids'] = m_track._tracking_ids_to_write(email)
  36. vals['email_score'] = m_track.email_score_from_email(email)
  37. return super(ResPartner, self).write(vals)