From 15eed5b4958b652872190efac67013e98b05052b Mon Sep 17 00:00:00 2001 From: GuillemCForgeFlow Date: Tue, 19 Apr 2022 09:20:01 +0200 Subject: [PATCH] [15.0][FW]base_user_role: Groups-roles navigation --- base_user_role/__manifest__.py | 1 + base_user_role/models/__init__.py | 2 +- base_user_role/models/group.py | 86 ++++++++++++++++++++++++++ base_user_role/models/res_groups.py | 9 --- base_user_role/readme/CONTRIBUTORS.rst | 1 + base_user_role/views/group.xml | 23 +++++++ 6 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 base_user_role/models/group.py delete mode 100644 base_user_role/models/res_groups.py create mode 100644 base_user_role/views/group.xml diff --git a/base_user_role/__manifest__.py b/base_user_role/__manifest__.py index 019661381..e173e78b1 100644 --- a/base_user_role/__manifest__.py +++ b/base_user_role/__manifest__.py @@ -17,6 +17,7 @@ "data/ir_module_category.xml", "views/role.xml", "views/user.xml", + "views/group.xml", ], "installable": True, } diff --git a/base_user_role/models/__init__.py b/base_user_role/models/__init__.py index 44140bcda..80cd1fb91 100644 --- a/base_user_role/models/__init__.py +++ b/base_user_role/models/__init__.py @@ -1,3 +1,3 @@ from . import role from . import user -from . import res_groups +from . import group diff --git a/base_user_role/models/group.py b/base_user_role/models/group.py new file mode 100644 index 000000000..eac5558b8 --- /dev/null +++ b/base_user_role/models/group.py @@ -0,0 +1,86 @@ +from odoo import api, fields, models + + +class ResGroups(models.Model): + _inherit = "res.groups" + + view_access = fields.Many2many( + groups="base.group_system", + ) + + # The inverse field of the field group_id on the res.users.role model + # This field should be used a One2one relation as a role can only be + # represented by one group. It's declared as a One2many field as the + # inverse field on the res.users.role it's declared as a Many2one + role_id = fields.One2many( + comodel_name="res.users.role", + inverse_name="group_id", + help="Relation for the groups that represents a role", + ) + + role_ids = fields.Many2many( + comodel_name="res.users.role", + relation="res_groups_implied_roles_rel", + string="Roles", + compute="_compute_role_ids", + help="Roles in which the group is involved", + ) + + parent_ids = fields.Many2many( + "res.groups", + "res_groups_implied_rel", + "hid", + "gid", + string="Parents", + help="Inverse relation for the Inherits field. " + "The groups from which this group is inheriting", + ) + + trans_parent_ids = fields.Many2many( + comodel_name="res.groups", + string="Parent Groups", + compute="_compute_trans_parent_ids", + ) + + role_count = fields.Integer("# Roles", compute="_compute_role_count") + + def _compute_role_count(self): + for group in self: + group.role_count = len(group.role_ids) + + @api.depends("parent_ids.trans_parent_ids") + def _compute_trans_parent_ids(self): + for group in self: + group.trans_parent_ids = ( + group.parent_ids | group.parent_ids.trans_parent_ids + ) + + def _compute_role_ids(self): + for group in self: + if group.trans_parent_ids: + group.role_ids = group.trans_parent_ids.role_id + else: + group.role_ids = group.role_id + + def action_view_roles(self): + self.ensure_one() + action = self.env["ir.actions.act_window"]._for_xml_id( + "base_user_role.action_res_users_role_tree" + ) + action["context"] = {} + if len(self.role_ids) > 1: + action["domain"] = [("id", "in", self.role_ids.ids)] + elif self.role_ids: + form_view = [ + (self.env.ref("base_user_role.view_res_users_role_form").id, "form") + ] + if "views" in action: + action["views"] = form_view + [ + (state, view) for state, view in action["views"] if view != "form" + ] + else: + action["views"] = form_view + action["res_id"] = self.role_ids.id + else: + action = {"type": "ir.actions.act_window_close"} + return action diff --git a/base_user_role/models/res_groups.py b/base_user_role/models/res_groups.py deleted file mode 100644 index 5a19610d8..000000000 --- a/base_user_role/models/res_groups.py +++ /dev/null @@ -1,9 +0,0 @@ -from odoo import fields, models - - -class ResGroups(models.Model): - _inherit = "res.groups" - - view_access = fields.Many2many( - groups="base.group_system", - ) diff --git a/base_user_role/readme/CONTRIBUTORS.rst b/base_user_role/readme/CONTRIBUTORS.rst index 5fe7d2ac1..e60ef5724 100644 --- a/base_user_role/readme/CONTRIBUTORS.rst +++ b/base_user_role/readme/CONTRIBUTORS.rst @@ -5,5 +5,6 @@ * Harald Panten * Kevin Khao * Tatiana Deribina (https://sprintit.fi) +* Guillem Casassas Do not contact contributors directly about support or help with technical issues. diff --git a/base_user_role/views/group.xml b/base_user_role/views/group.xml new file mode 100644 index 000000000..98522b1b3 --- /dev/null +++ b/base_user_role/views/group.xml @@ -0,0 +1,23 @@ + + + + res.groups.form - base_user_role + res.groups + + + +
+ +
+
+
+
+