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..f447545 --- /dev/null +++ b/helpdesk/helpdesk/templatetags/filter_helpers.py @@ -0,0 +1,42 @@ +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() + + +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") +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 = { + ( + 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() + } + + 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..a7e3196 --- /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 %} diff --git a/helpdesk/templates/teams/team_detail_tickets.html b/helpdesk/templates/teams/team_detail_tickets.html index 9cc27b9..b01080e 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 filter %} +
+
+ {% 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..bfcb718 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 filter %} +
+
+ {% 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..b055cbb 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 filter %}
{% render_table table %}
diff --git a/helpdesk/templates/tickets/ticketqueue_detail.html b/helpdesk/templates/tickets/ticketqueue_detail.html index dcd89ff..a1d0980 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 filter %}

{{ object.description }}

{% render_table table %}
diff --git a/helpdesk/templates/tickets/tickets_all.html b/helpdesk/templates/tickets/tickets_all.html index b769558..1a66b72 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 filter %}
{% render_table table %}