# 61 Filter code enhancement

* reorganizes code
This commit is contained in:
mpeltriaux 2022-01-12 09:23:22 +01:00
parent 0cb65ae720
commit 59a541397e
4 changed files with 34 additions and 42 deletions

View File

@ -10,7 +10,7 @@ from django.utils.translation import gettext_lazy as _
from django import forms from django import forms
from django.db.models import QuerySet, Q from django.db.models import QuerySet, Q
from konova.filters import QueryTableFilter, CheckboxTableFilter, SelectionTableFilter from konova.filters import QueryTableFilter, CheckboxTableFilter, SelectionTableFilter, AbstractTableFilter
class SelectionCompensationTableFilter(SelectionTableFilter): class SelectionCompensationTableFilter(SelectionTableFilter):
@ -82,30 +82,26 @@ class CheckboxCompensationTableFilter(CheckboxTableFilter):
return queryset return queryset
class CompensationTableFilter: class CompensationTableFilter(AbstractTableFilter):
""" TableFilter for compensations """ TableFilter for compensations
Based widely on InterventionTableFilter. Based widely on InterventionTableFilter.
Just some minor changes for Compensation model. Just some minor changes for Compensation model.
""" """
selection_filter = None def __init__(self, user=None, *args, **kwargs):
query_filter = None super().__init__(*args, **kwargs)
checkbox_filter = None
qs = None
def __init__(self, *args, **kwargs): self.user = user
user = kwargs.get("user", None)
qs = kwargs.get("queryset", None) qs = kwargs.get("queryset", None)
request_data = kwargs.get("data", None) request_data = kwargs.get("data", None)
# Overwrite all filters for special needs of compensations
self.selection_filter = SelectionCompensationTableFilter( self.selection_filter = SelectionCompensationTableFilter(
user=user,
data=request_data, data=request_data,
queryset=qs, queryset=qs,
) )
self.query_filter = QueryCompensationTableFilter( self.query_filter = QueryCompensationTableFilter(
user=user,
data=request_data, data=request_data,
queryset=self.selection_filter.qs, queryset=self.selection_filter.qs,
) )
@ -114,6 +110,7 @@ class CompensationTableFilter:
data=request_data, data=request_data,
queryset=self.query_filter.qs, queryset=self.query_filter.qs,
) )
# Overwrite final queryset as well
self.qs = self.checkbox_filter.qs self.qs = self.checkbox_filter.qs
@ -166,27 +163,22 @@ class CheckboxEcoAccountTableFilter(CheckboxTableFilter):
return queryset return queryset
class EcoAccountTableFilter: class EcoAccountTableFilter(AbstractTableFilter):
""" TableFilter for eco accounts """ TableFilter for eco accounts
""" """
selection_filter = None def __init__(self, user=None, *args, **kwargs):
query_filter = None super().__init__(*args, **kwargs)
checkbox_filter = None self.user = user
qs = None
def __init__(self, *args, **kwargs):
user = kwargs.get("user", None)
qs = kwargs.get("queryset", None) qs = kwargs.get("queryset", None)
request_data = kwargs.get("data", None) request_data = kwargs.get("data", None)
# Pipe the queryset through all needed filters
self.selection_filter = SelectionTableFilter( self.selection_filter = SelectionTableFilter(
user=user,
data=request_data, data=request_data,
queryset=qs, queryset=qs,
) )
self.query_filter = QueryTableFilter( self.query_filter = QueryTableFilter(
user=user,
data=request_data, data=request_data,
queryset=self.selection_filter.qs, queryset=self.selection_filter.qs,
) )
@ -195,4 +187,5 @@ class EcoAccountTableFilter:
data=request_data, data=request_data,
queryset=self.query_filter.qs, queryset=self.query_filter.qs,
) )
# Overwrite the final queryset result
self.qs = self.checkbox_filter.qs self.qs = self.checkbox_filter.qs

View File

@ -5,27 +5,22 @@ Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 22.07.21 Created on: 22.07.21
""" """
from konova.filters import QueryTableFilter, CheckboxTableFilter, SelectionTableFilter from konova.filters import AbstractTableFilter, SelectionTableFilter, QueryTableFilter, CheckboxTableFilter
class InterventionTableFilter: class InterventionTableFilter(AbstractTableFilter):
selection_filter = None def __init__(self, user=None, *args, **kwargs):
query_filter = None super().__init__(*args, **kwargs)
checkbox_filter = None self.user = user
qs = None
def __init__(self, *args, **kwargs):
user = kwargs.get("user", None)
qs = kwargs.get("queryset", None) qs = kwargs.get("queryset", None)
request_data = kwargs.get("data", None) request_data = kwargs.get("data", None)
# Pipe the queryset through all needed filters
self.selection_filter = SelectionTableFilter( self.selection_filter = SelectionTableFilter(
user=user,
data=request_data, data=request_data,
queryset=qs, queryset=qs,
) )
self.query_filter = QueryTableFilter( self.query_filter = QueryTableFilter(
user=user,
data=request_data, data=request_data,
queryset=self.selection_filter.qs, queryset=self.selection_filter.qs,
) )
@ -35,3 +30,4 @@ class InterventionTableFilter:
queryset=self.query_filter.qs, queryset=self.query_filter.qs,
) )
self.qs = self.checkbox_filter.qs self.qs = self.checkbox_filter.qs

