From 3f454f178ccd34be6e6f3c803e6435e19517ff08 Mon Sep 17 00:00:00 2001 From: Dan Trickey Date: Tue, 9 Apr 2024 19:52:49 +0100 Subject: [PATCH] Pass filterset into render_filter_badges explicitly --- .../helpdesk/templatetags/filter_helpers.py | 22 ++++++++++++------- .../templates/inc/tags/filter_badges.html | 2 +- .../templates/teams/team_detail_tickets.html | 2 +- helpdesk/templates/teams/team_filter.html | 2 +- .../tickets/ticketqueue_assigned.html | 2 +- .../templates/tickets/ticketqueue_detail.html | 2 +- helpdesk/templates/tickets/tickets_all.html | 2 +- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/helpdesk/helpdesk/templatetags/filter_helpers.py b/helpdesk/helpdesk/templatetags/filter_helpers.py index d0f9a82..f447545 100644 --- a/helpdesk/helpdesk/templatetags/filter_helpers.py +++ b/helpdesk/helpdesk/templatetags/filter_helpers.py @@ -1,9 +1,14 @@ -from typing import Any +from __future__ import annotations + +from typing import TYPE_CHECKING, Any from django import template from django.forms import Field from django_filters.fields import ChoiceField +if TYPE_CHECKING: + from django_filters import FilterSet + register = template.Library() @@ -19,17 +24,18 @@ def _get_value_display_for_field(field: Field, value: Any) -> Any: return value -@register.inclusion_tag("inc/tags/filter_badges.html", takes_context=True) -def render_filter_badges(context: dict[str, Any]) -> dict[str, Any]: - fltr = context["filter"] - - if fltr.is_valid(): - cleaned_data = {k: v for k, v in fltr.form.cleaned_data.items() if v is not None and v != ""} +@register.inclusion_tag("inc/tags/filter_badges.html") +def render_filter_badges(filterset: FilterSet) -> dict[str, Any]: + if filterset.is_valid(): # type: ignore[no-untyped-call] + cleaned_data = {k: v for k, v in filterset.form.cleaned_data.items() if v is not None and v != ""} else: cleaned_data = {} filter_badges = { - (fltr.form.fields[field_name].label, _get_value_display_for_field(fltr.form.fields[field_name], value)) + ( + filterset.form.fields[field_name].label, + _get_value_display_for_field(filterset.form.fields[field_name], value), + ) for field_name, value in cleaned_data.items() } diff --git a/helpdesk/templates/inc/tags/filter_badges.html b/helpdesk/templates/inc/tags/filter_badges.html index 4016bf0..a7e3196 100644 --- a/helpdesk/templates/inc/tags/filter_badges.html +++ b/helpdesk/templates/inc/tags/filter_badges.html @@ -11,4 +11,4 @@ {% endfor %} -{% endif %} \ No newline at end of file +{% endif %} diff --git a/helpdesk/templates/teams/team_detail_tickets.html b/helpdesk/templates/teams/team_detail_tickets.html index f44503e..b01080e 100644 --- a/helpdesk/templates/teams/team_detail_tickets.html +++ b/helpdesk/templates/teams/team_detail_tickets.html @@ -16,7 +16,7 @@ {% block content %} {% include "inc/nav/team-tabs.html" with active="tickets" team=object %}
- {% render_filter_badges %} + {% render_filter_badges filter %}
{% render_table table %} diff --git a/helpdesk/templates/teams/team_filter.html b/helpdesk/templates/teams/team_filter.html index 82181bd..bfcb718 100644 --- a/helpdesk/templates/teams/team_filter.html +++ b/helpdesk/templates/teams/team_filter.html @@ -12,7 +12,7 @@ {% block content %}
- {% render_filter_badges %} + {% render_filter_badges filter %}
{% render_table table %} diff --git a/helpdesk/templates/tickets/ticketqueue_assigned.html b/helpdesk/templates/tickets/ticketqueue_assigned.html index 6df021a..b055cbb 100644 --- a/helpdesk/templates/tickets/ticketqueue_assigned.html +++ b/helpdesk/templates/tickets/ticketqueue_assigned.html @@ -11,7 +11,7 @@ {% block content %}
- {% render_filter_badges %} + {% render_filter_badges filter %}
{% render_table table %}
diff --git a/helpdesk/templates/tickets/ticketqueue_detail.html b/helpdesk/templates/tickets/ticketqueue_detail.html index a04783d..a1d0980 100644 --- a/helpdesk/templates/tickets/ticketqueue_detail.html +++ b/helpdesk/templates/tickets/ticketqueue_detail.html @@ -39,7 +39,7 @@
- {% render_filter_badges %} + {% render_filter_badges filter %}

{{ object.description }}

{% render_table table %}
diff --git a/helpdesk/templates/tickets/tickets_all.html b/helpdesk/templates/tickets/tickets_all.html index 163f9b4..1a66b72 100644 --- a/helpdesk/templates/tickets/tickets_all.html +++ b/helpdesk/templates/tickets/tickets_all.html @@ -14,7 +14,7 @@ {% block content %}
- {% render_filter_badges %} + {% render_filter_badges filter %}
{% render_table table %}