[FIX] mail: correctly update new and removed subtypes when updating subscription

Currently, the subtype checked selection is not saved when the user adds and
removes subtypes. Indeed either an addition is performed, either a removal
but having both is not correctly taken into account when updating an existing
subscription.

How to reproduce
  * click on the pencil icon to edit the subscription of the follower;
  * as an example default "Discussions" will be the only one selected;
  * check "Notes" and save;
  * -> this works;
  * click again on the pencil icon and uncheck "Notes" and check "Activities"
    then save;
  * -> this does not work as only removal is performed (Activities is not
    checked);

The problem only occurs when you unchecked subtypes and checked other ones.

This commit fixes that behavior by taking into consideration both new
subtypes and subtypes to remove.

LINKS

Task ID-2205643
Task ID-2241688
COM PR #56775

X-Original-Commit odoo/odoo@d16b036638

closes odoo/odoo#65605

X-original-commit: 18cb4f6d8120a30a7f5d05eef107ac1734dd1431
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Co-authored-by: Thibault Delavallee <tde@odoo.com>
Co-authored-by: Shahista Pathan <sat@odoo.com>
This commit is contained in:
sahista pathan
2021-02-05 12:06:10 +00:00
committed by Thibault Delavallée
co-authored by Thibault Delavallee
parent f001e026a2
commit 7bd984dacc
2 changed files with 31 additions and 7 deletions
+12 -5
View File
@@ -366,11 +366,15 @@ GROUP BY fol.id%s%s""" % (
elif existing_policy in ('replace', 'update'):
fol_id, sids = next(((key, val[3]) for key, val in data_fols.items() if val[0] == res_id and val[1] == partner_id), (False, []))
new_sids = set(partner_subtypes[partner_id]) - set(sids)
old_sids = set(sids if sids[0] is not None else []) - set(partner_subtypes[partner_id])
old_sids = set(sids) - set(partner_subtypes[partner_id])
update_cmd = []
if fol_id and new_sids:
update[fol_id] = {'subtype_ids': [Command.link(sid) for sid in new_sids]}
update_cmd += [Command.link(sid) for sid in new_sids]
if fol_id and old_sids and existing_policy == 'replace':
update[fol_id] = {'subtype_ids': [Command.unlink(sid) for sid in old_sids]}
update_cmd += [Command.unlink(sid) for sid in old_sids]
if update_cmd:
update[fol_id] = {'subtype_ids': update_cmd}
for channel_id in set(channel_ids or []):
if channel_id not in doc_cids[res_id]:
new.setdefault(res_id, list()).append({
@@ -382,9 +386,12 @@ GROUP BY fol.id%s%s""" % (
fol_id, sids = next(((key, val[3]) for key, val in data_fols.items() if val[0] == res_id and val[2] == channel_id), (False, []))
new_sids = set(channel_subtypes[channel_id]) - set(sids)
old_sids = set(sids) - set(channel_subtypes[channel_id])
update_cmd = []
if fol_id and new_sids:
update[fol_id] = {'subtype_ids': [Command.link(sid) for sid in new_sids]}
update_cmd += [Command.link(sid) for sid in new_sids]
if fol_id and old_sids and existing_policy == 'replace':
update[fol_id] = {'subtype_ids': [Command.unlink(sid) for sid in old_sids]}
update_cmd += [Command.unlink(sid) for sid in old_sids]
if update_cmd:
update[fol_id] = {'subtype_ids': update_cmd}
return new, update
+19 -2
View File
@@ -3,10 +3,10 @@
from psycopg2 import IntegrityError
from odoo.tests import tagged, users
from odoo.addons.test_mail.tests.common import TestMailCommon
from odoo.tools.misc import mute_logger
from odoo.tests import tagged
from odoo.tests import users
from odoo.tools.misc import mute_logger
@tagged('mail_followers')
@@ -101,6 +101,7 @@ class BaseFollowersTest(TestMailCommon):
self.assertEqual(test_record.message_follower_ids.subtype_ids, self.mt_mg_nodef | self.mt_al_nodef)
def test_followers_multiple_subscription_noforce(self):
""" Calling message_subscribe without subtypes on an existing subscription should not do anything (default < existing) """
test_record = self.test_record.with_user(self.user_employee)
test_record.message_subscribe(partner_ids=[self.user_admin.partner_id.id], subtype_ids=[self.mt_mg_nodef.id, self.mt_al_nodef.id])
@@ -114,6 +115,22 @@ class BaseFollowersTest(TestMailCommon):
self.assertEqual(test_record.message_channel_ids, self.env['mail.channel'])
self.assertEqual(test_record.message_follower_ids.subtype_ids, self.mt_mg_nodef | self.mt_al_nodef)
def test_followers_multiple_subscription_update(self):
""" Calling message_subscribe with subtypes on an existing subscription should replace them (new > existing) """
test_record = self.test_record.with_user(self.user_employee)
test_record.message_subscribe(partner_ids=[self.user_employee.partner_id.id], subtype_ids=[self.mt_mg_def.id, self.mt_cl_def.id])
self.assertEqual(test_record.message_partner_ids, self.user_employee.partner_id)
follower = self.env['mail.followers'].search([
('res_model', '=', 'mail.test.simple'),
('res_id', '=', test_record.id),
('partner_id', '=', self.user_employee.partner_id.id)])
self.assertEqual(follower, test_record.message_follower_ids)
self.assertEqual(follower.subtype_ids, self.mt_mg_def | self.mt_cl_def)
# remove one subtype `mt_mg_def` and set new subtype `mt_al_def`
test_record.message_subscribe(partner_ids=[self.user_employee.partner_id.id], subtype_ids=[self.mt_cl_def.id, self.mt_al_def.id])
self.assertEqual(follower.subtype_ids, self.mt_cl_def | self.mt_al_def)
def test_followers_no_DID(self):
"""Test that a follower cannot suffer from dissociative identity disorder.
It cannot be both a partner and a channel.