From 0992ff6c678b07b6aa661ef8460385a8916289f2 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Tue, 4 Jul 2023 18:35:56 +0200 Subject: [PATCH] add clear filters button on prod stream search --- scipost_django/production/forms.py | 12 ++++++++++++ .../_hx_productionstream_search_form.html | 10 ++++++++++ .../templates/production/production_new.html | 18 +++++++++--------- scipost_django/production/urls.py | 5 +++++ scipost_django/production/views.py | 16 ++++++++++++++++ 5 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 scipost_django/production/templates/production/_hx_productionstream_search_form.html diff --git a/scipost_django/production/forms.py b/scipost_django/production/forms.py index 79862d403..c944cc1f8 100644 --- a/scipost_django/production/forms.py +++ b/scipost_django/production/forms.py @@ -3,6 +3,7 @@ __license__ = "AGPL v3" import datetime +from typing import Dict from django import forms from django.contrib.auth import get_user_model @@ -407,6 +408,17 @@ class ProductionStreamSearchForm(forms.Form): ), ) + def apply_filter_set(self, filters: Dict, none_on_empty: bool = False): + # Apply the filter set to the form + for key in self.fields: + if key in filters: + self.fields[key].initial = filters[key] + elif none_on_empty: + if isinstance(self.fields[key], forms.MultipleChoiceField): + self.fields[key].initial = [] + else: + self.fields[key].initial = None + def search_results(self): # Save the form data to the session if self.session_key is not None: diff --git a/scipost_django/production/templates/production/_hx_productionstream_search_form.html b/scipost_django/production/templates/production/_hx_productionstream_search_form.html new file mode 100644 index 000000000..177079afd --- /dev/null +++ b/scipost_django/production/templates/production/_hx_productionstream_search_form.html @@ -0,0 +1,10 @@ +{% load crispy_forms_tags %} + +<form hx-post="{% url 'production:_hx_productionstream_list' %}" + hx-trigger="load, keyup delay:500ms, change delay:500ms, click from:#refresh-button" + hx-sync="#search-productionstreams-form:replace" + hx-target="#search-productionstreams-results" + hx-indicator="#indicator-search-productionstreams"> + + <div id="search-productionstreams-form">{% crispy form %}</div> +</form> diff --git a/scipost_django/production/templates/production/production_new.html b/scipost_django/production/templates/production/production_new.html index 388326ba4..b78a7d3a9 100644 --- a/scipost_django/production/templates/production/production_new.html +++ b/scipost_django/production/templates/production/production_new.html @@ -28,8 +28,8 @@ <summary class="card-header fs-6 d-inline-flex align-items-center"> Search / Filter / Bulk Actions <div class="d-none d-sm-inline-flex ms-auto align-items-center"> + <div id="indicator-search-productionstreams" class="htmx-indicator"> - <button class="btn btn-warning text-white d-none d-md-block me-2" type="button" disabled> @@ -38,10 +38,14 @@ <div class="spinner-grow spinner-grow-sm ms-2" role="status" aria-hidden="true"></div> - </button> </div> + <button class="btn btn-outline-secondary me-2" + type="button" + hx-get="{% url 'production:_hx_productionstream_search_form' filter_set="empty" %}" + hx-target="#productionstream-search-form-container">Clear Filters</button> + <a id="refresh-button" class="m-2 btn btn-primary"> {% include "bi/arrow-clockwise.html" %} Refresh</a> @@ -49,13 +53,9 @@ </summary> <div class="card-body"> - <form hx-post="{% url 'production:_hx_productionstream_list' %}" - hx-trigger="load, keyup delay:500ms, change, click from:#refresh-button" - hx-target="#search-productionstreams-results" - hx-indicator="#indicator-search-productionstreams"> - - <div id="search-productionstreams-form">{% crispy search_productionstreams_form %}</div> - </form> + <div id="productionstream-search-form-container"> + {% include 'production/_hx_productionstream_search_form.html' with form=search_productionstreams_form %} + </div> {% comment %} Bulk Action buttons {% endcomment %} diff --git a/scipost_django/production/urls.py b/scipost_django/production/urls.py index 81e04bc2e..39a748173 100644 --- a/scipost_django/production/urls.py +++ b/scipost_django/production/urls.py @@ -47,6 +47,11 @@ urlpatterns = [ production_views._hx_productionstream_list, name="_hx_productionstream_list", ), + path( + "_hx_productionstream_search_form/<str:filter_set>", + production_views._hx_productionstream_search_form, + name="_hx_productionstream_search_form", + ), path( "_hx_productionstream_actions_bulk_assign_officers", production_views._hx_productionstream_actions_bulk_assign_officers, diff --git a/scipost_django/production/views.py b/scipost_django/production/views.py index 679b57708..c8fc4bf24 100644 --- a/scipost_django/production/views.py +++ b/scipost_django/production/views.py @@ -1481,6 +1481,22 @@ def _hx_productionstream_summary_assignees_status(request, productionstream_id): ) +def _hx_productionstream_search_form(request, filter_set: str): + productionstream_search_form = ProductionStreamSearchForm( + user=request.user, + session_key=request.session.session_key, + ) + + if filter_set == "empty": + productionstream_search_form.apply_filter_set({}, none_on_empty=True) + # TODO: add more filter sets saved in the session of the user + + context = { + "form": productionstream_search_form, + } + return render(request, "production/_hx_productionstream_search_form.html", context) + + def _hx_event_list(request, productionstream_id): productionstream = get_object_or_404(ProductionStream, pk=productionstream_id) -- GitLab