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.

143 lines
4.8 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Utils
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. from odoo import api, fields, models
  23. class Groups(models.AbstractModel):
  24. _name = "muk_utils.mixins.groups"
  25. _description = "Group Mixin"
  26. _parent_store = True
  27. _parent_name = "parent_group"
  28. # ----------------------------------------------------------
  29. # Database
  30. # ----------------------------------------------------------
  31. name = fields.Char(string="Group Name", required=True, translate=True)
  32. parent_path = fields.Char(string="Parent Path", index=True)
  33. count_users = fields.Integer(compute="_compute_users", string="Users", store=True)
  34. @api.model
  35. def _add_magic_fields(self):
  36. super(Groups, self)._add_magic_fields()
  37. def add(name, field):
  38. if name not in self._fields:
  39. self._add_field(name, field)
  40. add(
  41. "parent_group",
  42. fields.Many2one(
  43. _module=self._module,
  44. comodel_name=self._name,
  45. string="Parent Group",
  46. ondelete="cascade",
  47. auto_join=True,
  48. index=True,
  49. automatic=True,
  50. ),
  51. )
  52. add(
  53. "child_groups",
  54. fields.One2many(
  55. _module=self._module,
  56. comodel_name=self._name,
  57. inverse_name="parent_group",
  58. string="Child Groups",
  59. automatic=True,
  60. ),
  61. )
  62. add(
  63. "groups",
  64. fields.Many2many(
  65. _module=self._module,
  66. comodel_name="res.groups",
  67. relation="{}_groups_rel".format(self._table),
  68. column1="gid",
  69. column2="rid",
  70. string="Groups",
  71. automatic=True,
  72. ),
  73. )
  74. add(
  75. "explicit_users",
  76. fields.Many2many(
  77. _module=self._module,
  78. comodel_name="res.users",
  79. relation="{}_explicit_users_rel".format(self._table),
  80. column1="gid",
  81. column2="uid",
  82. string="Explicit Users",
  83. automatic=True,
  84. ),
  85. )
  86. add(
  87. "users",
  88. fields.Many2many(
  89. _module=self._module,
  90. comodel_name="res.users",
  91. relation="{}_users_rel".format(self._table),
  92. column1="gid",
  93. column2="uid",
  94. string="Group Users",
  95. compute="_compute_users",
  96. store=True,
  97. automatic=True,
  98. ),
  99. )
  100. _sql_constraints = [
  101. ("name_uniq", "unique (name)", "The name of the group must be unique!")
  102. ]
  103. # ----------------------------------------------------------
  104. # Functions
  105. # ----------------------------------------------------------
  106. @api.model
  107. def default_get(self, fields_list):
  108. res = super(Groups, self).default_get(fields_list)
  109. if not self.env.context.get("groups_no_autojoin"):
  110. if "explicit_users" in res and res["explicit_users"]:
  111. res["explicit_users"] = res["explicit_users"] + [self.env.uid]
  112. else:
  113. res["explicit_users"] = [self.env.uid]
  114. return res
  115. # ----------------------------------------------------------
  116. # Read, View
  117. # ----------------------------------------------------------
  118. @api.depends(
  119. "parent_group", "parent_group.users", "groups", "groups.users", "explicit_users"
  120. )
  121. def _compute_users(self):
  122. for record in self:
  123. users = record.mapped("groups.users")
  124. users |= record.mapped("explicit_users")
  125. users |= record.mapped("parent_group.users")
  126. record.update({"users": users, "count_users": len(users)})