Skip to content

Commit

Permalink
[FIX] website_forum: fix tag posts count dependencies
Browse files Browse the repository at this point in the history
Post counts were not updated when a post was created or deleted
because of missing dependencies in the _compute.

Note that while "active_test" is included when counting without
specifying it, the field must be explicitly added to the `_compute`'s
dependency list to trigger it.

Task-3358143

closes odoo#125860

X-original-commit: f09291d
Signed-off-by: Stéphane Debauche (std) <std@odoo.com>
  • Loading branch information
flch-odoo committed Jun 21, 2023
1 parent 66527c2 commit 16103b0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions addons/website_forum/models/forum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,10 +1153,10 @@ class Tags(models.Model):
('name_uniq', 'unique (name, forum_id)', "Tag name already exists !"),
]

@api.depends("post_ids.tag_ids", "post_ids.state")
@api.depends("post_ids", "post_ids.tag_ids", "post_ids.state", "post_ids.active")
def _get_posts_count(self):
for tag in self:
tag.posts_count = len(tag.post_ids)
tag.posts_count = len(tag.post_ids) # state filter is in field domain

@api.model
def create(self, vals):
Expand Down
1 change: 1 addition & 0 deletions addons/website_forum/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from . import common
from . import test_forum
from . import test_forum_process
from . import test_forum_tag
35 changes: 35 additions & 0 deletions addons/website_forum/tests/test_forum_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo.addons.website_forum.tests.common import TestForumCommon


class TestForumTag(TestForumCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env['forum.tag'].search([]).unlink()
cls.tags = cls.env['forum.tag'].create(
[{'forum_id': cls.forum.id, 'name': f'Test Tag {tag_idx}'} for tag_idx in range(1, 3)]
)
cls.env['forum.post'].create(
[{'name': 'Posting about tag 1', 'forum_id': cls.forum.id, 'tag_ids': [[6, 0, [cls.tags[0].id]]]}]
)

def _check_tags_post_counts(self, tags, expected_counts):
self.assertEqual(tags.mapped('posts_count'), expected_counts)

def test_tag_posts_count(self):
self._check_tags_post_counts(self.tags, [1, 0])
post_tag_1 = self.env['forum.post'].create(
[{'name': 'Posting about tag 1 again', 'forum_id': self.forum.id, 'tag_ids': [[6, 0, [self.tags[0].id]]]}]
)
self._check_tags_post_counts(self.tags, [2, 0])
post_tags = self.env['forum.post'].create(
[{'name': 'Posting about tag 2 now', 'forum_id': self.forum.id, 'tag_ids': [[6, 0, self.tags.ids]]}]
)
self._check_tags_post_counts(self.tags, [3, 1])
post_tag_1.active = False
self._check_tags_post_counts(self.tags, [2, 1])
post_tags.close(None)
self._check_tags_post_counts(self.tags, [1, 0])

0 comments on commit 16103b0

Please sign in to comment.