From 6914202cb0b9c82bedba2cb55da18916ac9c5648 Mon Sep 17 00:00:00 2001 From: MuK IT GmbH Date: Thu, 3 Jan 2019 02:48:24 +0000 Subject: [PATCH] publish muk_utils - 12.0 --- muk_utils/__manifest__.py | 2 +- muk_utils/models/__init__.py | 3 +- muk_utils/models/mixins_hierarchy.py | 94 ++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 muk_utils/models/mixins_hierarchy.py diff --git a/muk_utils/__manifest__.py b/muk_utils/__manifest__.py index ac9b6c8..f608b43 100644 --- a/muk_utils/__manifest__.py +++ b/muk_utils/__manifest__.py @@ -19,7 +19,7 @@ { "name": "MuK Utils", "summary": """Utility Features""", - "version": '12.0.1.1.16', + "version": '12.0.1.1.17', "category": 'Extra Tools', "license": "AGPL-3", "author": "MuK IT", diff --git a/muk_utils/models/__init__.py b/muk_utils/models/__init__.py index a7ca9b8..896072c 100644 --- a/muk_utils/models/__init__.py +++ b/muk_utils/models/__init__.py @@ -19,8 +19,9 @@ from . import base from . import mixins_groups -from . import ir_attachment +from . import mixins_hierarchy from . import scss_editor +from . import ir_attachment from . import ir_config_parameter from . import res_config_settings diff --git a/muk_utils/models/mixins_hierarchy.py b/muk_utils/models/mixins_hierarchy.py new file mode 100644 index 0000000..dc6d475 --- /dev/null +++ b/muk_utils/models/mixins_hierarchy.py @@ -0,0 +1,94 @@ +################################################################################### +# +# 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 . +# +################################################################################### + +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), + }) + \ No newline at end of file