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.

93 lines
3.4 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.Char(
  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. recs = self.with_context(prefetch_fields=False).browse(ids)
  65. data = {rec.id: (rec._name, rec.name) for rec in recs}
  66. for record in self:
  67. path_names = []
  68. path_json = []
  69. for id in reversed(list(map(int, record.parent_path.split('/')[:-1]))):
  70. if id not in data:
  71. break
  72. path_names.append(data[id][1])
  73. path_json.append({
  74. 'model': data[id][0],
  75. 'name': data[id][1],
  76. 'id': id,
  77. })
  78. path_names.reverse()
  79. path_json.reverse()
  80. record.update({
  81. 'parent_path_names': '/'.join(path_names),
  82. 'parent_path_json': json.dumps(path_json),
  83. })