""" 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 from konova.filters import AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, \ RecordableTableFilterMixin class CompensationTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, RecordableTableFilterMixin): """ TableFilter for compensations Based widely on InterventionTableFilter. Just some minor changes 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 EcoAccountTableFilter(AbstractTableFilter, GeoReferencedTableFilterMixin, ShareableTableFilterMixin, RecordableTableFilterMixin): """ TableFilter for eco accounts Just some minor changes for EcoAccount model. """ sr = django_filters.BooleanFilter( method='filter_only_show_unrecorded', label=_("Show only unrecorded"), label_suffix=_(""), widget=forms.CheckboxInput() ) 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