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.

75 lines
3.2 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. compute_sudo=True)
  20. mass_mailing_stats = fields.One2many(
  21. string="Mass mailing stats",
  22. comodel_name='mail.mail.statistics', inverse_name='partner_id')
  23. mass_mailing_stats_count = fields.Integer(
  24. string='Mass mailing stats number',
  25. compute='_compute_mass_mailing_stats_count', store=True)
  26. @api.constrains('email')
  27. def _check_email_mass_mailing_contacts(self):
  28. for partner in self:
  29. if partner.sudo().mass_mailing_contact_ids and not partner.email:
  30. raise ValidationError(
  31. _("This partner '%s' is subscribed to one or more "
  32. "mailing lists. Email must be assigned.") % partner.name)
  33. @api.multi
  34. @api.depends('mass_mailing_contact_ids',
  35. 'mass_mailing_contact_ids.opt_out')
  36. def _compute_mass_mailing_contacts_count(self):
  37. contact_data = self.env['mail.mass_mailing.contact'].read_group(
  38. [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
  39. mapped_data = dict(
  40. [(contact['partner_id'][0], contact['partner_id_count'])
  41. for contact in contact_data])
  42. for partner in self:
  43. partner.mass_mailing_contacts_count = mapped_data.get(partner.id,
  44. 0)
  45. @api.multi
  46. @api.depends('mass_mailing_stats')
  47. def _compute_mass_mailing_stats_count(self):
  48. contact_data = self.env['mail.mail.statistics'].read_group(
  49. [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
  50. mapped_data = dict(
  51. [(contact['partner_id'][0], contact['partner_id_count'])
  52. for contact in contact_data])
  53. for partner in self:
  54. partner.mass_mailing_stats_count = mapped_data.get(partner.id, 0)
  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['email'] = vals['email']
  63. # Using sudo because ACLs shouldn't produce data inconsistency
  64. self.env["mail.mass_mailing.contact"].sudo().search([
  65. ("partner_id", "in", self.ids),
  66. ]).write(mm_vals)
  67. return res