Skip to content

Commit

Permalink
[FIX] website_forum: fix tag click event not found records
Browse files Browse the repository at this point in the history
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 #94910

X-original-commit: 4ff52d0
Signed-off-by: Romain Derie (rde) <rde@odoo.com>
Co-authored-by: hoangtiendung <hoangtiendung070797@gmail.com>
  • Loading branch information
rdeodoo and hoangtiendung070797 committed Jun 30, 2022
1 parent a99b567 commit cc70a6c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
10 changes: 9 additions & 1 deletion addons/website_forum/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

from odoo.exceptions import UserError
from odoo.http import request
from odoo.osv import expression


_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -169,8 +171,14 @@ def forum_faq_karma(self, forum, **post):

@http.route('/forum/get_tags', type='http', auth="public", methods=['GET'], website=True, sitemap=False)
def tag_read(self, query='', limit=25, **post):
# TODO: In master always check the forum_id domain part and add forum_id
# as required method param, not in **post
forum_id = post.get('forum_id')
domain = [('name', '=ilike', (query or '') + "%")]
if forum_id:
domain = expression.AND([domain, [('forum_id', '=', int(forum_id))]])
data = request.env['forum.tag'].search_read(
domain=[('name', '=ilike', (query or '') + "%")],
domain=domain,
fields=['id', 'name'],
limit=int(limit),
)
Expand Down
4 changes: 2 additions & 2 deletions addons/website_forum/models/forum.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def unlink(self):
self._update_website_count()
return super(Forum, self).unlink()

@api.model
@api.model # TODO: Remove me, this is not an `api.model` method
def _tag_to_write_vals(self, tags=''):
Tag = self.env['forum.tag']
post_tags = []
Expand All @@ -235,7 +235,7 @@ def _tag_to_write_vals(self, tags=''):
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:
Expand Down
1 change: 1 addition & 0 deletions addons/website_forum/static/src/js/website_forum.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ publicWidget.registry.websiteForum = publicWidget.Widget.extend({
return {
query: term,
limit: 50,
forum_id: $('#wrapwrap').data('forum_id'),
};
},
results: function (data) {
Expand Down
22 changes: 22 additions & 0 deletions addons/website_forum/tests/test_forum.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,25 @@ def test_forum_mode_discussions(self):
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.")

0 comments on commit cc70a6c

Please sign in to comment.