This is a side-effect of commit [1]. Consider we have access to companies A and B, and current active company is A. Before the mentioned commit we were able to grand a badge to an employee in company B. We should keep that behavior, as long as the current user have also access to company B. [1]:https://github.com/odoo/odoo/commit/b5b105eabd90419a5856e8279e277743c5915fc9 closes odoo/odoo#95757 X-original-commit: 9590b910ccaf92d0466b27ce3c4dda3c567f8676 Signed-off-by: Kevin Baptiste <kba@odoo.com>
52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
# -*- 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', index=True)
|
|
|
|
@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.\
|
|
with_context(allowed_company_ids=self.env.user.company_ids.ids).employee_ids:
|
|
raise ValidationError(_('The selected employee does not correspond to the selected user.'))
|
|
|
|
def action_open_badge(self):
|
|
self.ensure_one()
|
|
return {
|
|
'type': 'ir.actions.act_window',
|
|
'res_model': 'gamification.badge',
|
|
'view_mode': 'form',
|
|
'res_id': self.id,
|
|
}
|
|
|
|
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)]
|
|
}
|