Filters refactoring
* splits filters.py of compensation and ema app into separate files in new /filters module * optimizes entry search for multi keyword input
This commit is contained in:
parent
d785285805
commit
ad4d64457b
7
compensation/filters/__init__.py
Normal file
7
compensation/filters/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 18.08.22
|
||||
|
||||
"""
|
@ -1,17 +1,14 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 29.07.21
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 18.08.22
|
||||
|
||||
"""
|
||||
import django_filters
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django import forms
|
||||
from django.db.models import QuerySet, Q
|
||||
|
||||
from konova.filters.mixins.office import ConservationOfficeTableFilterMixin
|
||||
from konova.filters.table_filters import QueryTableFilter, CheckboxTableFilter, SelectionTableFilter, AbstractTableFilter
|
||||
from konova.filters.table_filters import AbstractTableFilter, CheckboxTableFilter, QueryTableFilter, \
|
||||
SelectionTableFilter
|
||||
|
||||
|
||||
class SelectionCompensationTableFilter(SelectionTableFilter):
|
||||
@ -114,71 +111,3 @@ class CompensationTableFilter(AbstractTableFilter):
|
||||
)
|
||||
# Overwrite final queryset as well
|
||||
self.qs = self.checkbox_filter.qs
|
||||
|
||||
|
||||
class CheckboxEcoAccountTableFilter(CheckboxTableFilter):
|
||||
sr = django_filters.BooleanFilter(
|
||||
method='filter_only_show_unrecorded',
|
||||
label=_("Show only unrecorded"),
|
||||
label_suffix=_(""),
|
||||
widget=forms.CheckboxInput(
|
||||
attrs={
|
||||
"class": "form-check-input",
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
def filter_only_show_unrecorded(self, queryset, name, value) -> QuerySet:
|
||||
""" Filters queryset depending on value of 'show_recorded' setting
|
||||
|
||||
Args:
|
||||
queryset ():
|
||||
name ():
|
||||
value ():
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if value:
|
||||
return queryset.filter(
|
||||
recorded=None,
|
||||
)
|
||||
else:
|
||||
return queryset
|
||||
|
||||
|
||||
class SelectionEcoAccountTableFilter(ConservationOfficeTableFilterMixin):
|
||||
""" Special selection table filter for eco accounts
|
||||
|
||||
EcoAccounts only need a selection filter for conservation office
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class EcoAccountTableFilter(AbstractTableFilter):
|
||||
""" TableFilter for eco accounts
|
||||
|
||||
"""
|
||||
def __init__(self, user=None, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.user = user
|
||||
qs = kwargs.get("queryset", None)
|
||||
request_data = kwargs.get("data", None)
|
||||
|
||||
# Pipe the queryset through all needed filters
|
||||
self.selection_filter = SelectionEcoAccountTableFilter(
|
||||
data=request_data,
|
||||
queryset=qs,
|
||||
)
|
||||
self.query_filter = QueryTableFilter(
|
||||
data=request_data,
|
||||
queryset=self.selection_filter.qs,
|
||||
)
|
||||
self.checkbox_filter = CheckboxEcoAccountTableFilter(
|
||||
user=user,
|
||||
data=request_data,
|
||||
queryset=self.query_filter.qs,
|
||||
)
|
||||
# Overwrite the final queryset result
|
||||
self.qs = self.checkbox_filter.qs
|
82
compensation/filters/eco_account.py
Normal file
82
compensation/filters/eco_account.py
Normal file
@ -0,0 +1,82 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 18.08.22
|
||||
|
||||
"""
|
||||
from django import forms
|
||||
from django.db.models import QuerySet
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
import django_filters
|
||||
|
||||
from konova.filters.mixins.office import ConservationOfficeTableFilterMixin
|
||||
from konova.filters.table_filters import AbstractTableFilter, CheckboxTableFilter, QueryTableFilter
|
||||
|
||||
|
||||
class CheckboxEcoAccountTableFilter(CheckboxTableFilter):
|
||||
sr = django_filters.BooleanFilter(
|
||||
method='filter_only_show_unrecorded',
|
||||
label=_("Show only unrecorded"),
|
||||
label_suffix=_(""),
|
||||
widget=forms.CheckboxInput(
|
||||
attrs={
|
||||
"class": "form-check-input",
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
def filter_only_show_unrecorded(self, queryset, name, value) -> QuerySet:
|
||||
""" Filters queryset depending on value of 'show_recorded' setting
|
||||
|
||||
Args:
|
||||
queryset ():
|
||||
name ():
|
||||
value ():
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if value:
|
||||
return queryset.filter(
|
||||
recorded=None,
|
||||
)
|
||||
else:
|
||||
return queryset
|
||||
|
||||
|
||||
class SelectionEcoAccountTableFilter(ConservationOfficeTableFilterMixin):
|
||||
""" Special selection table filter for eco accounts
|
||||
|
||||
EcoAccounts only need a selection filter for conservation office
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class EcoAccountTableFilter(AbstractTableFilter):
|
||||
""" TableFilter for eco accounts
|
||||
|
||||
"""
|
||||
def __init__(self, user=None, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.user = user
|
||||
qs = kwargs.get("queryset", None)
|
||||
request_data = kwargs.get("data", None)
|
||||
|
||||
# Pipe the queryset through all needed filters
|
||||
self.selection_filter = SelectionEcoAccountTableFilter(
|
||||
data=request_data,
|
||||
queryset=qs,
|
||||
)
|
||||
self.query_filter = QueryTableFilter(
|
||||
data=request_data,
|
||||
queryset=self.selection_filter.qs,
|
||||
)
|
||||
self.checkbox_filter = CheckboxEcoAccountTableFilter(
|
||||
user=user,
|
||||
data=request_data,
|
||||
queryset=self.query_filter.qs,
|
||||
)
|
||||
# Overwrite the final queryset result
|
||||
self.qs = self.checkbox_filter.qs
|
@ -11,7 +11,7 @@ from django.urls import reverse
|
||||
from django.utils.html import format_html
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.filters import CompensationTableFilter
|
||||
from compensation.filters.compensation import CompensationTableFilter
|
||||
from compensation.models import Compensation
|
||||
from konova.utils.message_templates import DATA_IS_UNCHECKED, DATA_CHECKED_ON_TEMPLATE, DATA_CHECKED_PREVIOUSLY_TEMPLATE
|
||||
from konova.utils.tables import BaseTable, TableRenderMixin
|
||||
|
@ -11,12 +11,13 @@ from django.urls import reverse
|
||||
from django.utils.html import format_html
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.filters import EcoAccountTableFilter
|
||||
from compensation.filters.eco_account import EcoAccountTableFilter
|
||||
from compensation.models import EcoAccount
|
||||
from konova.utils.tables import TableRenderMixin, BaseTable
|
||||
|
||||
import django_tables2 as tables
|
||||
|
||||
|
||||
class EcoAccountTable(BaseTable, TableRenderMixin):
|
||||
id = tables.Column(
|
||||
verbose_name=_("Identifier"),
|
||||
|
@ -5,7 +5,7 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 19.08.21
|
||||
|
||||
"""
|
||||
from compensation.filters import EcoAccountTableFilter
|
||||
from compensation.filters.eco_account import EcoAccountTableFilter
|
||||
|
||||
|
||||
class EmaTableFilter(EcoAccountTableFilter):
|
||||
|
@ -36,6 +36,9 @@ class KeywordTableFilterMixin(django_filters.FilterSet):
|
||||
|
||||
"""
|
||||
value = value.strip()
|
||||
value = value.split(" ")
|
||||
# build filter expression
|
||||
q = Q(title__icontains=value) | Q(identifier__icontains=value)
|
||||
q = Q()
|
||||
for val in value:
|
||||
q &= Q(title__icontains=val) | Q(identifier__icontains=val)
|
||||
return queryset.filter(q)
|
||||
|
Loading…
Reference in New Issue
Block a user