[IMP] mail, hr: constraints when channel_type is not 'channel'

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) <seb@odoo.com>
This commit is contained in:
Zelong Lin
2022-03-29 11:59:45 +02:00
parent 739ab40e38
commit 74696cab83
4 changed files with 28 additions and 5 deletions
+8 -1
View File
@@ -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()
+1
View File
@@ -7,6 +7,7 @@
<field name="arch" type="xml">
<xpath expr="//field[@name='group_ids']" position="after">
<field name="subscription_department_ids" widget="many2many_tags"
attrs="{'invisible': [('channel_type', '!=', 'channel')]}"
string="Auto Subscribe Departments"/>
</xpath>
</field>
+17 -3
View File
@@ -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')
+2 -1
View File
@@ -73,9 +73,10 @@
<group class="o_label_nowrap">
<field name="public" widget="radio" string="Who can follow the group's activities?"/>
<field name="group_public_id"
attrs="{'invisible': [('public','!=','groups')], 'required': [('public','=','groups')]}"
attrs="{'invisible': ['|', ('channel_type', '!=', 'channel'), ('public','!=','groups')], 'required': [('public','=','groups')]}"
/>
<field name="group_ids" widget="many2many_tags"
attrs="{'invisible': [('channel_type', '!=', 'channel')]}"
string="Auto Subscribe Groups"/>
</group>
</page>