[FIX] website_forum: use read_group to compute user statistics
Instead of doing 1 search_count / user in the loop, use read_group to fetch data in batch. Also remove an unnecessary import. Task-3240082 (Forum: Reorganize and lint module) Part-of: odoo/odoo#114841
This commit is contained in:
@@ -1,20 +1,31 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class Users(models.Model):
|
||||
_inherit = 'res.users'
|
||||
|
||||
create_date = fields.Datetime('Create Date', readonly=True, index=True)
|
||||
forum_waiting_posts_count = fields.Integer('Waiting post', compute="_get_user_waiting_post")
|
||||
forum_waiting_posts_count = fields.Integer(
|
||||
'Waiting post', compute="_compute_forum_waiting_posts_count")
|
||||
|
||||
def _get_user_waiting_post(self):
|
||||
def _compute_forum_waiting_posts_count(self):
|
||||
if not self.ids:
|
||||
self.forum_waiting_posts_count = 0
|
||||
return
|
||||
read_group_res = self.env['forum.post']._read_group(
|
||||
[('create_uid', 'in', self.ids), ('state', '=', 'pending'), ('parent_id', '=', False)],
|
||||
['create_uid'],
|
||||
['create_uid'],
|
||||
)
|
||||
mapping = {
|
||||
res_group['create_uid'][0]: res_group['create_uid_count']
|
||||
for res_group in read_group_res
|
||||
}
|
||||
for user in self:
|
||||
Post = self.env['forum.post']
|
||||
domain = [('parent_id', '=', False), ('state', '=', 'pending'), ('create_uid', '=', user.id)]
|
||||
user.forum_waiting_posts_count = Post.search_count(domain)
|
||||
user.forum_waiting_posts_count = mapping.get(user.id, 0)
|
||||
|
||||
# Wrapper for call_kw with inherits
|
||||
def open_website_url(self):
|
||||
|
||||
Reference in New Issue
Block a user