mail_group_member.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import logging
  4. from odoo import api, fields, models
  5. from odoo.tools import email_normalize
  6. _logger = logging.getLogger(__name__)
  7. class MailGroupMember(models.Model):
  8. """Models a group member that can be either an email address either a full partner."""
  9. _name = 'mail.group.member'
  10. _description = 'Mailing List Member'
  11. _rec_name = 'email'
  12. email = fields.Char(string='Email', compute='_compute_email', readonly=False, store=True)
  13. email_normalized = fields.Char(
  14. string='Normalized Email', compute='_compute_email_normalized',
  15. index=True, store=True)
  16. mail_group_id = fields.Many2one('mail.group', string='Group', required=True, ondelete='cascade')
  17. partner_id = fields.Many2one('res.partner', 'Partner', ondelete='cascade')
  18. _sql_constraints = [(
  19. 'unique_partner',
  20. 'UNIQUE(partner_id, mail_group_id)',
  21. 'This partner is already subscribed to the group',
  22. )]
  23. @api.depends('partner_id.email')
  24. def _compute_email(self):
  25. for member in self:
  26. if member.partner_id:
  27. member.email = member.partner_id.email
  28. elif not member.email:
  29. member.email = False
  30. @api.depends('email')
  31. def _compute_email_normalized(self):
  32. for moderation in self:
  33. moderation.email_normalized = email_normalize(moderation.email)