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.

74 lines
3.0 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.multi
  33. @api.depends('mass_mailing_contact_ids',
  34. 'mass_mailing_contact_ids.opt_out')
  35. def _compute_mass_mailing_contacts_count(self):
  36. contact_data = self.env['mail.mass_mailing.contact'].read_group(
  37. [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
  38. mapped_data = dict(
  39. [(contact['partner_id'][0], contact['partner_id_count'])
  40. for contact in contact_data])
  41. for partner in self:
  42. partner.mass_mailing_contacts_count = mapped_data.get(partner.id,
  43. 0)
  44. @api.multi
  45. @api.depends('mass_mailing_stats')
  46. def _compute_mass_mailing_stats_count(self):
  47. contact_data = self.env['mail.mail.statistics'].read_group(
  48. [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
  49. mapped_data = dict(
  50. [(contact['partner_id'][0], contact['partner_id_count'])
  51. for contact in contact_data])
  52. for partner in self:
  53. partner.mass_mailing_stats_count = mapped_data.get(partner.id, 0)
  54. @api.multi
  55. def write(self, vals):
  56. res = super(ResPartner, self).write(vals)
  57. if vals.get('name') or vals.get('email'):
  58. mm_vals = {}
  59. if vals.get('name'):
  60. mm_vals['name'] = vals['name']
  61. if vals.get('email'):
  62. mm_vals['name'] = vals['email']
  63. self.env["mail.mass_mailing.contact"].search([
  64. ("partner_id", "in", self.ids),
  65. ]).write(mm_vals)
  66. return res