From 5f51ed9ad94d48a70bd2f2eaf0a875b073449270 Mon Sep 17 00:00:00 2001 From: "Gaetan Vanden Bergh (gavb)" Date: Mon, 19 Feb 2024 09:17:39 +0100 Subject: [PATCH] [FIX] gamification: Goal definition "sum" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps: - Open Settings/Gamification Tools/Challenges - Create a new challenge - Keep "Assign Challenge to" with default values - Need a domain that return some user - Create and Edit a goal - Computation Mode: "Automatic: sum on a field" - Save & Close - In reward Tab set "For Every Succeeding User" - Save - Start Challenge Actual result: - Traceback due to wrong aggregate value - CHallenge is not started Expected result: - Challenge is started opw-3740960 odoo/odoo@234db70d closes odoo/odoo#154614 X-original-commit: aa0f617682a59c5ac285308000f35d44b75f04df Signed-off-by: Rémy Voet (ryv) --- .../gamification/models/gamification_goal.py | 2 +- addons/gamification/tests/test_challenge.py | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/addons/gamification/models/gamification_goal.py b/addons/gamification/models/gamification_goal.py index e8997ebddc1..55343a0f77e 100644 --- a/addons/gamification/models/gamification_goal.py +++ b/addons/gamification/models/gamification_goal.py @@ -211,7 +211,7 @@ class Goal(models.Model): if definition.computation_mode == 'sum': field_name = definition.field_id.name - res = Obj._read_group(domain, [], [field_name]) + res = Obj._read_group(domain, [], [f'{field_name}:{definition.computation_mode}']) new_value = res[0][0] or 0.0 else: # computation mode = count diff --git a/addons/gamification/tests/test_challenge.py b/addons/gamification/tests/test_challenge.py index de0cc5f582d..ac3d1abd10e 100644 --- a/addons/gamification/tests/test_challenge.py +++ b/addons/gamification/tests/test_challenge.py @@ -151,6 +151,39 @@ class test_challenge(TestGamificationCommon): unchanged_goal_ids.user_id, ) + def test_30_create_challenge_with_sum_goal(self): + challenge = self.env['gamification.challenge'].create({ + 'name': 'test', + 'state': 'draft', + 'user_domain': '[("active", "=", True)]', #Include all active users to get a least one participant + 'reward_id': 1, + }) + + model = self.env['ir.model'].search([('model', '=', 'gamification.badge')])[0] + field = self.env['ir.model.fields'].search([('model', '=', 'gamification.badge'), ('name', '=', 'rule_max_number')])[0] + + sum_goal = self.env['gamification.goal.definition'].create({ + 'name': 'test', + 'computation_mode': 'sum', + 'model_id': model.id, + 'field_id': field.id + }) + + self.env['gamification.challenge.line'].create({ + 'challenge_id': challenge.id, + 'definition_id': sum_goal.id, + 'condition': 'higher', + 'target_goal': 1 + }) + + challenge.action_start() + + self.assertEqual( + challenge.state, + 'inprogress', + "Challenge failed to start", + ) + class test_badge_wizard(TestGamificationCommon):