From 74696cab830772a0072f51ca07ddecc9eaad0b76 Mon Sep 17 00:00:00 2001 From: Zelong Lin Date: Fri, 18 Feb 2022 12:23:13 +0000 Subject: [PATCH] [IMP] mail, hr: constraints when channel_type is not 'channel' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The group_public_id and group_id and subscription_department_ids should only be used when the channel_type is 'channel'. task-2766712 closes odoo/odoo#84813 Related: odoo/upgrade#3266 Signed-off-by: Sébastien Theys (seb) --- addons/hr/models/mail_channel.py | 9 ++++++++- addons/hr/views/mail_channel_views.xml | 1 + addons/mail/models/mail_channel.py | 20 +++++++++++++++++--- addons/mail/views/mail_channel_views.xml | 3 ++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/addons/hr/models/mail_channel.py b/addons/hr/models/mail_channel.py index 3d3b6e800a7..e043a941f7e 100644 --- a/addons/hr/models/mail_channel.py +++ b/addons/hr/models/mail_channel.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import fields, models +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError class Channel(models.Model): @@ -11,6 +12,12 @@ class Channel(models.Model): '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() diff --git a/addons/hr/views/mail_channel_views.xml b/addons/hr/views/mail_channel_views.xml index 25ad1350bc3..b8c6e5df7ea 100644 --- a/addons/hr/views/mail_channel_views.xml +++ b/addons/hr/views/mail_channel_views.xml @@ -7,6 +7,7 @@ diff --git a/addons/mail/models/mail_channel.py b/addons/mail/models/mail_channel.py index 9c53a7c4f56..e0ab0326e50 100644 --- a/addons/mail/models/mail_channel.py +++ b/addons/mail/models/mail_channel.py @@ -88,15 +88,17 @@ class Channel(models.Model): ('groups', 'Selected group of users')], string='Privacy', required=True, default='groups', help='This group is visible by non members. Invisible groups can add members through the invite button.') - group_public_id = fields.Many2one('res.groups', string='Authorized Group', - default=lambda self: self.env.ref('base.group_user')) + group_public_id = fields.Many2one('res.groups', string='Authorized Group', compute='_compute_group_public_id', readonly=False, store=True) _sql_constraints = [ ('channel_type_not_null', 'CHECK(channel_type IS NOT NULL)', 'The channel type cannot be empty'), ('uuid_unique', 'UNIQUE(uuid)', 'The channel UUID must be unique'), + ('group_public_id_check', + "CHECK (channel_type = 'channel' OR group_public_id IS NULL)", + 'Group authorization and group auto-subscription are only supported on channels.') ] - # CHAT CONSTRAINT + # CONSTRAINTS @api.constrains('channel_last_seen_partner_ids', 'channel_partner_ids') def _constraint_partners_chat(self): @@ -104,6 +106,12 @@ class Channel(models.Model): if len(ch.channel_last_seen_partner_ids) > 2 or len(ch.channel_partner_ids) > 2: raise ValidationError(_("A channel of type 'chat' cannot have more than two users.")) + @api.constrains('group_public_id', 'group_ids') + def _constraint_group_id_channel(self): + failing_channels = self.sudo().filtered(lambda channel: channel.channel_type != 'channel' and (channel.group_public_id or channel.group_ids)) + if failing_channels: + raise ValidationError(_("For %(channels)s, channel_type should be 'channel' to have the group-based authorization or group auto-subscription.", channels=', '.join([ch.name for ch in failing_channels]))) + # COMPUTE / INVERSE @api.depends('channel_type') @@ -169,6 +177,12 @@ class Channel(models.Model): for channel in self: channel.member_count = member_count_by_channel_id.get(channel.id, 0) + @api.depends('channel_type') + def _compute_group_public_id(self): + channels = self.filtered(lambda channel: channel.channel_type == 'channel') + channels.filtered(lambda channel: not channel.group_public_id).group_public_id = self.env.ref('base.group_user') + (self - channels).group_public_id = None + # ONCHANGE @api.onchange('public') diff --git a/addons/mail/views/mail_channel_views.xml b/addons/mail/views/mail_channel_views.xml index 9797c440da2..a9794d0bda7 100644 --- a/addons/mail/views/mail_channel_views.xml +++ b/addons/mail/views/mail_channel_views.xml @@ -73,9 +73,10 @@