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.

56 lines
2.1 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_contacts = fields.One2many(
  12. string="Mailing lists",
  13. comodel_name='mail.mass_mailing.contact', inverse_name='partner_id')
  14. mass_mailing_contacts_count = fields.Integer(
  15. string='Mailing list number',
  16. compute='_compute_mass_mailing_contacts_count', store=True)
  17. mass_mailing_stats = fields.One2many(
  18. string="Mass mailing stats",
  19. comodel_name='mail.mail.statistics', inverse_name='partner_id')
  20. mass_mailing_stats_count = fields.Integer(
  21. string='Mass mailing stats number',
  22. compute='_compute_mass_mailing_stats_count', store=True)
  23. @api.one
  24. @api.constrains('email')
  25. def _check_email_mass_mailing_contacts(self):
  26. if self.mass_mailing_contacts and not self.email:
  27. raise ValidationError(
  28. _("This partner '%s' is subscribed to one or more "
  29. "mailing lists. Email must be assigned." % self.name))
  30. @api.one
  31. @api.depends('mass_mailing_contacts')
  32. def _compute_mass_mailing_contacts_count(self):
  33. self.mass_mailing_contacts_count = len(self.mass_mailing_contacts)
  34. @api.one
  35. @api.depends('mass_mailing_stats')
  36. def _compute_mass_mailing_stats_count(self):
  37. self.mass_mailing_stats_count = len(self.mass_mailing_stats)
  38. @api.multi
  39. def write(self, vals):
  40. res = super(ResPartner, self).write(vals)
  41. if vals.get('name') or vals.get('email'):
  42. mm_vals = {}
  43. if vals.get('name'):
  44. mm_vals['name'] = vals['name']
  45. if vals.get('email'):
  46. mm_vals['name'] = vals['email']
  47. self.mapped('mass_mailing_contacts').write(mm_vals)
  48. return res