From 3773a0c552e1e671fa5dbcfa23cac82e12d24f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Warnon?= Date: Mon, 4 Nov 2019 08:03:36 +0000 Subject: [PATCH] [IMP] gamification: add sql constraint to force rank's min_karma > 0 This commit adds a SQL constraint to force the min_karma of the gamification.karma.rank model to be above 0. A karma_min set to 0 (or lower) could create some frontend issues when displaying required karma. PR #39870 Task#2032649 --- addons/gamification/models/gamification_karma_rank.py | 9 +++++++-- addons/gamification/tests/test_karma_tracking.py | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/addons/gamification/models/gamification_karma_rank.py b/addons/gamification/models/gamification_karma_rank.py index fc50218148e..a324b323773 100644 --- a/addons/gamification/models/gamification_karma_rank.py +++ b/addons/gamification/models/gamification_karma_rank.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import api, fields, models, exceptions +from odoo import api, fields, models from odoo.tools.translate import html_translate class KarmaRank(models.Model): @@ -14,9 +14,14 @@ class KarmaRank(models.Model): description_motivational = fields.Html( string='Motivational', translate=html_translate, sanitize_attributes=False, help="Motivational phrase to reach this rank") - karma_min = fields.Integer(string='Required Karma', help='Minimum karma needed to reach this rank') + karma_min = fields.Integer(string='Required Karma', required=True, default=1, + help='Minimum karma needed to reach this rank') user_ids = fields.One2many('res.users', 'rank_id', string='Users', help="Users having this rank") + _sql_constraints = [ + ('karma_min_check', "CHECK( karma_min > 0 )", 'The required karma has to be above 0.') + ] + @api.model_create_multi def create(self, values_list): res = super(KarmaRank, self).create(values_list) diff --git a/addons/gamification/tests/test_karma_tracking.py b/addons/gamification/tests/test_karma_tracking.py index 3bbd904a86b..47b6eb577a7 100644 --- a/addons/gamification/tests/test_karma_tracking.py +++ b/addons/gamification/tests/test_karma_tracking.py @@ -219,7 +219,7 @@ class TestComputeRankCommon(common.SavepointCase): cls.rank_1 = cls.env['gamification.karma.rank'].create({ 'name': 'rank 1', - 'karma_min': 0, + 'karma_min': 1, }) cls.rank_2 = cls.env['gamification.karma.rank'].create({