[FIX] website_forum: fix website/forum count synchronization, rename field
Currently 'forums_count' field on website is not correctly updated. Test introduced recently show that * at website create, counter is not computed; * at forum create, counter is not computed; Only updating a forum active flag, website value, or unlinking it recomputes the counters. Note that manually updating counters is required as forum with False website are global forum, valid for all websites. Finally rename 'forums_count' into 'forum_count'. Fields in Odoo are rarely plural (more like 'forum_ids' / 'forum_count' for example, even if in our case the 2many does not exist due to shared behavior). Task-3240082 (Forum: Reorganize and lint module) Part-of: odoo/odoo#114841
This commit is contained in:
@@ -198,12 +198,13 @@ class Forum(models.Model):
|
||||
Forum,
|
||||
self.with_context(mail_create_nolog=True, mail_create_nosubscribe=True)
|
||||
).create(vals_list)
|
||||
forums._set_default_faq() # will trigger a write and call update_website_count
|
||||
self.env['website'].sudo()._update_forum_count()
|
||||
forums._set_default_faq()
|
||||
return forums
|
||||
|
||||
def unlink(self):
|
||||
self._update_website_count()
|
||||
return super(Forum, self).unlink()
|
||||
self.env['website'].sudo()._update_forum_count()
|
||||
return super().unlink()
|
||||
|
||||
def write(self, vals):
|
||||
if 'privacy' in vals:
|
||||
@@ -216,13 +217,13 @@ class Forum(models.Model):
|
||||
elif vals['privacy'] == 'connected':
|
||||
vals['authorized_group_id'] = False
|
||||
|
||||
res = super(Forum, self).write(vals)
|
||||
res = super().write(vals)
|
||||
if 'active' in vals:
|
||||
# archiving/unarchiving a forum does it on its posts, too
|
||||
self.env['forum.post'].with_context(active_test=False).search([('forum_id', 'in', self.ids)]).write({'active': vals['active']})
|
||||
|
||||
if 'active' in vals or 'website_id' in vals:
|
||||
self._update_website_count()
|
||||
self.env['website'].sudo()._update_forum_count()
|
||||
return res
|
||||
|
||||
def _set_default_faq(self):
|
||||
@@ -267,11 +268,6 @@ class Forum(models.Model):
|
||||
self.ensure_one()
|
||||
return self.env['website'].get_client_action(self._compute_website_url())
|
||||
|
||||
@api.model
|
||||
def _update_website_count(self):
|
||||
for website in self.env['website'].sudo().search([]):
|
||||
website.forums_count = self.env['forum.forum'].sudo().search_count(website.website_domain())
|
||||
|
||||
@api.model
|
||||
def _search_get_detail(self, website, order, options):
|
||||
with_description = options['displayDescription']
|
||||
|
||||
@@ -8,11 +8,13 @@ from odoo.addons.http_routing.models.ir_http import url_for
|
||||
class Website(models.Model):
|
||||
_inherit = 'website'
|
||||
|
||||
@api.model
|
||||
def get_default_forum_count(self):
|
||||
self.forums_count = self.env['forum.forum'].search_count(self.website_domain())
|
||||
forum_count = fields.Integer(readonly=True, default=0)
|
||||
|
||||
forums_count = fields.Integer(readonly=True, default=get_default_forum_count)
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
websites = super().create(vals_list)
|
||||
websites._update_forum_count()
|
||||
return websites
|
||||
|
||||
def get_suggested_controllers(self):
|
||||
suggested_controllers = super(Website, self).get_suggested_controllers()
|
||||
@@ -37,3 +39,18 @@ class Website(models.Model):
|
||||
if search_type in ['forums', 'forum_posts_only', 'all']:
|
||||
result.append(self.env['forum.post']._search_get_detail(self, order, options))
|
||||
return result
|
||||
|
||||
def _update_forum_count(self):
|
||||
""" Update count of forum linked to some websites. This has to be
|
||||
done manually as website_id=False on forum model means a shared forum.
|
||||
There is therefore no straightforward relationship to be used between
|
||||
forum and website.
|
||||
|
||||
This method either runs on self (if not void), either on all existing
|
||||
websites (to update globally counters, notably when a new forum is
|
||||
created). """
|
||||
websites = self if self else self.search([])
|
||||
for website in websites:
|
||||
website.forum_count = self.env['forum.forum'].sudo().search_count(
|
||||
website.website_domain()
|
||||
)
|
||||
|
||||
@@ -22,14 +22,14 @@ class TestForumInternals(TestForumCommon):
|
||||
self.assertFalse(forums.website_id)
|
||||
|
||||
@users('admin')
|
||||
def test_website_forums_count(self):
|
||||
def test_website_forum_count(self):
|
||||
""" Test synchronization of website / forum counters. """
|
||||
base_website = self.base_website.with_env(self.env)
|
||||
website_2 = self.website_2.with_env(self.env)
|
||||
|
||||
self.assertEqual(base_website.forums_count, 2,
|
||||
self.assertEqual(base_website.forum_count, 2,
|
||||
'Should count default global forums')
|
||||
self.assertEqual(website_2.forums_count, 2,
|
||||
self.assertEqual(website_2.forum_count, 2,
|
||||
'Should count default global forums')
|
||||
|
||||
new_forums = self.env['forum.forum'].create([
|
||||
@@ -47,15 +47,15 @@ class TestForumInternals(TestForumCommon):
|
||||
'website_id': website_2.id,
|
||||
}
|
||||
])
|
||||
self.assertEqual(base_website.forums_count, 4,
|
||||
self.assertEqual(base_website.forum_count, 4,
|
||||
'3 globals, 1 specific')
|
||||
self.assertEqual(website_2.forums_count, 5,
|
||||
self.assertEqual(website_2.forum_count, 5,
|
||||
'3 globals, 2 specific')
|
||||
|
||||
new_forums.write({'website_id': False})
|
||||
self.assertEqual(base_website.forums_count, 6,
|
||||
self.assertEqual(base_website.forum_count, 6,
|
||||
'6 global forums')
|
||||
self.assertEqual(website_2.forums_count, 6,
|
||||
self.assertEqual(website_2.forum_count, 6,
|
||||
'6 global forums')
|
||||
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@
|
||||
<!-- Desktop -->
|
||||
<ul class="navbar-nav me-auto d-none d-lg-flex">
|
||||
<li class="nav-item">
|
||||
<a t-if="request.website.forums_count > 1" class="nav-link" href="/forum/" title="All forums">
|
||||
<a t-if="request.website.forum_count > 1" class="nav-link" href="/forum/" title="All forums">
|
||||
All Forums
|
||||
</a>
|
||||
</li>
|
||||
|
||||
Reference in New Issue
Block a user