[IMP] website_forum: display other user forum links in the sidebar

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) <std@odoo.com>
This commit is contained in:
Pierre-Yves Dufays
2023-09-19 16:37:11 +00:00
parent 01c909f8ec
commit e24058b14d
5 changed files with 96 additions and 0 deletions
@@ -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):
+1
View File
@@ -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
+8
View File
@@ -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({
@@ -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)
@@ -338,6 +338,15 @@
<t t-out="tag.name"/>
</a>
</div>
<div t-if="my_other_forums" class="o_wforum_sidebar_section pt-3">
<div class="px-3 pb-1 fw-bold">My forums</div>
<t t-foreach="my_other_forums.sorted(lambda f: f.name.casefold())" t-as="my_forum">
<a class="nav-link my-1 py-1 text-reset" t-attf-href="/forum/#{slug(my_forum)}">
<i class="fa fa-file-o fa-fw opacity-50"/>
<t t-out="my_forum.name"/>
</a>
</t>
</div>
</template>
<template id="header_welcome_message" inherit_id="website_forum.header" name="Forum Welcome Message (oe_structure_forum_top)">