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.

41 lines
1.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # See README.rst file on addon root folder for license details
  3. from openerp import models, fields, api, _
  4. from openerp.exceptions import ValidationError
  5. class ResPartner(models.Model):
  6. _inherit = 'res.partner'
  7. mass_mailing_contacts = fields.One2many(
  8. comodel_name='mail.mass_mailing.contact', inverse_name='partner_id')
  9. mass_mailing_contacts_count = fields.Integer(
  10. string='Mailing list number', compute='_count_mass_mailing_contacts',
  11. store=True)
  12. @api.one
  13. @api.constrains('email')
  14. def _check_email_mass_mailing_contacts(self):
  15. if self.mass_mailing_contacts and not self.email:
  16. raise ValidationError(
  17. _("This partner '%s' is subscribed to one or more "
  18. "mailing lists. Email must be assigned." % self.name))
  19. @api.one
  20. @api.depends('mass_mailing_contacts')
  21. def _count_mass_mailing_contacts(self):
  22. self.mass_mailing_contacts_count = len(self.mass_mailing_contacts)
  23. @api.multi
  24. def write(self, vals):
  25. res = super(ResPartner, self).write(vals)
  26. if vals.get('name') or vals.get('email'):
  27. mm_vals = {}
  28. if vals.get('name'):
  29. mm_vals['name'] = vals['name']
  30. if vals.get('email'):
  31. mm_vals['name'] = vals['email']
  32. self.mapped('mass_mailing_contacts').write(mm_vals)
  33. return res