View File

@ -19,16 +19,18 @@ from konova.models import Parcel, District
class AbstractTableFilter(django_filters.FilterSet): class AbstractTableFilter(django_filters.FilterSet):
""" TableFilter for Intervention model """ Base TableFilter for all models
""" """
selection_filter = None
query_filter = None
checkbox_filter = None
qs = None
user = None
class Meta: class Meta:
abstract = True abstract = True
def __init__(self, user: User, *args, **kwargs):
self.user = user
super().__init__(*args, **kwargs)
class KeywordTableFilterMixin(django_filters.FilterSet): class KeywordTableFilterMixin(django_filters.FilterSet):
q = django_filters.Filter( q = django_filters.Filter(
@ -292,9 +294,10 @@ class ShareableTableFilterMixin(django_filters.FilterSet):
abstract = True abstract = True
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) self.user = kwargs.pop("user", None)
if self.user is None: if self.user is None:
raise AttributeError("User must be set for further filtering!") raise AttributeError("User must be set for further filtering!")
super().__init__(*args, **kwargs)
def filter_show_all(self, queryset, name, value) -> QuerySet: def filter_show_all(self, queryset, name, value) -> QuerySet:
""" Filters queryset depending on value of 'show_all' setting """ Filters queryset depending on value of 'show_all' setting
@ -418,8 +421,7 @@ class ConservationOfficeTableFilterMixin(django_filters.FilterSet):
return qs return qs
class SelectionTableFilter(AbstractTableFilter, class SelectionTableFilter(RegistrationOfficeTableFilterMixin,
RegistrationOfficeTableFilterMixin,
ConservationOfficeTableFilterMixin): ConservationOfficeTableFilterMixin):
""" TableFilter holding different filter options for selection related filtering """ TableFilter holding different filter options for selection related filtering
@ -427,8 +429,7 @@ class SelectionTableFilter(AbstractTableFilter,
pass pass
class QueryTableFilter(AbstractTableFilter, class QueryTableFilter(KeywordTableFilterMixin,
KeywordTableFilterMixin,
FileNumberTableFilterMixin, FileNumberTableFilterMixin,
GeoReferencedTableFilterMixin): GeoReferencedTableFilterMixin):
""" TableFilter holding different filter options for query related filtering """ TableFilter holding different filter options for query related filtering
@ -437,7 +438,7 @@ class QueryTableFilter(AbstractTableFilter,
pass pass
class CheckboxTableFilter(AbstractTableFilter, ShareableTableFilterMixin, RecordableTableFilterMixin): class CheckboxTableFilter(ShareableTableFilterMixin, RecordableTableFilterMixin):
""" TableFilter holding different filter options for checkbox related filtering """ TableFilter holding different filter options for checkbox related filtering
""" """

View File

@ -404,6 +404,7 @@ class AutocompleteTestCase(BaseViewTestCase):
cls.atcmplt_code_comp_process = reverse("codes-process-type-autocomplete") cls.atcmplt_code_comp_process = reverse("codes-process-type-autocomplete")
cls.atcmplt_code_comp_reg_off = reverse("codes-registration-office-autocomplete") cls.atcmplt_code_comp_reg_off = reverse("codes-registration-office-autocomplete")
cls.atcmplt_code_comp_cons_off = reverse("codes-conservation-office-autocomplete") cls.atcmplt_code_comp_cons_off = reverse("codes-conservation-office-autocomplete")
cls.atcmplt_code_share_user = reverse("share-user-autocomplete")
def _test_views_anonymous_user(self): def _test_views_anonymous_user(self):
# ATTENTION: As of the current state of django-autocomplete-light, there is no way to check on authenticated # ATTENTION: As of the current state of django-autocomplete-light, there is no way to check on authenticated
@ -421,6 +422,7 @@ class AutocompleteTestCase(BaseViewTestCase):
self.atcmplt_code_comp_process, self.atcmplt_code_comp_process,
self.atcmplt_code_comp_reg_off, self.atcmplt_code_comp_reg_off,
self.atcmplt_code_comp_cons_off, self.atcmplt_code_comp_cons_off,
self.atcmplt_code_share_user,
] ]
self.assert_url_fail(client, urls) self.assert_url_fail(client, urls)