# 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
f92ec7fde7
commit
d8607d9ade
@ -10,15 +10,11 @@ from django.utils.translation import gettext_lazy as _
|
||||
from django import forms
|
||||
from django.db.models import QuerySet
|
||||
|
||||
from konova.filters import AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, \
|
||||
RecordableTableFilterMixin
|
||||
from konova.filters import QueryTableFilter, CheckboxTableFilter
|
||||
|
||||
|
||||
class CompensationTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, RecordableTableFilterMixin):
|
||||
""" TableFilter for compensations
|
||||
|
||||
Based widely on InterventionTableFilter.
|
||||
Just some minor changes for Compensation model.
|
||||
class CheckboxCompensationTableFilter(CheckboxTableFilter):
|
||||
""" Specialization of regular CheckboxTableFilter for compensation model
|
||||
|
||||
"""
|
||||
|
||||
@ -59,12 +55,36 @@ class CompensationTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin
|
||||
return queryset
|
||||
|
||||
|
||||
class EcoAccountTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, RecordableTableFilterMixin):
|
||||
""" TableFilter for eco accounts
|
||||
class CompensationTableFilter:
|
||||
""" 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(
|
||||
method='filter_only_show_unrecorded',
|
||||
label=_("Show only unrecorded"),
|
||||
@ -107,3 +127,29 @@ class EcoAccountTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin,
|
||||
)
|
||||
else:
|
||||
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
|
||||
|
||||
"""
|
||||
from intervention.models import Intervention
|
||||
from konova.filters import AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, \
|
||||
RecordableTableFilterMixin
|
||||
from konova.filters import QueryTableFilter, CheckboxTableFilter
|
||||
|
||||
|
||||
class InterventionTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, RecordableTableFilterMixin):
|
||||
""" TableFilter for Intervention model
|
||||
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = Intervention
|
||||
fields = []
|
||||
class InterventionTableFilter:
|
||||
query_filter = None
|
||||
checkbox_filter = None
|
||||
qs = None
|
||||
|
||||
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
|
||||
|
||||
"""
|
||||
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(
|
||||
method='_filter_by_keyword',
|
||||
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)
|
||||
@ -31,11 +40,7 @@ class AbstractTableFilter(django_filters.FilterSet):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __init__(self, user: User, *args, **kwargs):
|
||||
self.user = user
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def _filter_by_keyword(self, queryset, name, value) -> QuerySet:
|
||||
def filter_by_keyword(self, queryset, name, value) -> QuerySet:
|
||||
""" Filters queryset depending on value of search bar input
|
||||
|
||||
Args:
|
||||
@ -309,3 +314,17 @@ class RecordableTableFilterMixin(django_filters.FilterSet):
|
||||
)
|
||||
else:
|
||||
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="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 }}">
|
||||
<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' %}">
|
||||
<button type="submit" class="btn btn-default input-group-text">
|
||||
<span class="">
|
||||
@ -82,11 +82,18 @@
|
||||
</div>
|
||||
<div id="filter" class="collapse" aria-labelledby="filterHeader">
|
||||
<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' %}">
|
||||
{% fa5_icon 'filter' %}
|
||||
{% trans 'Apply filter' %}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user