2016-06-14 17:22:17 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
2016-10-14 21:53:04 +02:00
|
|
|
from odoo import models, api, fields
|
2016-06-14 17:22:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ResPartner(models.Model):
|
|
|
|
_inherit = 'res.partner'
|
|
|
|
|
2016-10-14 15:42:29 +02:00
|
|
|
# tracking_emails_count and email_score are non-store fields in order
|
|
|
|
# to improve performance
|
|
|
|
# email_bounced is store=True and index=True field in order to filter
|
|
|
|
# in tree view for processing bounces easier
|
2016-06-14 17:22:17 +02:00
|
|
|
tracking_emails_count = fields.Integer(
|
2016-10-14 15:42:29 +02:00
|
|
|
compute='_compute_tracking_emails_count', readonly=True)
|
|
|
|
email_bounced = fields.Boolean(index=True)
|
|
|
|
email_score = fields.Float(compute='_compute_email_score', readonly=True)
|
|
|
|
|
|
|
|
@api.depends('email')
|
|
|
|
def _compute_email_score(self):
|
|
|
|
for partner in self.filtered('email'):
|
|
|
|
partner.email_score = self.env['mail.tracking.email'].\
|
|
|
|
email_score_from_email(partner.email)
|
2016-06-14 17:22:17 +02:00
|
|
|
|
2016-10-14 15:42:29 +02:00
|
|
|
@api.multi
|
|
|
|
@api.depends('email')
|
2016-06-14 17:22:17 +02:00
|
|
|
def _compute_tracking_emails_count(self):
|
2016-10-14 21:53:04 +02:00
|
|
|
for partner in self:
|
|
|
|
partner.tracking_emails_count = self.env['mail.tracking.email'].\
|
|
|
|
search_count([
|
|
|
|
('recipient_address', '=ilike', partner.email)
|
|
|
|
])
|
2016-06-14 17:22:17 +02:00
|
|
|
|
2016-10-14 15:42:29 +02:00
|
|
|
@api.multi
|
|
|
|
def email_bounced_set(self, tracking_email, reason):
|
|
|
|
"""Inherit this method to make any other actions to partners"""
|
|
|
|
partners = self.filtered(lambda r: not r.email_bounced)
|
|
|
|
return partners.write({'email_bounced': True})
|
|
|
|
|
2016-06-14 17:22:17 +02:00
|
|
|
def write(self, vals):
|
|
|
|
email = vals.get('email')
|
|
|
|
if email is not None:
|
2016-10-14 15:42:29 +02:00
|
|
|
vals['email_bounced'] = (
|
|
|
|
bool(email) and
|
|
|
|
self.env['mail.tracking.email'].email_is_bounced(email))
|
2016-06-14 17:22:17 +02:00
|
|
|
return super(ResPartner, self).write(vals)
|