Skip to content

Commit 5f39d02

Browse files
committed
Merge PR #522 into 16.0
Signed-off-by dreispt
2 parents da45c27 + 7455992 commit 5f39d02

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

document_page/models/document_page.py

+46
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,49 @@ def copy(self, default=None):
190190
draft_summary=_("summary"),
191191
)
192192
return super().copy(default=default)
193+
194+
@api.model
195+
def create(self, vals):
196+
"""Automatically make the category followers follow the new page"""
197+
res = super().create(vals)
198+
199+
if res.type == "content" and res.parent_id and res.parent_id.type == "category":
200+
category = res.parent_id
201+
category_followers = category.message_partner_ids.ids
202+
203+
if category_followers:
204+
res.message_subscribe(partner_ids=category_followers)
205+
206+
return res
207+
208+
def message_subscribe(self, partner_ids, subtype_ids=None):
209+
res = super().message_subscribe(partner_ids, subtype_ids)
210+
self._toggle_follow_category_documents()
211+
212+
return res
213+
214+
def message_unsubscribe(self, partner_ids):
215+
res = super().message_unsubscribe(partner_ids)
216+
self._toggle_follow_category_documents()
217+
218+
return res
219+
220+
def _toggle_follow_category_documents(self):
221+
"""
222+
Follow/unfollow all documents in a category
223+
based on the category's follower status.
224+
"""
225+
for rec in self:
226+
if rec.type != "category":
227+
continue
228+
229+
related_docs = self.env["document.page"].search(
230+
[("type", "=", "content"), ("parent_id", "=", rec.id)]
231+
)
232+
233+
partner_ids = [self.env.user.partner_id.id]
234+
235+
if not rec.message_is_follower:
236+
related_docs.message_unsubscribe(partner_ids)
237+
else:
238+
related_docs.message_subscribe(partner_ids)

document_page/tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
from . import test_document_page_create_menu
55
from . import test_document_page_history
66
from . import test_document_page_show_diff
7+
from . import test_document_page_followers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
2+
from odoo.tests import common
3+
4+
5+
class TestDocumentPageFollowers(common.TransactionCase):
6+
def setUp(self):
7+
super().setUp()
8+
9+
self.user_1 = self.env["res.users"].create(
10+
{
11+
"name": "Test User 1",
12+
"login": "test_user_1",
13+
"email": "user1@test.com",
14+
}
15+
)
16+
17+
self.user_2 = self.env["res.users"].create(
18+
{
19+
"name": "Test User 2",
20+
"login": "test_user_2",
21+
"email": "user2@test.com",
22+
}
23+
)
24+
25+
self.category = self.env["document.page"].create(
26+
{
27+
"name": "Test Category",
28+
"type": "category",
29+
}
30+
)
31+
32+
self.content_page = self.env["document.page"].create(
33+
{
34+
"name": "Test Content Page",
35+
"type": "content",
36+
"parent_id": self.category.id,
37+
}
38+
)
39+
40+
def test_auto_subscribe_new_page(self):
41+
"""When creating a page in a category with followers, they are subscribed"""
42+
43+
self.category.message_subscribe(
44+
partner_ids=[self.user_1.partner_id.id, self.user_2.partner_id.id]
45+
)
46+
47+
new_page = self.env["document.page"].create(
48+
{
49+
"name": "New Auto-Subscribed Page",
50+
"type": "content",
51+
"parent_id": self.category.id,
52+
}
53+
)
54+
55+
followers = new_page.message_partner_ids.ids
56+
57+
self.assertIn(self.user_1.partner_id.id, followers)
58+
self.assertIn(self.user_2.partner_id.id, followers)
59+
60+
def test_follow_category_subscribes_documents(self):
61+
"""Follow a category should subscribe to its existing pages"""
62+
self.category.message_subscribe([self.user_1.partner_id.id])
63+
64+
self.assertIn(
65+
self.user_1.partner_id.id,
66+
self.content_page.parent_id.message_partner_ids.ids,
67+
)
68+
69+
def test_unfollow_category_unsubscribes_documents(self):
70+
"""Unfollowing a category should unsubscribe from its existing pages"""
71+
self.category.message_subscribe([self.user_1.partner_id.id])
72+
self.category.message_unsubscribe([self.user_1.partner_id.id])
73+
74+
self.assertNotIn(
75+
self.user_1.partner_id.id,
76+
self.content_page.parent_id.message_partner_ids.ids,
77+
)
78+
79+
def test_no_auto_subscribe_without_category(self):
80+
"""Pages without a category should not have automatic subscriptions"""
81+
new_page = self.env["document.page"].create(
82+
{"name": "Orphan Page", "type": "content"}
83+
)
84+
85+
self.assertFalse(new_page.message_partner_ids)

0 commit comments

Comments
 (0)