# 61 Filter backend
* refactors XYTableFilter classes to hold query_filter and checkbox_filter which are separate instances of django_filter.FilterSets * improves render ability for frontend
This commit is contained in:
parent
87f11a9914
commit
8a19d7239d
@ -10,15 +10,11 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.db.models import QuerySet
|
from django.db.models import QuerySet
|
||||||
|
|
||||||
from konova.filters import AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, \
|
from konova.filters import QueryTableFilter, CheckboxTableFilter
|
||||||
RecordableTableFilterMixin
|
|
||||||
|
|
||||||
|
|
||||||
class CompensationTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, RecordableTableFilterMixin):
|
class CheckboxCompensationTableFilter(CheckboxTableFilter):
|
||||||
""" TableFilter for compensations
|
""" Specialization of regular CheckboxTableFilter for compensation model
|
||||||
|
|
||||||
Based widely on InterventionTableFilter.
|
|
||||||
Just some minor changes for Compensation model.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -59,12 +55,36 @@ class CompensationTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin
|
|||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
class EcoAccountTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, RecordableTableFilterMixin):
|
class CompensationTableFilter:
|
||||||
""" TableFilter for eco accounts
|
""" TableFilter for compensations
|
||||||
|
|
||||||
Just some minor changes for EcoAccount model.
|
Based widely on InterventionTableFilter.
|
||||||
|
Just some minor changes for Compensation model.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
query_filter = None
|
||||||
|
checkbox_filter = None
|
||||||
|
qs = None
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
user = kwargs.get("user", None)
|
||||||
|
qs = kwargs.get("queryset", None)
|
||||||
|
request_data = kwargs.get("data", None)
|
||||||
|
|
||||||
|
self.query_filter = QueryTableFilter(
|
||||||
|
user=user,
|
||||||
|
data=request_data,
|
||||||
|
queryset=qs,
|
||||||
|
)
|
||||||
|
self.checkbox_filter = CheckboxCompensationTableFilter(
|
||||||
|
user=user,
|
||||||
|
data=request_data,
|
||||||
|
queryset=self.query_filter.qs,
|
||||||
|
)
|
||||||
|
self.qs = self.checkbox_filter.qs
|
||||||
|
|
||||||
|
|
||||||
|
class CheckboxEcoAccountTableFilter(CheckboxTableFilter):
|
||||||
sr = django_filters.BooleanFilter(
|
sr = django_filters.BooleanFilter(
|
||||||
method='filter_only_show_unrecorded',
|
method='filter_only_show_unrecorded',
|
||||||
label=_("Show only unrecorded"),
|
label=_("Show only unrecorded"),
|
||||||
@ -107,3 +127,29 @@ class EcoAccountTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin,
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
|
class EcoAccountTableFilter:
|
||||||
|
""" TableFilter for eco accounts
|
||||||
|
|
||||||
|
"""
|
||||||
|
query_filter = None
|
||||||
|
checkbox_filter = None
|
||||||
|
qs = None
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
user = kwargs.get("user", None)
|
||||||
|
qs = kwargs.get("queryset", None)
|
||||||
|
request_data = kwargs.get("data", None)
|
||||||
|
|
||||||
|
self.query_filter = QueryTableFilter(
|
||||||
|
user=user,
|
||||||
|
data=request_data,
|
||||||
|
queryset=qs,
|
||||||
|
)
|
||||||
|
self.checkbox_filter = CheckboxEcoAccountTableFilter(
|
||||||
|
user=user,
|
||||||
|
data=request_data,
|
||||||
|
queryset=self.query_filter.qs,
|
||||||
|
)
|
||||||
|
self.qs = self.checkbox_filter.qs
|
||||||
|
@ -5,19 +5,27 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
|||||||
Created on: 22.07.21
|
Created on: 22.07.21
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from intervention.models import Intervention
|
from konova.filters import QueryTableFilter, CheckboxTableFilter
|
||||||
from konova.filters import AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, \
|
|
||||||
RecordableTableFilterMixin
|
|
||||||
|
|
||||||
|
|
||||||
class InterventionTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, RecordableTableFilterMixin):
|
class InterventionTableFilter:
|
||||||
""" TableFilter for Intervention model
|
query_filter = None
|
||||||
|
checkbox_filter = None
|
||||||
"""
|
qs = None
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = Intervention
|
|
||||||
fields = []
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
user = kwargs.get("user", None)
|
||||||
|
qs = kwargs.get("queryset", None)
|
||||||
|
request_data = kwargs.get("data", None)
|
||||||
|
|
||||||
|
self.query_filter = QueryTableFilter(
|
||||||
|
user=user,
|
||||||
|
data=request_data,
|
||||||
|
queryset=qs,
|
||||||
|
)
|
||||||
|
self.checkbox_filter = CheckboxTableFilter(
|
||||||
|
user=user,
|
||||||
|
data=request_data,
|
||||||
|
queryset=self.query_filter.qs,
|
||||||
|
)
|
||||||
|
self.qs = self.checkbox_filter.qs
|
||||||
|
@ -19,8 +19,17 @@ class AbstractTableFilter(django_filters.FilterSet):
|
|||||||
""" TableFilter for Intervention model
|
""" TableFilter for Intervention model
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
def __init__(self, user: User, *args, **kwargs):
|
||||||
|
self.user = user
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class KeywordTableFilterMixin(django_filters.FilterSet):
|
||||||
q = django_filters.Filter(
|
q = django_filters.Filter(
|
||||||
method='_filter_by_keyword',
|
method='filter_by_keyword',
|
||||||
# Since we use a custom search bar in the template, we need to 'render' this filter
|
# 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
|
# 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)
|
# overwritten with these id and name (which would be equal)
|
||||||
@ -31,11 +40,7 @@ class AbstractTableFilter(django_filters.FilterSet):
|
|||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
def __init__(self, user: User, *args, **kwargs):
|
def filter_by_keyword(self, queryset, name, value) -> QuerySet:
|
||||||
self.user = user
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
def _filter_by_keyword(self, queryset, name, value) -> QuerySet:
|
|
||||||
""" Filters queryset depending on value of search bar input
|
""" Filters queryset depending on value of search bar input
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -309,3 +314,17 @@ class RecordableTableFilterMixin(django_filters.FilterSet):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
|
class QueryTableFilter(AbstractTableFilter, KeywordTableFilterMixin, GeoReferencedTableFilterMixin):
|
||||||
|
""" TableFilter holding different filter options for query related filtering
|
||||||
|
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class CheckboxTableFilter(AbstractTableFilter, ShareableTableFilterMixin, RecordableTableFilterMixin):
|
||||||
|
""" TableFilter holding different filter options for checkbox related filtering
|
||||||
|
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
@ -41,7 +41,7 @@
|
|||||||
<div class="row my-1">
|
<div class="row my-1">
|
||||||
<div class="col-sm-12 col-md-8 col-lg-6">
|
<div class="col-sm-12 col-md-8 col-lg-6">
|
||||||
<div class="input-group">
|
<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 }}">
|
<input id="id_{{table.filter.query_filter.filters.q.field_name}}" name="{{table.filter.query_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' %}">
|
<div class="input-group-append" title="{% trans 'Start search' %}">
|
||||||
<button type="submit" class="btn btn-default input-group-text">
|
<button type="submit" class="btn btn-default input-group-text">
|
||||||
<span class="">
|
<span class="">
|
||||||
@ -82,11 +82,18 @@
|
|||||||
</div>
|
</div>
|
||||||
<div id="filter" class="collapse" aria-labelledby="filterHeader">
|
<div id="filter" class="collapse" aria-labelledby="filterHeader">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
{{ table.filter.form.as_p }}
|
<div class="row">
|
||||||
|
{{table.filter.query_filter.form.as_p}}
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
{{table.filter.checkbox_filter.form.as_p}}
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
<button class="btn btn-default" title="{% trans 'Filter' %}">
|
<button class="btn btn-default" title="{% trans 'Filter' %}">
|
||||||
{% fa5_icon 'filter' %}
|
{% fa5_icon 'filter' %}
|
||||||
{% trans 'Apply filter' %}
|
{% trans 'Apply filter' %}
|
||||||
</button>
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user