summaryrefslogtreecommitdiff
path: root/addons/hr_gamification/models/gamification.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/hr_gamification/models/gamification.py
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/hr_gamification/models/gamification.py')
-rw-r--r--addons/hr_gamification/models/gamification.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/addons/hr_gamification/models/gamification.py b/addons/hr_gamification/models/gamification.py
new file mode 100644
index 00000000..6f9dba17
--- /dev/null
+++ b/addons/hr_gamification/models/gamification.py
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+from odoo.exceptions import ValidationError
+
+
+class GamificationBadgeUser(models.Model):
+ """User having received a badge"""
+ _inherit = 'gamification.badge.user'
+
+ employee_id = fields.Many2one('hr.employee', string='Employee')
+
+ @api.constrains('employee_id')
+ def _check_employee_related_user(self):
+ for badge_user in self:
+ if badge_user.employee_id not in badge_user.user_id.employee_ids:
+ raise ValidationError(_('The selected employee does not correspond to the selected user.'))
+
+
+class GamificationBadge(models.Model):
+ _inherit = 'gamification.badge'
+
+ granted_employees_count = fields.Integer(compute="_compute_granted_employees_count")
+
+ @api.depends('owner_ids.employee_id')
+ def _compute_granted_employees_count(self):
+ for badge in self:
+ badge.granted_employees_count = self.env['gamification.badge.user'].search_count([
+ ('badge_id', '=', badge.id),
+ ('employee_id', '!=', False)
+ ])
+
+ def get_granted_employees(self):
+ employee_ids = self.mapped('owner_ids.employee_id').ids
+ return {
+ 'type': 'ir.actions.act_window',
+ 'name': 'Granted Employees',
+ 'view_mode': 'kanban,tree,form',
+ 'res_model': 'hr.employee.public',
+ 'domain': [('id', 'in', employee_ids)]
+ }