From 59a541397e75a8e0997423a5f7789012ec1b2526 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Wed, 12 Jan 2022 09:23:22 +0100 Subject: [PATCH] # 61 Filter code enhancement * reorganizes code --- compensation/filters.py | 33 +++++++++++++-------------------- intervention/filters.py | 18 +++++++----------- konova/filters.py | 23 ++++++++++++----------- konova/tests/test_views.py | 2 ++ 4 files changed, 34 insertions(+), 42 deletions(-) diff --git a/compensation/filters.py b/compensation/filters.py index 3e3df60e..62c22bd1 100644 --- a/compensation/filters.py +++ b/compensation/filters.py @@ -10,7 +10,7 @@ from django.utils.translation import gettext_lazy as _ from django import forms 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): @@ -82,30 +82,26 @@ class CheckboxCompensationTableFilter(CheckboxTableFilter): return queryset -class CompensationTableFilter: +class CompensationTableFilter(AbstractTableFilter): """ TableFilter for compensations Based widely on InterventionTableFilter. Just some minor changes for Compensation model. """ - selection_filter = None - query_filter = None - checkbox_filter = None - qs = None + def __init__(self, user=None, *args, **kwargs): + super().__init__(*args, **kwargs) - def __init__(self, *args, **kwargs): - user = kwargs.get("user", None) + self.user = user qs = kwargs.get("queryset", None) request_data = kwargs.get("data", None) + # Overwrite all filters for special needs of compensations self.selection_filter = SelectionCompensationTableFilter( - user=user, data=request_data, queryset=qs, ) self.query_filter = QueryCompensationTableFilter( - user=user, data=request_data, queryset=self.selection_filter.qs, ) @@ -114,6 +110,7 @@ class CompensationTableFilter: data=request_data, queryset=self.query_filter.qs, ) + # Overwrite final queryset as well self.qs = self.checkbox_filter.qs @@ -166,27 +163,22 @@ class CheckboxEcoAccountTableFilter(CheckboxTableFilter): return queryset -class EcoAccountTableFilter: +class EcoAccountTableFilter(AbstractTableFilter): """ TableFilter for eco accounts """ - selection_filter = None - query_filter = None - checkbox_filter = None - qs = None - - def __init__(self, *args, **kwargs): - user = kwargs.get("user", None) + 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 = SelectionTableFilter( - user=user, data=request_data, queryset=qs, ) self.query_filter = QueryTableFilter( - user=user, data=request_data, queryset=self.selection_filter.qs, ) @@ -195,4 +187,5 @@ class EcoAccountTableFilter: data=request_data, queryset=self.query_filter.qs, ) + # Overwrite the final queryset result self.qs = self.checkbox_filter.qs diff --git a/intervention/filters.py b/intervention/filters.py index 4b452089..0d927573 100644 --- a/intervention/filters.py +++ b/intervention/filters.py @@ -5,27 +5,22 @@ Contact: michel.peltriaux@sgdnord.rlp.de Created on: 22.07.21 """ -from konova.filters import QueryTableFilter, CheckboxTableFilter, SelectionTableFilter +from konova.filters import AbstractTableFilter, SelectionTableFilter, QueryTableFilter, CheckboxTableFilter -class InterventionTableFilter: - selection_filter = None - query_filter = None - checkbox_filter = None - qs = None - - def __init__(self, *args, **kwargs): - user = kwargs.get("user", None) +class InterventionTableFilter(AbstractTableFilter): + 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 = SelectionTableFilter( - user=user, data=request_data, queryset=qs, ) self.query_filter = QueryTableFilter( - user=user, data=request_data, queryset=self.selection_filter.qs, ) @@ -35,3 +30,4 @@ class InterventionTableFilter: queryset=self.query_filter.qs, ) self.qs = self.checkbox_filter.qs + diff --git a/konova/filters.py b/konova/filters.py index 4a112872..6244c35a 100644 --- a/konova/filters.py +++ b/konova/filters.py @@ -19,16 +19,18 @@ from konova.models import Parcel, District 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: 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( @@ -292,9 +294,10 @@ class ShareableTableFilterMixin(django_filters.FilterSet): abstract = True def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) + self.user = kwargs.pop("user", None) if self.user is None: raise AttributeError("User must be set for further filtering!") + super().__init__(*args, **kwargs) def filter_show_all(self, queryset, name, value) -> QuerySet: """ Filters queryset depending on value of 'show_all' setting @@ -418,8 +421,7 @@ class ConservationOfficeTableFilterMixin(django_filters.FilterSet): return qs -class SelectionTableFilter(AbstractTableFilter, - RegistrationOfficeTableFilterMixin, +class SelectionTableFilter(RegistrationOfficeTableFilterMixin, ConservationOfficeTableFilterMixin): """ TableFilter holding different filter options for selection related filtering @@ -427,8 +429,7 @@ class SelectionTableFilter(AbstractTableFilter, pass -class QueryTableFilter(AbstractTableFilter, - KeywordTableFilterMixin, +class QueryTableFilter(KeywordTableFilterMixin, FileNumberTableFilterMixin, GeoReferencedTableFilterMixin): """ TableFilter holding different filter options for query related filtering @@ -437,7 +438,7 @@ class QueryTableFilter(AbstractTableFilter, pass -class CheckboxTableFilter(AbstractTableFilter, ShareableTableFilterMixin, RecordableTableFilterMixin): +class CheckboxTableFilter(ShareableTableFilterMixin, RecordableTableFilterMixin): """ TableFilter holding different filter options for checkbox related filtering """ diff --git a/konova/tests/test_views.py b/konova/tests/test_views.py index d31726ff..dad44297 100644 --- a/konova/tests/test_views.py +++ b/konova/tests/test_views.py @@ -404,6 +404,7 @@ class AutocompleteTestCase(BaseViewTestCase): 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_cons_off = reverse("codes-conservation-office-autocomplete") + cls.atcmplt_code_share_user = reverse("share-user-autocomplete") def _test_views_anonymous_user(self): # 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_reg_off, self.atcmplt_code_comp_cons_off, + self.atcmplt_code_share_user, ] self.assert_url_fail(client, urls)