summaryrefslogtreecommitdiff
path: root/addons/gamification/models/gamification_karma_rank.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/gamification/models/gamification_karma_rank.py
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/gamification/models/gamification_karma_rank.py')
-rw-r--r--addons/gamification/models/gamification_karma_rank.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/addons/gamification/models/gamification_karma_rank.py b/addons/gamification/models/gamification_karma_rank.py
new file mode 100644
index 00000000..8e987076
--- /dev/null
+++ b/addons/gamification/models/gamification_karma_rank.py
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+from odoo import api, fields, models
+from odoo.tools.translate import html_translate
+
+
+class KarmaRank(models.Model):
+ _name = 'gamification.karma.rank'
+ _description = 'Rank based on karma'
+ _inherit = 'image.mixin'
+ _order = 'karma_min'
+
+ name = fields.Text(string='Rank Name', translate=True, required=True)
+ description = fields.Html(string='Description', translate=html_translate, sanitize_attributes=False,)
+ 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', 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")
+ rank_users_count = fields.Integer("# Users", compute="_compute_rank_users_count")
+
+ _sql_constraints = [
+ ('karma_min_check', "CHECK( karma_min > 0 )", 'The required karma has to be above 0.')
+ ]
+
+ @api.depends('user_ids')
+ def _compute_rank_users_count(self):
+ requests_data = self.env['res.users'].read_group([('rank_id', '!=', False)], ['rank_id'], ['rank_id'])
+ requests_mapped_data = dict((data['rank_id'][0], data['rank_id_count']) for data in requests_data)
+ for rank in self:
+ rank.rank_users_count = requests_mapped_data.get(rank.id, 0)
+
+ @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()
+ return res
+
+ def write(self, vals):
+ if 'karma_min' in vals:
+ previous_ranks = self.env['gamification.karma.rank'].search([], order="karma_min DESC").ids
+ low = min(vals['karma_min'], self.karma_min)
+ high = max(vals['karma_min'], self.karma_min)
+
+ res = super(KarmaRank, self).write(vals)
+
+ 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)])
+ else:
+ users = self.env['res.users'].sudo().search([('karma', '>=', low), ('karma', '<=', high)])
+ users._recompute_rank()
+ return res