From 9dc6234e48ffdf09e0b27ef0c1b8ccc9fc718e5c Mon Sep 17 00:00:00 2001 From: Fabio Barbero Date: Thu, 21 Apr 2022 10:08:59 +0000 Subject: [PATCH] [IMP] gamification: improve performance of ranks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Purpose ======= Avoid calling _recompute_rank for users that are not affected by the newly created/written ranks, by filtering users by karma_min Task-2746929 closes odoo/odoo#89463 X-original-commit: 3a0562ea809b5ba40a66fa4a996424696320f91c Signed-off-by: Warnon Aurélien (awa) --- addons/gamification/models/gamification_karma_rank.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/addons/gamification/models/gamification_karma_rank.py b/addons/gamification/models/gamification_karma_rank.py index 737e0187320..eaa1cfa10c1 100644 --- a/addons/gamification/models/gamification_karma_rank.py +++ b/addons/gamification/models/gamification_karma_rank.py @@ -35,8 +35,10 @@ class KarmaRank(models.Model): @api.model_create_multi def create(self, values_list): res = super(KarmaRank, self).create(values_list) - users = self.env['res.users'].sudo().search([('karma', '>', 0)]) - users._recompute_rank() + if any(res.mapped('karma_min')) > 0: + users = self.env['res.users'].sudo().search([('karma', '>=', max(min(res.mapped('karma_min')), 1))]) + if users: + users._recompute_rank() return res def write(self, vals): @@ -50,8 +52,8 @@ class KarmaRank(models.Model): if 'karma_min' in vals: after_ranks = self.env['gamification.karma.rank'].search([], order="karma_min DESC").ids if previous_ranks != after_ranks: - users = self.env['res.users'].sudo().search([('karma', '>', 0)]) + users = self.env['res.users'].sudo().search([('karma', '>=', max(low, 1))]) else: - users = self.env['res.users'].sudo().search([('karma', '>=', low), ('karma', '<=', high)]) + users = self.env['res.users'].sudo().search([('karma', '>=', max(low, 1)), ('karma', '<=', high)]) users._recompute_rank() return res