114 lines
2.8 KiB
Python
114 lines
2.8 KiB
Python
|
"""
|
||
|
Author: Michel Peltriaux
|
||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||
|
Created on: 22.07.21
|
||
|
|
||
|
"""
|
||
|
import django_filters
|
||
|
from django import forms
|
||
|
from django.contrib.auth.models import User
|
||
|
from django.db.models import QuerySet, Q
|
||
|
|
||
|
from django.utils.translation import gettext_lazy as _
|
||
|
from intervention.models import Intervention
|
||
|
|
||
|
|
||
|
class InterventionTableFilter(django_filters.FilterSet):
|
||
|
q = django_filters.Filter(
|
||
|
method='_filter_by_keyword',
|
||
|
widget=forms.HiddenInput(), # use search bar in template, we only need the filter logic in here!
|
||
|
)
|
||
|
sa = django_filters.BooleanFilter(
|
||
|
method='_filter_show_all',
|
||
|
label=_("Show all"),
|
||
|
label_suffix=_(""),
|
||
|
widget=forms.CheckboxInput()
|
||
|
)
|
||
|
sr = django_filters.BooleanFilter(
|
||
|
method='_filter_show_recorded',
|
||
|
label=_("Show recorded"),
|
||
|
label_suffix=_(""),
|
||
|
widget=forms.CheckboxInput()
|
||
|
)
|
||
|
# Gemarkung ##ToDo
|
||
|
g = django_filters.CharFilter(
|
||
|
field_name="name",
|
||
|
lookup_expr="icontains",
|
||
|
label=_(""),
|
||
|
label_suffix=_(""),
|
||
|
widget=forms.TextInput(
|
||
|
attrs={
|
||
|
"placeholder": _("District"),
|
||
|
"title": _("Search for district")
|
||
|
}
|
||
|
),
|
||
|
)
|
||
|
# Kreis
|
||
|
## ToDo
|
||
|
# Flur
|
||
|
## ToDo
|
||
|
# Zähler
|
||
|
## ToDo
|
||
|
# Nenner
|
||
|
## ToDo
|
||
|
|
||
|
class Meta:
|
||
|
model = Intervention
|
||
|
fields = []
|
||
|
|
||
|
def __init__(self, user: User, *args, **kwargs):
|
||
|
self.user = user
|
||
|
super().__init__(*args, **kwargs)
|
||
|
|
||
|
def _filter_by_keyword(self, queryset, name, value) -> QuerySet:
|
||
|
""" Filters queryset depending on value of search bar input
|
||
|
|
||
|
Args:
|
||
|
queryset ():
|
||
|
name ():
|
||
|
value ():
|
||
|
|
||
|
Returns:
|
||
|
|
||
|
"""
|
||
|
# build filter expression
|
||
|
q = Q(title__icontains=value) | Q(identifier__icontains=value)
|
||
|
return queryset.filter(q)
|
||
|
|
||
|
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_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(
|
||
|
recorded_on=None,
|
||
|
)
|
||
|
else:
|
||
|
return queryset
|