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.

63 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  3. # Copyright 2015 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  4. # Copyright 2015 Javier Iniesta <javieria@antiun.com>
  5. # Copyright 2017 David Vidal <david.vidal@tecnativa.com>
  6. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  7. from odoo import _, api, fields, models
  8. from odoo.exceptions import ValidationError
  9. class ResPartner(models.Model):
  10. _inherit = 'res.partner'
  11. mass_mailing_contact_ids = fields.One2many(
  12. string="Mailing lists",
  13. oldname="mass_mailing_contacts",
  14. domain=[('opt_out', '=', False)],
  15. comodel_name='mail.mass_mailing.contact', inverse_name='partner_id')
  16. mass_mailing_contacts_count = fields.Integer(
  17. string='Mailing list number',
  18. compute='_compute_mass_mailing_contacts_count', store=True,
  19. compute_sudo=True)
  20. mass_mailing_stats = fields.One2many(
  21. string="Mass mailing stats",
  22. comodel_name='mail.mail.statistics', inverse_name='partner_id')
  23. mass_mailing_stats_count = fields.Integer(
  24. string='Mass mailing stats number',
  25. compute='_compute_mass_mailing_stats_count', store=True)
  26. @api.constrains('email')
  27. def _check_email_mass_mailing_contacts(self):
  28. for partner in self:
  29. if partner.sudo().mass_mailing_contact_ids and not partner.email:
  30. raise ValidationError(
  31. _("This partner '%s' is subscribed to one or more "
  32. "mailing lists. Email must be assigned.") % partner.name)
  33. @api.depends('mass_mailing_contact_ids',
  34. 'mass_mailing_contact_ids.opt_out')
  35. def _compute_mass_mailing_contacts_count(self):
  36. for reg in self:
  37. reg.mass_mailing_contacts_count = len(
  38. reg.mass_mailing_contact_ids)
  39. @api.depends('mass_mailing_stats')
  40. def _compute_mass_mailing_stats_count(self):
  41. for reg in self:
  42. reg.mass_mailing_stats_count = len(reg.mass_mailing_stats)
  43. def write(self, vals):
  44. res = super(ResPartner, self).write(vals)
  45. if vals.get('name') or vals.get('email'):
  46. mm_vals = {}
  47. if vals.get('name'):
  48. mm_vals['name'] = vals['name']
  49. if vals.get('email'):
  50. mm_vals['email'] = vals['email']
  51. # Using sudo because ACLs shouldn't produce data inconsistency
  52. self.env["mail.mass_mailing.contact"].sudo().search([
  53. ("partner_id", "in", self.ids),
  54. ]).write(mm_vals)
  55. return res