From d09170ed3848089c3fc4db741b86a15d442d66b2 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 7 Apr 2026 20:42:37 +0200 Subject: ⚡ hierarchical type: improve full label - prepare for hierarchical cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/models_common.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index 0161aadad..d10c04d38 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -803,14 +803,26 @@ class HierarchicalType(GeneralType): has_full_label = True def full_label(self): - lbls = [self.label] - item = self - parents = [self.pk] # prevent loop - while item.parent and item.parent_id not in parents: - parents.append(item.parent_id) - item = item.parent - lbls.append(item.label) - return " > ".join(reversed(lbls)) + return " > ".join(reversed(self.get_up_label_hierarchy())) + + def _get_up_label_hierarchy(self, labels, pk, parents): + if not pk or pk in parents: + return + parents.add(pk) + q = self.__class__.objects.filter(pk=pk) + label, parent_id = q.values_list("label", "parent_id").all()[0] + labels.append(label) + self._get_label_hierarchy(labels, parent_id, parents) + return labels + + def get_label_up_hierarchy(self): + """ + Get every upward labels of the hierarchy for full label or + cached hierarchical search. + Returned in reversed order. + """ + parents = set((self.pk,)) # prevent loop + return self._get_label_hierarchy([self.label], self.parent_id, parents) @property def first_parent(self): @@ -1681,12 +1693,14 @@ class BaseHistorizedItem( All historized items are searchable and have a data json field. Historized items can be "locked" for edition. Historized items can have associated ishtar user for permission management + Cached hierarchy improve performance for hierarchal types """ IS_BASKET = False EXTERNAL_ID_KEY = "" EXTERNAL_ID_DEPENDENCIES = [] HISTORICAL_M2M = [] + CACHED_HIERARCHY = [] history_modifier = models.ForeignKey( User, -- cgit v1.2.3