Files
Sébastien Theys 90cb44e1e1 [REF] mail, *: rename mail.channel to discuss.channel
* = bus, calendar, crm_livechat, hr, hr_holidays, im_livechat, mail_bot,
    mass_mailing, privacy_lookup, test_discuss_full, test_mail,
    test_mail_full, website_crm_livechat, website_livechat, base

In preparation of splitting discuss and mail modules.

Part of task-3265211

closes odoo/odoo#118354

Related: odoo/upgrade#4553
Related: odoo/enterprise#39661
Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
2023-04-21 02:21:53 +02:00

36 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class Channel(models.Model):
_inherit = 'discuss.channel'
subscription_department_ids = fields.Many2many(
'hr.department', string='HR Departments',
help='Automatically subscribe members of those departments to the channel.')
@api.constrains('subscription_department_ids')
def _constraint_subscription_department_ids_channel(self):
failing_channels = self.sudo().filtered(lambda channel: channel.channel_type != 'channel' and channel.subscription_department_ids)
if failing_channels:
raise ValidationError(_("For %(channels)s, channel_type should be 'channel' to have the department auto-subscription.", channels=', '.join([ch.name for ch in failing_channels])))
def _subscribe_users_automatically_get_members(self):
""" Auto-subscribe members of a department to a channel """
new_members = super(Channel, self)._subscribe_users_automatically_get_members()
for channel in self:
new_members[channel.id] = list(
set(new_members[channel.id]) |
set((channel.subscription_department_ids.member_ids.user_id.partner_id.filtered(lambda p: p.active) - channel.channel_partner_ids).ids)
)
return new_members
def write(self, vals):
res = super(Channel, self).write(vals)
if vals.get('subscription_department_ids'):
self._subscribe_users_automatically()
return res