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.

94 lines
3.4 KiB

###################################################################################
#
# Copyright (C) 2017 MuK IT GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###################################################################################
import json
import operator
import functools
import collections
from odoo import models, fields, api
class Hierarchy(models.AbstractModel):
_name = 'muk_utils.mixins.hierarchy'
_description = 'Hierarchy Mixin'
_parent_store = True
_parent_path_sudo = False
#----------------------------------------------------------
# Database
#----------------------------------------------------------
parent_path = fields.Char(
string="Parent Path",
index=True)
@api.model
def _add_magic_fields(self):
super(Hierarchy, self)._add_magic_fields()
def add(name, field):
if name not in self._fields:
self._add_field(name, field)
add('parent_path_names', fields.Char(
_module=self._module,
compute='_compute_parent_path',
compute_sudo=self._parent_path_sudo,
string="Path Names",
readonly=True,
store=True,
automatic=True))
add('parent_path_json', fields.Char(
_module=self._module,
compute='_compute_parent_path',
compute_sudo=self._parent_path_sudo,
string="Path Json",
readonly=True,
store=True,
automatic=True))
#----------------------------------------------------------
# Read, View
#----------------------------------------------------------
@api.depends('parent_path')
def _compute_parent_path(self):
paths = [list(map(int, rec.parent_path.split('/')[:-1])) for rec in self]
ids = set(functools.reduce(operator.concat, paths))
recs = self.with_context(prefetch_fields=False).browse(ids)
data = {rec.id: (rec._name, rec.name) for rec in recs}
for record in self:
path_names = []
path_json = []
for id in reversed(list(map(int, record.parent_path.split('/')[:-1]))):
if id not in data:
break
path_names.append(data[id][1])
path_json.append({
'model': data[id][0],
'name': data[id][1],
'id': id,
})
path_names.reverse()
path_json.reverse()
record.update({
'parent_path_names': '/'.join(path_names),
'parent_path_json': json.dumps(path_json),
})