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:
parent
1d77eb293e
commit
c76a061740
@ -11,20 +11,26 @@ from django.contrib.auth.models import User
|
|||||||
from django.db.models import QuerySet, Q
|
from django.db.models import QuerySet, Q
|
||||||
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
from intervention.forms import DummyFilterInput
|
||||||
from intervention.models import Intervention
|
from intervention.models import Intervention
|
||||||
|
|
||||||
|
|
||||||
class InterventionTableFilter(django_filters.FilterSet):
|
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(
|
sa = django_filters.BooleanFilter(
|
||||||
method='_filter_show_all',
|
method='_filter_show_all',
|
||||||
label=_("Show unshared"),
|
label=_("Show unshared"),
|
||||||
label_suffix=_(""),
|
label_suffix=_(""),
|
||||||
widget=forms.CheckboxInput()
|
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(
|
sr = django_filters.BooleanFilter(
|
||||||
method='_filter_show_recorded',
|
method='_filter_show_recorded',
|
||||||
label=_("Show recorded"),
|
label=_("Show recorded"),
|
||||||
|
@ -218,3 +218,13 @@ class OpenInterventionForm(EditInterventionForm):
|
|||||||
# Disable all form fields
|
# Disable all form fields
|
||||||
for field in self.fields:
|
for field in self.fields:
|
||||||
self.disable_form_field(field)
|
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"
|
@ -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 %}
|
@ -1,5 +1,94 @@
|
|||||||
{% extends 'base.html' %}
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% load django_tables2 %}
|
||||||
|
{% load i18n static fontawesome_5 %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
{% include 'table.html' %}
|
<div class="col-md">
|
||||||
|
{% if table.title is not None %}
|
||||||
|
<div class="row">
|
||||||
|
<h3>
|
||||||
|
{{ table.title }}
|
||||||
|
</h3>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
<div class="row">
|
||||||
|
{% if table.add_new_entries %}
|
||||||
|
<div class="col-md">
|
||||||
|
<a href="{{ table.add_new_url }}">
|
||||||
|
<button class="btn btn-default" title="{% trans 'New entry' %}">
|
||||||
|
<i class="fas fa-plus mr-2"></i>
|
||||||
|
{% trans 'New' %}
|
||||||
|
</button>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% comment %}
|
||||||
|
Search input and rpp selector
|
||||||
|
{% endcomment %}
|
||||||
|
<form method="get">
|
||||||
|
<div class="row my-1">
|
||||||
|
<div class="col-sm-12 col-md-8 col-lg-6">
|
||||||
|
<div class="input-group">
|
||||||
|
<input id="id_{{table.filter.filters.q.field_name}}" name="{{table.filter.filters.q.field_name}}" type="text" class="form-control" aria-label="{% trans 'Search for keywords' %}" placeholder="{% trans 'Search' %}" value="{{ request.GET.q }}">
|
||||||
|
<div class="input-group-append" title="{% trans 'Start search' %}">
|
||||||
|
<button type="submit" class="btn btn-default input-group-text">
|
||||||
|
<span class="">
|
||||||
|
{% fa5_icon 'search' %}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md d-flex justify-content-end">
|
||||||
|
<div class="dropdown show">
|
||||||
|
<div class="btn btn-default dropdown-toggle" href="#" role="button" id="rppToggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||||
|
{% trans 'Results per page' %}
|
||||||
|
</div>
|
||||||
|
<div class="dropdown-menu dropdown-menu-right">
|
||||||
|
{% for rpp_option in table.results_per_page_choices %}
|
||||||
|
<a class="dropdown-item {% if table.results_per_page_chosen == rpp_option %}selected{% endif %}" href="{% querystring table.results_per_page_parameter=rpp_option %}">
|
||||||
|
{{ rpp_option }}
|
||||||
|
</a>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
{% comment %}
|
||||||
|
Filter section
|
||||||
|
{% endcomment %}
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md">
|
||||||
|
<div class="card">
|
||||||
|
<div id="filterHeader" class="card-header cursor-pointer" data-toggle="collapse" data-target="#filter" aria-expanded="true" aria-controls="filter">
|
||||||
|
<h5>
|
||||||
|
{% fa5_icon 'filter' %}
|
||||||
|
{% trans 'Filter' %}
|
||||||
|
</h5>
|
||||||
|
</div>
|
||||||
|
<div id="filter" class="collapse" aria-labelledby="filterHeader">
|
||||||
|
<div class="card-body">
|
||||||
|
{{ table.filter.form.as_p }}
|
||||||
|
<button class="btn btn-default" title="{% trans 'Filter' %}">
|
||||||
|
{% fa5_icon 'filter' %}
|
||||||
|
{% trans 'Apply filter' %}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% comment %}
|
||||||
|
Table section
|
||||||
|
{% endcomment %}
|
||||||
|
{% render_table table %}
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
@ -1,89 +0,0 @@
|
|||||||
{% load django_tables2 %}
|
|
||||||
{% load i18n static fontawesome_5 %}
|
|
||||||
<div class="col-md">
|
|
||||||
{% if table.title is not None %}
|
|
||||||
<div class="row">
|
|
||||||
<h3>
|
|
||||||
{{ table.title }}
|
|
||||||
</h3>
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
<div class="row">
|
|
||||||
{% if table.add_new_entries %}
|
|
||||||
<div class="col-md">
|
|
||||||
<a href="{{ table.add_new_url }}">
|
|
||||||
<button class="btn btn-default" title="{% trans 'New entry' %}">
|
|
||||||
<i class="fas fa-plus mr-2"></i>
|
|
||||||
{% trans 'New' %}
|
|
||||||
</button>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{% comment %}
|
|
||||||
Search input and rpp selector
|
|
||||||
{% endcomment %}
|
|
||||||
<div class="row my-1">
|
|
||||||
<div class="col-sm-12 col-md-8 col-lg-6">
|
|
||||||
<form method="get">
|
|
||||||
<div class="input-group">
|
|
||||||
<input id="id_q" name="q" type="text" class="form-control" aria-label="{% trans 'Search for keywords' %}" placeholder="{% trans 'Search' %}" value="{{ request.GET.q }}">
|
|
||||||
<div class="input-group-append" title="{% trans 'Start search' %}">
|
|
||||||
<button type="submit" class="btn btn-default input-group-text">
|
|
||||||
<span class="">
|
|
||||||
{% fa5_icon 'search' %}
|
|
||||||
</span>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
<div class="col-md d-flex justify-content-end">
|
|
||||||
<div class="dropdown show">
|
|
||||||
<div class="btn btn-default dropdown-toggle" href="#" role="button" id="rppToggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
|
||||||
{% trans 'Results per page' %}
|
|
||||||
</div>
|
|
||||||
<div class="dropdown-menu dropdown-menu-right">
|
|
||||||
{% for rpp_option in table.results_per_page_choices %}
|
|
||||||
<a class="dropdown-item {% if table.results_per_page_chosen == rpp_option %}selected{% endif %}" href="{% querystring table.results_per_page_parameter=rpp_option %}">
|
|
||||||
{{ rpp_option }}
|
|
||||||
</a>
|
|
||||||
{% endfor %}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
{% comment %}
|
|
||||||
Filter section
|
|
||||||
{% endcomment %}
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-md">
|
|
||||||
<div class="card">
|
|
||||||
<div id="filterHeader" class="card-header cursor-pointer" data-toggle="collapse" data-target="#filter" aria-expanded="true" aria-controls="filter">
|
|
||||||
<h5>
|
|
||||||
{% fa5_icon 'filter' %}
|
|
||||||
{% trans 'Filter' %}
|
|
||||||
</h5>
|
|
||||||
</div>
|
|
||||||
<div id="filter" class="collapse" aria-labelledby="filterHeader">
|
|
||||||
<div class="card-body">
|
|
||||||
<form method="get">
|
|
||||||
{{ table.filter.form.as_p }}
|
|
||||||
<button class="btn btn-default" title="{% trans 'Filter' %}">
|
|
||||||
{% fa5_icon 'filter' %}
|
|
||||||
{% trans 'Apply filter' %}
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{% comment %}
|
|
||||||
Table section
|
|
||||||
{% endcomment %}
|
|
||||||
{% render_table table %}
|
|
Loading…
Reference in New Issue
Block a user