2015-08-28 11:15:50 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-06-14 20:47:41 +02:00
|
|
|
# Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
|
|
|
|
# Copyright 2015 Antonio Espinosa <antonio.espinosa@tecnativa.com>
|
|
|
|
# Copyright 2015 Javier Iniesta <javieria@antiun.com>
|
|
|
|
# Copyright 2017 David Vidal <david.vidal@tecnativa.com>
|
2016-08-08 13:14:37 +02:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2017-06-14 20:47:41 +02:00
|
|
|
from odoo import _, api, fields, models
|
|
|
|
from odoo.exceptions import ValidationError
|
2015-08-28 11:15:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ResPartner(models.Model):
|
|
|
|
_inherit = 'res.partner'
|
|
|
|
|
2016-09-12 08:43:32 +02:00
|
|
|
mass_mailing_contact_ids = fields.One2many(
|
2016-08-08 13:14:37 +02:00
|
|
|
string="Mailing lists",
|
2016-09-12 08:43:32 +02:00
|
|
|
oldname="mass_mailing_contacts",
|
|
|
|
domain=[('opt_out', '=', False)],
|
2015-08-28 11:15:50 +02:00
|
|
|
comodel_name='mail.mass_mailing.contact', inverse_name='partner_id')
|
|
|
|
mass_mailing_contacts_count = fields.Integer(
|
2016-08-08 13:14:37 +02:00
|
|
|
string='Mailing list number',
|
2017-06-14 20:47:41 +02:00
|
|
|
compute='_compute_mass_mailing_contacts_count', store=True,
|
|
|
|
compute_sudo=True)
|
2016-08-08 13:14:37 +02:00
|
|
|
mass_mailing_stats = fields.One2many(
|
|
|
|
string="Mass mailing stats",
|
|
|
|
comodel_name='mail.mail.statistics', inverse_name='partner_id')
|
|
|
|
mass_mailing_stats_count = fields.Integer(
|
|
|
|
string='Mass mailing stats number',
|
|
|
|
compute='_compute_mass_mailing_stats_count', store=True)
|
2015-08-28 11:15:50 +02:00
|
|
|
|
|
|
|
@api.constrains('email')
|
|
|
|
def _check_email_mass_mailing_contacts(self):
|
2017-06-14 20:47:41 +02:00
|
|
|
for partner in self:
|
|
|
|
if partner.sudo().mass_mailing_contact_ids and not partner.email:
|
|
|
|
raise ValidationError(
|
|
|
|
_("This partner '%s' is subscribed to one or more "
|
|
|
|
"mailing lists. Email must be assigned.") % partner.name)
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2016-09-12 08:43:32 +02:00
|
|
|
@api.depends('mass_mailing_contact_ids',
|
|
|
|
'mass_mailing_contact_ids.opt_out')
|
2016-08-08 13:14:37 +02:00
|
|
|
def _compute_mass_mailing_contacts_count(self):
|
2017-06-14 20:47:41 +02:00
|
|
|
for reg in self:
|
|
|
|
reg.mass_mailing_contacts_count = len(
|
|
|
|
reg.mass_mailing_contact_ids)
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2016-08-08 13:14:37 +02:00
|
|
|
@api.depends('mass_mailing_stats')
|
|
|
|
def _compute_mass_mailing_stats_count(self):
|
2017-06-14 20:47:41 +02:00
|
|
|
for reg in self:
|
|
|
|
reg.mass_mailing_stats_count = len(reg.mass_mailing_stats)
|
2016-08-08 13:14:37 +02:00
|
|
|
|
2015-08-28 11:15:50 +02:00
|
|
|
def write(self, vals):
|
|
|
|
res = super(ResPartner, self).write(vals)
|
|
|
|
if vals.get('name') or vals.get('email'):
|
|
|
|
mm_vals = {}
|
|
|
|
if vals.get('name'):
|
|
|
|
mm_vals['name'] = vals['name']
|
|
|
|
if vals.get('email'):
|
|
|
|
mm_vals['name'] = vals['email']
|
2016-09-12 08:43:32 +02:00
|
|
|
self.env["mail.mass_mailing.contact"].search([
|
|
|
|
("partner_id", "in", self.ids),
|
|
|
|
]).write(mm_vals)
|
2015-08-28 11:15:50 +02:00
|
|
|
return res
|