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.

179 lines
6.0 KiB

7 years ago
  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.groups'
  22. _inherit = 'muk_utils.model'
  23. _parent_store = True
  24. _parent_name = "parent_group"
  25. _parent_order = 'parent_left'
  26. _order = 'parent_left'
  27. #----------------------------------------------------------
  28. # Database
  29. #----------------------------------------------------------
  30. name = fields.Char(
  31. string="Group Name",
  32. required=True,
  33. translate=True)
  34. parent_left = fields.Integer(
  35. string='Left Parent',
  36. index=True)
  37. parent_right = fields.Integer(
  38. string='Right Parent',
  39. index=True)
  40. count_users = fields.Integer(
  41. compute='_compute_count_users',
  42. string="Users")
  43. @api.model
  44. def _add_magic_fields(self):
  45. super(Groups, self)._add_magic_fields()
  46. def add(name, field):
  47. if name not in self._fields:
  48. self._add_field(name, field)
  49. base, model = self._name.split(".")
  50. add('parent_group', fields.Many2one(
  51. _module=base,
  52. comodel_name=self._name,
  53. string='Parent Group',
  54. ondelete='cascade',
  55. auto_join=True,
  56. index=True,
  57. automatic=True))
  58. add('child_groups', fields.One2many(
  59. _module=base,
  60. comodel_name=self._name,
  61. inverse_name='parent_group',
  62. string='Child Groups',
  63. automatic=True))
  64. add('groups', fields.Many2many(
  65. _module=base,
  66. comodel_name='res.groups',
  67. relation='%s_groups_rel' % (self._table),
  68. column1='gid',
  69. column2='rid',
  70. string='Groups',
  71. automatic=True))
  72. add('explicit_users', fields.Many2many(
  73. _module=base,
  74. comodel_name='res.users',
  75. relation='%s_explicit_users_rel' % (self._table),
  76. column1='gid',
  77. column2='uid',
  78. string='Explicit Users',
  79. automatic=True))
  80. add('users', fields.Many2many(
  81. _module=base,
  82. comodel_name='res.users',
  83. relation='%s_users_rel' % (self._table),
  84. column1='gid',
  85. column2='uid',
  86. string='Users',
  87. compute='_compute_users',
  88. store=True,
  89. automatic=True))
  90. _sql_constraints = [
  91. ('name_uniq', 'unique (name)', 'The name of the group must be unique!')
  92. ]
  93. #----------------------------------------------------------
  94. # Functions
  95. #----------------------------------------------------------
  96. @api.multi
  97. def trigger_computation_up(self, fields, *largs, **kwargs):
  98. parent_groups = self.mapped('parent_group')
  99. if parent_groups.exists():
  100. parent_groups.with_context(is_parent=True).trigger_computation(fields)
  101. @api.multi
  102. def trigger_computation_down(self, fields, *largs, **kwargs):
  103. child_groups = self.mapped('child_groups')
  104. if child_groups.exists():
  105. child_groups.with_context(is_child=True).trigger_computation(fields)
  106. @api.multi
  107. def trigger_computation(self, fields, *largs, **kwargs):
  108. super(Groups, self).trigger_computation(fields, *largs, **kwargs)
  109. if "users" in fields:
  110. self.suspend_security()._compute_users()
  111. self.suspend_security().trigger_computation_down(fields)
  112. @api.model
  113. def check_user_values(self, values):
  114. if any(field in values for field in [
  115. 'parent_group', 'groups', 'explicit_users']):
  116. return True
  117. return False
  118. @api.multi
  119. @api.returns('res.users')
  120. def get_users(self):
  121. self.ensure_one()
  122. users = self.env['res.users']
  123. if self.parent_group:
  124. users |= self.parent_group.users
  125. users |= self.groups.mapped('users')
  126. users |= self.explicit_users
  127. return users
  128. #----------------------------------------------------------
  129. # Read, View
  130. #----------------------------------------------------------
  131. @api.multi
  132. def _compute_users(self):
  133. for record in self:
  134. record.users = record.get_users()
  135. @api.depends('users')
  136. def _compute_count_users(self):
  137. for record in self:
  138. record.count_users = len(record.users)
  139. #----------------------------------------------------------
  140. # Create, Write, Delete
  141. #----------------------------------------------------------
  142. @api.multi
  143. def _check_recomputation(self, vals, olds, *largs, **kwargs):
  144. super(Groups, self)._check_recomputation(vals, olds, *largs, **kwargs)
  145. fields = []
  146. if self.check_user_values(vals):
  147. fields.extend(['users'])
  148. if fields:
  149. self.trigger_computation(fields)
  150. #----------------------------------------------------------
  151. # Cron Job Functions
  152. #----------------------------------------------------------
  153. @api.model
  154. def update_groups(self, *args, **kwargs):
  155. self.search([]).trigger_computation(['users'])