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.

61 lines
2.4 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. mass_mailing_stats = fields.One2many(
  20. string="Mass mailing stats",
  21. comodel_name='mail.mail.statistics', inverse_name='partner_id')
  22. mass_mailing_stats_count = fields.Integer(
  23. string='Mass mailing stats number',
  24. compute='_compute_mass_mailing_stats_count', store=True)
  25. @api.constrains('email')
  26. def _check_email_mass_mailing_contacts(self):
  27. for partner in self:
  28. if partner.mass_mailing_contact_ids and not partner.email:
  29. raise ValidationError(
  30. _("This partner '%s' is subscribed to one or more "
  31. "mailing lists. Email must be assigned.") % partner.name)
  32. @api.depends('mass_mailing_contact_ids',
  33. 'mass_mailing_contact_ids.opt_out')
  34. def _compute_mass_mailing_contacts_count(self):
  35. for reg in self:
  36. reg.mass_mailing_contacts_count = len(
  37. reg.mass_mailing_contact_ids)
  38. @api.depends('mass_mailing_stats')
  39. def _compute_mass_mailing_stats_count(self):
  40. for reg in self:
  41. reg.mass_mailing_stats_count = len(reg.mass_mailing_stats)
  42. def write(self, vals):
  43. res = super(ResPartner, self).write(vals)
  44. if vals.get('name') or vals.get('email'):
  45. mm_vals = {}
  46. if vals.get('name'):
  47. mm_vals['name'] = vals['name']
  48. if vals.get('email'):
  49. mm_vals['name'] = vals['email']
  50. self.env["mail.mass_mailing.contact"].search([
  51. ("partner_id", "in", self.ids),
  52. ]).write(mm_vals)
  53. return res