Template improvements

* refactors templates/table.html into templates/generic_index.html
* adds DummyFilterInput as do-not-render-widget
* hardens combination of q filter and other filters on index view rendering
This commit is contained in:
mipel
2021-07-28 09:53:14 +02:00
parent af6e3e6223
commit 3eb594c1b5
5 changed files with 115 additions and 94 deletions

View File

@@ -11,20 +11,26 @@ from django.contrib.auth.models import User
from django.db.models import QuerySet, Q
from django.utils.translation import gettext_lazy as _
from intervention.forms import DummyFilterInput
from intervention.models import Intervention
class InterventionTableFilter(django_filters.FilterSet):
q = django_filters.Filter(
method='_filter_by_keyword',
widget=forms.HiddenInput(), # use search bar in template, we only need the filter logic in here!
)
sa = django_filters.BooleanFilter(
method='_filter_show_all',
label=_("Show unshared"),
label_suffix=_(""),
widget=forms.CheckboxInput()
)
q = django_filters.Filter(
method='_filter_by_keyword',
# Since we use a custom search bar in the template, we need to 'render' this filter
# as 'anonymous' HiddenInput (no id, no name). This way our custom search bar's id and name won't be
# overwritten with these id and name (which would be equal)
# This way we can use the simple filter method mapping for a parameter without using a predefined widget!
widget=DummyFilterInput(),
)
sr = django_filters.BooleanFilter(
method='_filter_show_recorded',
label=_("Show recorded"),

View File

@@ -218,3 +218,13 @@ class OpenInterventionForm(EditInterventionForm):
# Disable all form fields
for field in self.fields:
self.disable_form_field(field)
class DummyFilterInput(forms.HiddenInput):
""" A dummy input widget
Does not render anything. Can be used to keep filter logic using django_filter without having a pre defined
filter widget being rendered to the template.
"""
template_name = "intervention/dummy-filter-input.html"

View File

@@ -0,0 +1,5 @@
{% comment %}
This is an empty template.
It's used to dismiss a default widget for a django_filter filter and to use a predefined input directly in the
template without losing the comfort of matching the filter methods declared in the filter
{% endcomment %}