You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.7 KiB

  1. from odoo import api, fields, models
  2. class ResGroups(models.Model):
  3. _inherit = "res.groups"
  4. # The inverse field of the field group_id on the res.users.role model
  5. # This field should be used a One2one relation as a role can only be
  6. # represented by one group. It's declared as a One2many field as the
  7. # inverse field on the res.users.role it's declared as a Many2one
  8. role_id = fields.One2many(
  9. comodel_name="res.users.role",
  10. inverse_name="group_id",
  11. help="Relation for the groups that represents a role",
  12. )
  13. role_ids = fields.Many2many(
  14. comodel_name="res.users.role",
  15. relation="res_groups_implied_roles_rel",
  16. string="Roles",
  17. compute="_compute_role_ids",
  18. help="Roles in which the group is involved",
  19. )
  20. parent_ids = fields.Many2many(
  21. "res.groups",
  22. "res_groups_implied_rel",
  23. "hid",
  24. "gid",
  25. string="Parents",
  26. help="Inverse relation for the Inherits field. "
  27. "The groups from which this group is inheriting",
  28. )
  29. trans_parent_ids = fields.Many2many(
  30. comodel_name="res.groups",
  31. string="Parent Groups",
  32. compute="_compute_trans_parent_ids",
  33. )
  34. role_count = fields.Integer("# Roles", compute="_compute_role_count")
  35. def _compute_role_count(self):
  36. for group in self:
  37. group.role_count = len(group.role_ids)
  38. @api.depends("parent_ids.trans_parent_ids")
  39. def _compute_trans_parent_ids(self):
  40. for group in self:
  41. group.trans_parent_ids = (
  42. group.parent_ids | group.parent_ids.trans_parent_ids
  43. )
  44. def _compute_role_ids(self):
  45. for group in self:
  46. if group.trans_parent_ids:
  47. group.role_ids = group.trans_parent_ids.role_id
  48. else:
  49. group.role_ids = group.role_id
  50. def action_view_roles(self):
  51. self.ensure_one()
  52. action = self.env["ir.actions.act_window"]._for_xml_id(
  53. "base_user_role.action_res_users_role_tree"
  54. )
  55. action["context"] = {}
  56. if len(self.role_ids) > 1:
  57. action["domain"] = [("id", "in", self.role_ids.ids)]
  58. elif self.role_ids:
  59. form_view = [
  60. (self.env.ref("base_user_role.view_res_users_role_form").id, "form")
  61. ]
  62. if "views" in action:
  63. action["views"] = form_view + [
  64. (state, view) for state, view in action["views"] if view != "form"
  65. ]
  66. else:
  67. action["views"] = form_view
  68. action["res_id"] = self.role_ids.id
  69. else:
  70. action = {"type": "ir.actions.act_window_close"}
  71. return action