From e24058b14d0f990ff0601cbe8dc675e1ab1dd2df Mon Sep 17 00:00:00 2001 From: Pierre-Yves Dufays Date: Fri, 9 Jun 2023 09:36:10 +0200 Subject: [PATCH] [IMP] website_forum: display other user forum links in the sidebar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Links of the current website forums belonging to the user are now displayed in the sidebar. A forum is considered to "belongs" to a user if either he has posted a message on it or he has added one of the post in his favourites. Technical note: we add the value "my_other_forums" in _prepare_user_values method to display the other user forums of the website in the side menu on various pages. That value is only added when the "forum" value is a forum indicating that the page is currently displaying one of the forum (we use the test "forum and forum is not True" because forum parameter can take the value True, None or a recordset). We use a lazy value so that the query to select the forum is only executed in the page that uses it. Task-3354382 closes odoo/odoo#130469 Signed-off-by: Stéphane Debauche (std) --- .../controllers/website_forum.py | 15 +++++ addons/website_forum/tests/__init__.py | 1 + addons/website_forum/tests/common.py | 8 +++ .../tests/test_forum_controller.py | 63 +++++++++++++++++++ .../views/forum_forum_templates_layout.xml | 9 +++ 5 files changed, 96 insertions(+) create mode 100644 addons/website_forum/tests/test_forum_controller.py diff --git a/addons/website_forum/controllers/website_forum.py b/addons/website_forum/controllers/website_forum.py index 994939bbcc1..0ce1ff935ed 100644 --- a/addons/website_forum/controllers/website_forum.py +++ b/addons/website_forum/controllers/website_forum.py @@ -36,6 +36,21 @@ class WebsiteForum(WebsiteProfile): values['forum'] = kwargs.get('forum') elif kwargs.get('forum_id'): values['forum'] = request.env['forum.forum'].browse(int(kwargs.pop('forum_id'))) + forum = values.get('forum') + if forum and forum is not True and not request.env.user._is_public(): + def _get_my_other_forums(): + post_domain = expression.OR( + [[('create_uid', '=', request.uid)], + [('favourite_ids', '=', request.uid)]] + ) + return request.env['forum.forum'].search(expression.AND([ + request.website.website_domain(), + [('id', '!=', forum.id)], + [('post_ids', 'any', post_domain)] + ])) + values['my_other_forums'] = tools.lazy(_get_my_other_forums) + else: + values['my_other_forums'] = request.env['forum.forum'] return values def _prepare_mark_as_offensive_values(self, post, **kwargs): diff --git a/addons/website_forum/tests/__init__.py b/addons/website_forum/tests/__init__.py index 19b53cb2ec1..eb576b9e6b9 100644 --- a/addons/website_forum/tests/__init__.py +++ b/addons/website_forum/tests/__init__.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from . import common +from . import test_forum_controller from . import test_forum_internals from . import test_forum_karma_access from . import test_forum_tag diff --git a/addons/website_forum/tests/common.py b/addons/website_forum/tests/common.py index a53fba76075..268b13f136a 100644 --- a/addons/website_forum/tests/common.py +++ b/addons/website_forum/tests/common.py @@ -44,6 +44,13 @@ class TestForumCommon(common.TransactionCase): 'karma': 0, 'groups_id': [(6, 0, [group_employee_id])] }) + cls.user_employee_2 = cls.env['res.users'].create({ + 'name': 'Merlin Employee', + 'login': 'Merlin', + 'email': 'merlin.employee@example.com', + 'karma': KARMA['ask'], + 'groups_id': [(6, 0, [cls.env.ref('base.group_user').id])], + }) cls.user_portal = TestUsersEnv.create({ 'name': 'Beatrice Portal', 'login': 'Beatrice', @@ -58,6 +65,7 @@ class TestForumCommon(common.TransactionCase): 'karma': 0, 'groups_id': [(6, 0, [group_public_id])] }) + cls.user_admin = cls.env.ref('base.user_admin') # Test forum cls.forum = Forum.create({ diff --git a/addons/website_forum/tests/test_forum_controller.py b/addons/website_forum/tests/test_forum_controller.py new file mode 100644 index 00000000000..72a8ab56f77 --- /dev/null +++ b/addons/website_forum/tests/test_forum_controller.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.addons.website.tools import MockRequest +from odoo.addons.website_forum.controllers.website_forum import WebsiteForum +from odoo.addons.website_forum.tests.common import KARMA, TestForumCommon + + +class TestForumController(TestForumCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls._activate_multi_website() + cls.minimum_karma_allowing_to_post = KARMA['ask'] + cls.forums = cls.env['forum.forum'].create([{ + 'name': f'Forum {idx + 2}', + 'karma_ask': cls.minimum_karma_allowing_to_post, + 'website_id': website.id, + } for idx, website in enumerate( + (cls.base_website, cls.base_website, cls.base_website, cls.website_2, cls.website_2))]) + cls.forum_1, cls.forum_2, cls.forum_3, cls.forum_1_website_2, cls.forum_2_website_2 = cls.forums + cls.controller = WebsiteForum() + + def _get_my_other_forums(self, forum): + """ Get user other forums limited to the forums of the test (self.forums). """ + return self.forums & self.controller._prepare_user_values(forum=forum).get('my_other_forums') + + def forum_post(self, user, forum): + return self.env['forum.post'].with_user(user).create({ + 'content': 'A post ...', + 'forum_id': forum.id, + 'name': 'Post...', + }) + + def test_prepare_user_values_my_other_forum(self): + """ Test user other forums values (my_other_forums) in various contexts. """ + employee_2_forum_2_post = self.forum_post(self.user_employee_2, self.forum_2) + employee_2_website_2_forum_2_post = self.forum_post(self.user_employee_2, self.forum_2_website_2) + for user in (self.user_admin, self.user_employee, self.user_portal, self.user_public): + with self.with_user(user.login), MockRequest(self.env, website=self.base_website): + self.assertFalse(self._get_my_other_forums(self.forum_1)) + self.assertFalse(self._get_my_other_forums(None)) + self.assertFalse(self._get_my_other_forums(True)) + if user != self.user_public: + self.env.user.karma = self.minimum_karma_allowing_to_post + # Like a post on forum 2 and verify that forum 2 is now in "my other forum" + employee_2_forum_2_post.favourite_ids += self.env.user + self.assertEqual(self._get_my_other_forums(self.forum_1), self.forum_2) + self.assertFalse(self._get_my_other_forums(self.forum_2)) + # Check similarly with posting and also checking that we don't see forum of website 2 + self.forum_post(self.env.user, self.forum_3) + self.forum_post(self.env.user, self.forum_1_website_2) + self.assertEqual(self._get_my_other_forums(self.forum_1), self.forum_2 + self.forum_3) + self.assertEqual(self._get_my_other_forums(self.forum_2), self.forum_3) + self.assertEqual(self._get_my_other_forums(self.forum_3), self.forum_2) + with self.with_user(user.login), MockRequest(self.env, website=self.website_2): + self.assertFalse(self._get_my_other_forums(None)) + self.assertFalse(self._get_my_other_forums(True)) + if user != self.user_public: + self.assertFalse(self._get_my_other_forums(self.forum_1_website_2)) + self.assertEqual(self._get_my_other_forums(self.forum_2_website_2), self.forum_1_website_2) + employee_2_website_2_forum_2_post.favourite_ids += self.env.user + self.assertEqual(self._get_my_other_forums(self.forum_1_website_2), self.forum_2_website_2) diff --git a/addons/website_forum/views/forum_forum_templates_layout.xml b/addons/website_forum/views/forum_forum_templates_layout.xml index 6a9d5860150..bb3f061a5c9 100644 --- a/addons/website_forum/views/forum_forum_templates_layout.xml +++ b/addons/website_forum/views/forum_forum_templates_layout.xml @@ -338,6 +338,15 @@ +
+
My forums
+ + + + + + +