From 7946a926b4ce311ce217667d6ca32a1ad61e1e62 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 17 Feb 2026 15:57:37 +0100 Subject: ⚡ templates: `list_filter` custom tag - improve performance for json_sections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/models_common.py | 8 ++++++++ ishtar_common/templatetags/ishtar_helpers.py | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index a94eadb80..10c564024 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -1339,6 +1339,8 @@ class JsonData(models.Model, CachedGen): @property def json_sections(self): + if getattr(self, "__json_sections", None): + return self.__json_sections sections = [] try: content_type = ContentType.objects.get_for_model(self) @@ -1382,6 +1384,12 @@ class JsonData(models.Model, CachedGen): # if section name is identical it is the same sections.append((section_name, [])) sections[-1][1].append((field.name, value)) + # clean + sections = [ + (label or "", section) for label, section in sections + if any(1 for __, value in section if value) + ] + self.__json_sections = sections return sections @classmethod diff --git a/ishtar_common/templatetags/ishtar_helpers.py b/ishtar_common/templatetags/ishtar_helpers.py index 8c0c30761..de1d55d2e 100644 --- a/ishtar_common/templatetags/ishtar_helpers.py +++ b/ishtar_common/templatetags/ishtar_helpers.py @@ -142,3 +142,28 @@ def limit_label(value, limit=120): return mark_safe( f'{value[:(limit - 4)]} (...)' ) + + +@register.simple_tag +def list_filter(lst, filter_value, item_number=None): + """ + Filter a list (`lst`) by item containing `filter_value`. + If `filter_value` begin with "-:" filter by item NOT containing `filter_value`. + If `item_number` (int) is provided, the list contains a list and filter is made on the + `item_number` value of this list + """ + reverse = False + if filter_value.startswith("-:"): + filter_value = filter_value[2:] + reverse = True + if not reverse: + if item_number is not None: + cmp = lambda x: x and x[item_number] and filter_value in x[item_number] + else: + cmp = lambda x: x and filter_value in x + else: + if item_number is not None: + cmp = lambda x: not x or not x[item_number] or filter_value not in x[item_number] + else: + cmp = lambda x: not x or filter_value not in x + return [item for item in lst if cmp(item)] -- cgit v1.2.3