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.

67 lines
2.8 KiB

6 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. import logging
  20. from collections import defaultdict
  21. from odoo import api, fields, models
  22. from odoo import tools, _
  23. from odoo.exceptions import ValidationError
  24. _logger = logging.getLogger(__name__)
  25. class ResGroups(models.Model):
  26. _inherit = "res.groups"
  27. #----------------------------------------------------------
  28. # Create, Update, Delete
  29. #----------------------------------------------------------
  30. @api.multi
  31. def write(self, vals):
  32. model_recs = defaultdict(set)
  33. model_names = self.pool.descendants(['muk_utils.groups'], '_inherit', '_inherits')
  34. if any(field in vals for field in ['users']):
  35. for model_name in model_names:
  36. model = self.env[model_name].sudo()
  37. if not model._abstract:
  38. model_recs[model_name] = model.search([['groups', 'in', self.mapped('id')]])
  39. result = super(ResGroups, self).write(vals)
  40. if any(field in vals for field in ['users']):
  41. for model_name in model_names:
  42. model = self.env[model_name].sudo()
  43. if not model._abstract:
  44. model_recs[model_name] = model_recs[model_name] | model.search([['groups', 'in', self.mapped('id')]])
  45. for tuple in model_recs.items():
  46. tuple[1].trigger_computation(['users'])
  47. return result
  48. @api.multi
  49. def unlink(self):
  50. model_recs = defaultdict(set)
  51. model_names = self.pool.descendants(['muk_utils.groups'], '_inherit', '_inherits')
  52. for model_name in model_names:
  53. model = self.env[model_name].sudo()
  54. if not model._abstract:
  55. model_recs[model_name] = model.search([['groups', 'in', self.mapped('id')]])
  56. result = super(ResGroups, self).unlink(vals)
  57. for tuple in model_recs.items():
  58. tuple[1].trigger_computation(['users'])
  59. return result