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.

109 lines
4.1 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. import json
  20. import operator
  21. import functools
  22. import collections
  23. from odoo import models, fields, api
  24. class Hierarchy(models.AbstractModel):
  25. _name = 'muk_utils.mixins.hierarchy'
  26. _description = 'Hierarchy Mixin'
  27. _parent_store = True
  28. _parent_path_sudo = False
  29. #----------------------------------------------------------
  30. # Database
  31. #----------------------------------------------------------
  32. parent_path = fields.Char(
  33. string="Parent Path",
  34. index=True)
  35. @api.model
  36. def _add_magic_fields(self):
  37. super(Hierarchy, self)._add_magic_fields()
  38. def add(name, field):
  39. if name not in self._fields:
  40. self._add_field(name, field)
  41. add('parent_path_names', fields.Char(
  42. _module=self._module,
  43. compute='_compute_parent_path',
  44. compute_sudo=self._parent_path_sudo,
  45. string="Path Names",
  46. readonly=True,
  47. store=True,
  48. automatic=True))
  49. add('parent_path_json', fields.Text(
  50. _module=self._module,
  51. compute='_compute_parent_path',
  52. compute_sudo=self._parent_path_sudo,
  53. string="Path Json",
  54. readonly=True,
  55. store=True,
  56. automatic=True))
  57. #----------------------------------------------------------
  58. # Read, View
  59. #----------------------------------------------------------
  60. @api.depends('parent_path')
  61. def _compute_parent_path(self):
  62. paths = [list(map(int, rec.parent_path.split('/')[:-1])) for rec in self]
  63. ids = set(functools.reduce(operator.concat, paths))
  64. data = dict(self.browse(ids)._filter_access('read').name_get())
  65. for record in self:
  66. path_names = [""]
  67. path_json = []
  68. for id in reversed(list(map(int, record.parent_path.split('/')[:-1]))):
  69. if id not in data:
  70. break
  71. path_names.append(data[id])
  72. path_json.append({
  73. 'model': record._name,
  74. 'name': data[id],
  75. 'id': record.id,
  76. })
  77. path_names.reverse()
  78. path_json.reverse()
  79. record.update({
  80. 'parent_path_names': '/'.join(path_names),
  81. 'parent_path_json': json.dumps(path_json),
  82. })
  83. #----------------------------------------------------------
  84. # Create, Update, Delete
  85. #----------------------------------------------------------
  86. @api.multi
  87. def write(self, vals):
  88. if self._rec_name_fallback() in vals:
  89. with self.env.norecompute():
  90. res = super(Hierarchy, self).write(vals)
  91. domain = [('id', 'child_of', self.ids)]
  92. records = self.sudo().search(domain)
  93. records.modified(['parent_path'])
  94. if self.env.recompute and self.env.context.get('recompute', True):
  95. records.recompute()
  96. return res
  97. return super(Hierarchy, self).write(vals)