konova/compensation/filters.py
mpeltriaux 834fa8dbd4 # 61 Filter offices frontend/backend
* adds filters for registration and conservation offices
* adds SelectionTableFilter as new holder for these types of filter components
2022-01-12 08:56:49 +01:00

199 lines
5.3 KiB
Python

"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 29.07.21
"""
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 import QueryTableFilter, CheckboxTableFilter, SelectionTableFilter
class SelectionCompensationTableFilter(SelectionTableFilter):
""" Specialization of regular SelectionTableFilter for compensation model
"""
def filter_reg_office(self, queryset, name, value):
return queryset.filter(
intervention__responsible__registration_office=value
)
def filter_cons_office(self, queryset, name, value):
return queryset.filter(
intervention__responsible__conservation_office=value
)
class QueryCompensationTableFilter(QueryTableFilter):
""" Specialization of regular QueryTableFilter for compensation model
"""
def filter_file_number(self, queryset, name, value) -> QuerySet:
queryset = queryset.filter(
Q(intervention__responsible__registration_file_number__icontains=value) |
Q(intervention__responsible__conservation_file_number__icontains=value)
)
return queryset
class CheckboxCompensationTableFilter(CheckboxTableFilter):
""" Specialization of regular CheckboxTableFilter for compensation model
"""
def filter_show_all(self, queryset, name, value) -> QuerySet:
""" Filters queryset depending on value of 'show_all' setting
Args:
queryset ():
name ():
value ():
Returns:
"""
if not value:
return queryset.filter(
intervention__users__in=[self.user], # requesting user has access
)
else:
return queryset
def filter_show_recorded(self, queryset, name, value) -> QuerySet:
""" Filters queryset depending on value of 'show_recorded' setting
Args:
queryset ():
name ():
value ():
Returns:
"""
if not value:
return queryset.filter(
intervention__recorded=None,
)
else:
return queryset
class CompensationTableFilter:
""" 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, *args, **kwargs):
user = kwargs.get("user", None)
qs = kwargs.get("queryset", None)
request_data = kwargs.get("data", None)
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,
)
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"),
label_suffix=_(""),
widget=forms.CheckboxInput(
attrs={
"class": "form-check-input",
}
)
)
def filter_show_all(self, queryset, name, value) -> QuerySet:
""" Filters queryset depending on value of 'show_all' setting
Args:
queryset ():
name ():
value ():
Returns:
"""
if not value:
return queryset.filter(
users__in=[self.user], # requesting user has access
)
else:
return queryset
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 EcoAccountTableFilter:
""" 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)
qs = kwargs.get("queryset", None)
request_data = kwargs.get("data", None)
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,
)
self.checkbox_filter = CheckboxEcoAccountTableFilter(
user=user,
data=request_data,
queryset=self.query_filter.qs,
)
self.qs = self.checkbox_filter.qs