From 32b4271ea9e7540a7613429563fffde20e57690d Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 6 Feb 2025 15:24:37 +0100 Subject: 🐛 more robust filters for sheets (refs #6109) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/models_common.py | 2 +- ishtar_common/templates/ishtar/sheet_ishtaruser.html | 2 +- ishtar_common/templatetags/ishtar_helpers.py | 10 +++++++++- ishtar_common/views_item.py | 7 ++++++- 4 files changed, 17 insertions(+), 4 deletions(-) (limited to 'ishtar_common') diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index 4bea24ab5..49c69e9e1 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -881,7 +881,7 @@ class BaseSheetFilter(models.Model): with open(tpl.template.origin.name, "r") as fle: sub_content = fle.read() keys += attrs.findall(sub_content) - return sorted(set(keys)) + return sorted(set([k.replace("_not_available", "") for k in keys])) class SheetFilter(BaseSheetFilter): diff --git a/ishtar_common/templates/ishtar/sheet_ishtaruser.html b/ishtar_common/templates/ishtar/sheet_ishtaruser.html index e30df5278..ce351c720 100644 --- a/ishtar_common/templates/ishtar/sheet_ishtaruser.html +++ b/ishtar_common/templates/ishtar/sheet_ishtaruser.html @@ -69,7 +69,7 @@
{% trans "Profile(s)" %}
{% for profile in item.person.profiles.all %} - {% if forloop.counter0 %}; {% endif %}{{profile}}   + {% if forloop.counter0 %}; {% endif %}{{profile.profile_type}}   {% endfor %} diff --git a/ishtar_common/templatetags/ishtar_helpers.py b/ishtar_common/templatetags/ishtar_helpers.py index bfe7efc2a..cfd04a5d7 100644 --- a/ishtar_common/templatetags/ishtar_helpers.py +++ b/ishtar_common/templatetags/ishtar_helpers.py @@ -51,6 +51,14 @@ def and_(value1, value2): return value1 and value2 +@register.filter +def safe_and_not(item, value): + try: + return item and not getattr(item, value, None) and item + except AttributeError: + return item + + @register.filter def safe_or(item, args): if not item: @@ -72,7 +80,7 @@ def safe_or(item, args): if callable(current_item): result = current_item() if result: - return True + return item return False diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index b30a9e525..cecc0318d 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -446,9 +446,14 @@ def filter_sheet(ishtar_user, item): keys += base_keys if exclude: for key in keys: + param = getattr(item, key, None) + if hasattr(param, "count"): + # set a key_not_available variable in order to filter on the + # sheet non setable variable such as queryset or properties + setattr(item, key + "_not_available", True) try: setattr(item, key, None) - except TypeError: + except (TypeError, AttributeError): pass return item new_item = type("BaseObject", (object,), {}) -- cgit v1.2.3