Skip to content

Commit a62b26c

Browse files
AlvaroRM11Christian-RB
authored andcommitted
[IMP] document_page: extend follower to childs
1 parent 6bece12 commit a62b26c

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

document_page/models/document_page.py

+45
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,48 @@ 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+
res = super(DocumentPage, self).create(vals)
197+
198+
"""Automatically make the category followers follow the new page"""
199+
if res.type == "content" and res.parent_id and res.parent_id.type == "category":
200+
201+
category = res.parent_id
202+
category_followers = category.message_partner_ids.ids
203+
204+
if category_followers:
205+
res.message_subscribe(partner_ids=category_followers)
206+
207+
return res
208+
209+
def message_subscribe(self, partner_ids, subtype_ids=None):
210+
res = super(DocumentPage, self).message_subscribe(partner_ids, subtype_ids)
211+
self._toggle_follow_category_documents()
212+
213+
return res
214+
215+
def message_unsubscribe(self, partner_ids):
216+
res = super(DocumentPage, self).message_unsubscribe(partner_ids)
217+
self._toggle_follow_category_documents()
218+
219+
return res
220+
221+
def _toggle_follow_category_documents(self):
222+
"""Follow/unfollow all documents in a category based on the category's follower status."""
223+
for rec in self:
224+
if rec.type != "category":
225+
continue
226+
227+
related_docs = self.env['document.page'].search([
228+
('type', '=', 'content'),
229+
('parent_id', '=', rec.id)
230+
])
231+
232+
partner_ids = [self.env.user.partner_id.id]
233+
234+
if not rec.message_is_follower:
235+
related_docs.message_unsubscribe(partner_ids)
236+
else:
237+
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,77 @@
1+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
2+
from odoo.tests import common
3+
4+
class TestDocumentPageFollowers(common.TransactionCase):
5+
6+
def setUp(self):
7+
super().setUp()
8+
9+
self.user_1 = self.env["res.users"].create({
10+
"name": "Test User 1",
11+
"login": "test_user_1",
12+
"email": "user1@test.com",
13+
})
14+
15+
self.user_2 = self.env["res.users"].create({
16+
"name": "Test User 2",
17+
"login": "test_user_2",
18+
"email": "user2@test.com",
19+
})
20+
21+
self.category = self.env["document.page"].create({
22+
"name": "Test Category",
23+
"type": "category",
24+
})
25+
26+
self.content_page = self.env["document.page"].create({
27+
"name": "Test Content Page",
28+
"type": "content",
29+
"parent_id": self.category.id,
30+
})
31+
32+
def test_auto_subscribe_new_page(self):
33+
"""When creating a page in a category with followers, they are subscribed"""
34+
35+
self.category.message_subscribe(partner_ids=[
36+
self.user_1.partner_id.id,
37+
self.user_2.partner_id.id
38+
])
39+
40+
new_page = self.env["document.page"].create({
41+
"name": "New Auto-Subscribed Page",
42+
"type": "content",
43+
"parent_id": self.category.id,
44+
})
45+
46+
followers = new_page.message_partner_ids.ids
47+
48+
self.assertIn(self.user_1.partner_id.id, followers)
49+
self.assertIn(self.user_2.partner_id.id, followers)
50+
51+
def test_follow_category_subscribes_documents(self):
52+
"""Follow a category should subscribe to its existing pages"""
53+
self.category.message_subscribe([self.user_1.partner_id.id])
54+
55+
self.assertIn(
56+
self.user_1.partner_id.id,
57+
self.content_page.parent_id.message_partner_ids.ids
58+
)
59+
60+
def test_unfollow_category_unsubscribes_documents(self):
61+
"""Unfollowing a category should unsubscribe from its existing pages"""
62+
self.category.message_subscribe([self.user_1.partner_id.id])
63+
self.category.message_unsubscribe([self.user_1.partner_id.id])
64+
65+
self.assertNotIn(
66+
self.user_1.partner_id.id,
67+
self.content_page.parent_id.message_partner_ids.ids
68+
)
69+
70+
def test_no_auto_subscribe_without_category(self):
71+
"""Pages without a category should not have automatic subscriptions"""
72+
new_page = self.env["document.page"].create({
73+
"name": "Orphan Page",
74+
"type": "content"
75+
})
76+
77+
self.assertFalse(new_page.message_partner_ids)

0 commit comments

Comments
 (0)