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.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
  3. # © 2015 Antonio Espinosa <antonioea@antiun.com>
  4. # © 2015 Javier Iniesta <javieria@antiun.com>
  5. # © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  6. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  7. from openerp import models, fields, api, _
  8. from openerp.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.one
  26. @api.constrains('email')
  27. def _check_email_mass_mailing_contacts(self):
  28. if self.mass_mailing_contact_ids and not self.email:
  29. raise ValidationError(
  30. _("This partner '%s' is subscribed to one or more "
  31. "mailing lists. Email must be assigned." % self.name))
  32. @api.one
  33. @api.depends('mass_mailing_contact_ids',
  34. 'mass_mailing_contact_ids.opt_out')
  35. def _compute_mass_mailing_contacts_count(self):
  36. self.mass_mailing_contacts_count = len(self.mass_mailing_contact_ids)
  37. @api.one
  38. @api.depends('mass_mailing_stats')
  39. def _compute_mass_mailing_stats_count(self):
  40. self.mass_mailing_stats_count = len(self.mass_mailing_stats)
  41. @api.multi
  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