From a0f5e1dbbc58043b1b0f35cd105559c28cfbaf85 Mon Sep 17 00:00:00 2001 From: Dan Trickey Date: Mon, 8 Apr 2024 19:12:31 +0100 Subject: [PATCH 1/2] Add badges showing current filters for tables --- helpdesk/helpdesk/settings.py | 1 + helpdesk/helpdesk/templatetags/__init__.py | 0 .../helpdesk/templatetags/filter_helpers.py | 36 +++++++++++++++++++ .../templates/inc/tags/filter_badges.html | 14 ++++++++ .../templates/teams/team_detail_tickets.html | 10 ++++-- helpdesk/templates/teams/team_filter.html | 10 ++++-- .../tickets/ticketqueue_assigned.html | 2 ++ .../templates/tickets/ticketqueue_detail.html | 2 ++ helpdesk/templates/tickets/tickets_all.html | 2 ++ 9 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 helpdesk/helpdesk/templatetags/__init__.py create mode 100644 helpdesk/helpdesk/templatetags/filter_helpers.py create mode 100644 helpdesk/templates/inc/tags/filter_badges.html diff --git a/helpdesk/helpdesk/settings.py b/helpdesk/helpdesk/settings.py index 952587d..8febbf9 100644 --- a/helpdesk/helpdesk/settings.py +++ b/helpdesk/helpdesk/settings.py @@ -94,6 +94,7 @@ INSTALLED_APPS = [ "accounts", "display", + "helpdesk", "teams", "tickets", "crispy_forms", diff --git a/helpdesk/helpdesk/templatetags/__init__.py b/helpdesk/helpdesk/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/helpdesk/helpdesk/templatetags/filter_helpers.py b/helpdesk/helpdesk/templatetags/filter_helpers.py new file mode 100644 index 0000000..d0f9a82 --- /dev/null +++ b/helpdesk/helpdesk/templatetags/filter_helpers.py @@ -0,0 +1,36 @@ +from typing import Any + +from django import template +from django.forms import Field +from django_filters.fields import ChoiceField + +register = template.Library() + + +def _get_value_display_for_choices(choice_field: ChoiceField, value: Any) -> Any: + choices = dict(choice_field.choices.choices) + return choices.get(value, value) + + +def _get_value_display_for_field(field: Field, value: Any) -> Any: + if isinstance(field, ChoiceField): + return _get_value_display_for_choices(field, value) + + 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 != ""} + else: + cleaned_data = {} + + filter_badges = { + (fltr.form.fields[field_name].label, _get_value_display_for_field(fltr.form.fields[field_name], value)) + for field_name, value in cleaned_data.items() + } + + return {"filter_badges": filter_badges} diff --git a/helpdesk/templates/inc/tags/filter_badges.html b/helpdesk/templates/inc/tags/filter_badges.html new file mode 100644 index 0000000..4016bf0 --- /dev/null +++ b/helpdesk/templates/inc/tags/filter_badges.html @@ -0,0 +1,14 @@ +{# render using the render_filter_badges templatetag #} +{% if filter_badges %} +
+ {% for label, value in filter_badges %} +
+
+ {{ label }} + {{ value }} + +
+
+ {% endfor %} +
+{% endif %} \ No newline at end of file diff --git a/helpdesk/templates/teams/team_detail_tickets.html b/helpdesk/templates/teams/team_detail_tickets.html index 9cc27b9..f44503e 100644 --- a/helpdesk/templates/teams/team_detail_tickets.html +++ b/helpdesk/templates/teams/team_detail_tickets.html @@ -1,6 +1,7 @@ {% extends "layouts/base_app.html" %} {% load render_table from django_tables2 %} {% load crispy_forms_tags %} +{% load filter_helpers %} {% block page_title %}{{object.tla}}: {{ object.name }}{% endblock %} {% block title %}{{object.tla}}: {{ object.name }}{% endblock %} @@ -14,9 +15,12 @@ {% block content %} {% include "inc/nav/team-tabs.html" with active="tickets" team=object %} -
-
- {% render_table table %} +
+ {% render_filter_badges %} +
+
+ {% render_table table %} +
{% include "inc/modals/filter.html" %} diff --git a/helpdesk/templates/teams/team_filter.html b/helpdesk/templates/teams/team_filter.html index e9b949c..82181bd 100644 --- a/helpdesk/templates/teams/team_filter.html +++ b/helpdesk/templates/teams/team_filter.html @@ -1,6 +1,7 @@ {% extends "layouts/base_app.html" %} {% load render_table from django_tables2 %} {% load crispy_forms_tags %} +{% load filter_helpers %} {% block page_title %}Teams{% endblock %} {% block title %}Teams{% endblock %} @@ -10,9 +11,12 @@ {% endblock %} {% block content %} -
-
- {% render_table table %} +
+ {% render_filter_badges %} +
+
+ {% render_table table %} +
{% include "inc/modals/filter.html" %} diff --git a/helpdesk/templates/tickets/ticketqueue_assigned.html b/helpdesk/templates/tickets/ticketqueue_assigned.html index 0a0522b..6df021a 100644 --- a/helpdesk/templates/tickets/ticketqueue_assigned.html +++ b/helpdesk/templates/tickets/ticketqueue_assigned.html @@ -1,5 +1,6 @@ {% extends "layouts/base_app.html" %} {% load render_table from django_tables2 %} +{% load filter_helpers %} {% block page_title %}My Tickets{% endblock %} {% block title %}My Tickets{% endblock %} @@ -10,6 +11,7 @@ {% block content %}
+ {% render_filter_badges %}
{% render_table table %}
diff --git a/helpdesk/templates/tickets/ticketqueue_detail.html b/helpdesk/templates/tickets/ticketqueue_detail.html index dcd89ff..a04783d 100644 --- a/helpdesk/templates/tickets/ticketqueue_detail.html +++ b/helpdesk/templates/tickets/ticketqueue_detail.html @@ -1,5 +1,6 @@ {% extends "layouts/base_app.html" %} {% load render_table from django_tables2 %} +{% load filter_helpers %} {% block page_title %}Queue: {{ object.name }}{% endblock %} {% block title %}Queue: {{ object.name }}{% endblock %} @@ -38,6 +39,7 @@
+ {% render_filter_badges %}

{{ object.description }}

{% render_table table %}
diff --git a/helpdesk/templates/tickets/tickets_all.html b/helpdesk/templates/tickets/tickets_all.html index b769558..163f9b4 100644 --- a/helpdesk/templates/tickets/tickets_all.html +++ b/helpdesk/templates/tickets/tickets_all.html @@ -1,5 +1,6 @@ {% extends "layouts/base_app.html" %} {% load render_table from django_tables2 %} +{% load filter_helpers %} {% block page_title %}All Tickets{% endblock %} {% block title %}All Tickets{% endblock %} @@ -13,6 +14,7 @@ {% block content %}
+ {% render_filter_badges %}
{% render_table table %}
From 3f454f178ccd34be6e6f3c803e6435e19517ff08 Mon Sep 17 00:00:00 2001 From: Dan Trickey Date: Tue, 9 Apr 2024 19:52:49 +0100 Subject: [PATCH 2/2] 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 %}