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.

125 lines
4.4 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 models, fields, api
  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(
  32. string="Group Name",
  33. required=True,
  34. translate=True)
  35. parent_path = fields.Char(
  36. string="Parent Path",
  37. index=True)
  38. count_users = fields.Integer(
  39. compute='_compute_users',
  40. string="Users",
  41. store=True)
  42. @api.model
  43. def _add_magic_fields(self):
  44. super(Groups, self)._add_magic_fields()
  45. def add(name, field):
  46. if name not in self._fields:
  47. self._add_field(name, field)
  48. add('parent_group', fields.Many2one(
  49. _module=self._module,
  50. comodel_name=self._name,
  51. string='Parent Group',
  52. ondelete='cascade',
  53. auto_join=True,
  54. index=True,
  55. automatic=True))
  56. add('child_groups', fields.One2many(
  57. _module=self._module,
  58. comodel_name=self._name,
  59. inverse_name='parent_group',
  60. string='Child Groups',
  61. automatic=True))
  62. add('groups', fields.Many2many(
  63. _module=self._module,
  64. comodel_name='res.groups',
  65. relation='%s_groups_rel' % (self._table),
  66. column1='gid',
  67. column2='rid',
  68. string='Groups',
  69. automatic=True))
  70. add('explicit_users', fields.Many2many(
  71. _module=self._module,
  72. comodel_name='res.users',
  73. relation='%s_explicit_users_rel' % (self._table),
  74. column1='gid',
  75. column2='uid',
  76. string='Explicit Users',
  77. automatic=True))
  78. add('users', fields.Many2many(
  79. _module=self._module,
  80. comodel_name='res.users',
  81. relation='%s_users_rel' % (self._table),
  82. column1='gid',
  83. column2='uid',
  84. string='Group Users',
  85. compute='_compute_users',
  86. store=True,
  87. automatic=True))
  88. _sql_constraints = [
  89. ('name_uniq', 'unique (name)', 'The name of the group must be unique!')
  90. ]
  91. #----------------------------------------------------------
  92. # Functions
  93. #----------------------------------------------------------
  94. @api.model
  95. def default_get(self, fields_list):
  96. res = super(Groups, self).default_get(fields_list)
  97. if not self.env.context.get('groups_no_autojoin'):
  98. if 'explicit_users' in res and res['explicit_users']:
  99. res['explicit_users'] = res['explicit_users'] + [self.env.uid]
  100. else:
  101. res['explicit_users'] = [self.env.uid]
  102. return res
  103. #----------------------------------------------------------
  104. # Read, View
  105. #----------------------------------------------------------
  106. @api.depends('parent_group', 'parent_group.users', 'groups', 'groups.users', 'explicit_users')
  107. def _compute_users(self):
  108. for record in self:
  109. users = record.mapped('groups.users')
  110. users |= record.mapped('explicit_users')
  111. users |= record.mapped('parent_group.users')
  112. record.update({'users': users, 'count_users': len(users)})