[FIX] website_forum: fix tag click event not found records

A forum tag is always linked to a forum.
But some flows are not considering that part and are returning/using
tags from other forum too.

Step to reproduce, having 2 forum `Cats` and `Dogs`:
- Create a `Food` tag on the `Dogs` forum
- On the `Cats` forum, type `Food` in the tag input
- First error: the `Food` from `Dogs` incorrectly shows up
  It means that the user can't create a new one from the `Cats` forum
  but will actually have to select (without know it) the one from the
  `Dogs` forum.
- Select that tag anyway
- Second error: The tags displayed on the post is redirecting to the
  wrong other forum, ultimately not showing the post from all forums
  (and the one you just came from).
- Third error: The /tags page won't list the `Food` tag on the `Cats`
  forum.

Note that there is probably some access traceback involved in a multi
website and/or multi company setup.

After this commit:
- only the tags from the current forum will show up and be selectable
  when creating a post.
- it will be possible to create a new tag for a forum is another forum
  already has a tag of that name
- the tag on the post (next to its title) will not redirect to the
  incorrect (and possibly not accessible) forum

Note that the method was already reading multiple properties of `self`,
it was already not an `api.model` method anymore.

closes odoo/odoo#94917

X-original-commit: 4ff52d0b9a71d3072f5973d8e900d802e6b129c0
Signed-off-by: Romain Derie (rde) <rde@odoo.com>
Co-authored-by: hoangtiendung <hoangtiendung070797@gmail.com>
This commit is contained in:
Romain Derie
2022-06-30 20:56:14 +02:00
co-authored by hoangtiendung
parent 250f72d960
commit 1186cd1c40
4 changed files with 27 additions and 4 deletions
+3 -2
View File
@@ -20,6 +20,7 @@ from odoo.addons.portal.controllers.portal import _build_url_w_params
from odoo.exceptions import UserError
from odoo.http import request
_logger = logging.getLogger(__name__)
@@ -179,9 +180,9 @@ class WebsiteForum(WebsiteProfile):
return request.render("website_forum.faq_karma", values)
@http.route('/forum/get_tags', type='http', auth="public", methods=['GET'], website=True, sitemap=False)
def tag_read(self, query='', limit=25, **post):
def tag_read(self, forum_id, query='', limit=25, **post):
data = request.env['forum.tag'].search_read(
domain=[('name', '=ilike', (query or '') + "%")],
domain=[('forum_id', '=', int(forum_id)), ('name', '=ilike', (query or '') + "%")],
fields=['id', 'name'],
limit=int(limit),
)
+1 -2
View File
@@ -222,7 +222,6 @@ class Forum(models.Model):
self._update_website_count()
return super(Forum, self).unlink()
@api.model
def _tag_to_write_vals(self, tags=''):
Tag = self.env['forum.tag']
post_tags = []
@@ -231,7 +230,7 @@ class Forum(models.Model):
for tag in (tag for tag in tags.split(',') if tag):
if tag.startswith('_'): # it's a new tag
# check that not already created meanwhile or maybe excluded by the limit on the search
tag_ids = Tag.search([('name', '=', tag[1:])])
tag_ids = Tag.search([('name', '=', tag[1:]), ('forum_id', '=', self.id)])
if tag_ids:
existing_keep.append(int(tag_ids[0]))
else:
@@ -94,6 +94,7 @@ publicWidget.registry.websiteForum = publicWidget.Widget.extend({
return {
query: term,
limit: 50,
forum_id: $('#wrapwrap').data('forum_id'),
};
},
results: function (data) {
+22
View File
@@ -436,3 +436,25 @@ class TestForum(TestForumCommon):
not discussions_post.uid_has_answered or discussions_post.forum_id.mode == 'discussions', True)
self.assertEqual(
discussions_post.uid_has_answered and discussions_post.forum_id.mode == 'questions', False)
def test_tag_creation_multi_forum(self):
Post = self.env['forum.post']
forum_1 = self.forum
forum_2 = forum_1.copy({
'name': 'Questions Forum'
})
self.user_portal.karma = KARMA['tag_create']
Post.with_user(self.user_portal).create({
'name': "Post Forum 1",
'forum_id': forum_1.id,
'tag_ids': forum_1._tag_to_write_vals('_Food'),
})
Post.with_user(self.user_portal).create({
'name': "Post Forum 2",
'forum_id': forum_2.id,
'tag_ids': forum_2._tag_to_write_vals('_Food'),
})
food_tags = self.env['forum.tag'].search([('name', '=', 'Food')])
self.assertEqual(len(food_tags), 2, "One Food tag should have been created in each forum.")
self.assertIn(forum_1, food_tags.forum_id, "One Food tag should have been created for forum 1.")
self.assertIn(forum_2, food_tags.forum_id, "One Food tag should have been created for forum 2.")