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.

108 lines
3.9 KiB

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