diff --git a/compensation/filters.py b/compensation/filters.py index b4dab866..3e3df60e 100644 --- a/compensation/filters.py +++ b/compensation/filters.py @@ -10,7 +10,22 @@ 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 +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): @@ -74,6 +89,7 @@ class CompensationTableFilter: Just some minor changes for Compensation model. """ + selection_filter = None query_filter = None checkbox_filter = None qs = None @@ -83,11 +99,16 @@ class CompensationTableFilter: qs = kwargs.get("queryset", None) request_data = kwargs.get("data", None) - self.query_filter = QueryCompensationTableFilter( + 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, @@ -149,6 +170,7 @@ class EcoAccountTableFilter: """ TableFilter for eco accounts """ + selection_filter = None query_filter = None checkbox_filter = None qs = None @@ -158,11 +180,16 @@ class EcoAccountTableFilter: qs = kwargs.get("queryset", None) request_data = kwargs.get("data", None) - self.query_filter = QueryTableFilter( + 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, diff --git a/intervention/filters.py b/intervention/filters.py index c6b61156..4b452089 100644 --- a/intervention/filters.py +++ b/intervention/filters.py @@ -5,10 +5,11 @@ Contact: michel.peltriaux@sgdnord.rlp.de Created on: 22.07.21 """ -from konova.filters import QueryTableFilter, CheckboxTableFilter +from konova.filters import QueryTableFilter, CheckboxTableFilter, SelectionTableFilter class InterventionTableFilter: + selection_filter = None query_filter = None checkbox_filter = None qs = None @@ -18,11 +19,16 @@ class InterventionTableFilter: qs = kwargs.get("queryset", None) request_data = kwargs.get("data", None) - self.query_filter = QueryTableFilter( + 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 = CheckboxTableFilter( user=user, data=request_data, diff --git a/konova/filters.py b/konova/filters.py index 1364aeb1..4a112872 100644 --- a/konova/filters.py +++ b/konova/filters.py @@ -6,11 +6,14 @@ Created on: 11.01.22 """ import django_filters +from dal_select2.widgets import ModelSelect2 from django import forms from django.utils.translation import gettext_lazy as _ from django.contrib.auth.models import User from django.db.models import QuerySet, Q +from codelist.models import KonovaCode +from codelist.settings import CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID from intervention.inputs import DummyFilterInput from konova.models import Parcel, District @@ -351,6 +354,79 @@ class RecordableTableFilterMixin(django_filters.FilterSet): return queryset +class RegistrationOfficeTableFilterMixin(django_filters.FilterSet): + """ A mixin for AbstractTableFilter + + Specialized on filtering for related registration offices + + """ + ro = django_filters.ModelChoiceFilter( + method="filter_reg_office", + label=_(""), + label_suffix=_(""), + queryset=KonovaCode.objects.filter( + is_archived=False, + is_leaf=True, + code_lists__in=[CODELIST_REGISTRATION_OFFICE_ID], + ), + widget=ModelSelect2( + url="codes-registration-office-autocomplete", + attrs={ + "data-placeholder": _("Registration office"), + "title": _("Search for registration office"), + "class": "", + } + ), + ) + + def filter_reg_office(self, queryset, name, value): + qs = queryset.filter( + responsible__registration_office=value + ) + return qs + + +class ConservationOfficeTableFilterMixin(django_filters.FilterSet): + """ A mixin for AbstractTableFilter + + Specialized on filtering for related conservation offices + + """ + co = django_filters.ModelChoiceFilter( + method="filter_cons_office", + label=_(""), + label_suffix=_(""), + queryset=KonovaCode.objects.filter( + is_archived=False, + is_leaf=True, + code_lists__in=[CODELIST_CONSERVATION_OFFICE_ID], + ), + widget=ModelSelect2( + url="codes-conservation-office-autocomplete", + attrs={ + "data-placeholder": _("Conservation office"), + "title": _("Search for conservation office"), + "class": "", + } + ), + ) + + def filter_cons_office(self, queryset, name, value): + qs = queryset.filter( + responsible__conservation_office=value + ) + return qs + + +class SelectionTableFilter(AbstractTableFilter, + RegistrationOfficeTableFilterMixin, + ConservationOfficeTableFilterMixin): + """ TableFilter holding different filter options for selection related filtering + + """ + pass + + class QueryTableFilter(AbstractTableFilter, KeywordTableFilterMixin, FileNumberTableFilterMixin, diff --git a/templates/generic_index.html b/templates/generic_index.html index cd2c3269..13080726 100644 --- a/templates/generic_index.html +++ b/templates/generic_index.html @@ -3,6 +3,16 @@ {% load django_tables2 %} {% load i18n static fontawesome_5 %} +{% block head %} + {% comment %} + dal documentation (django-autocomplete-light) states using form.media for adding needed scripts. + This does not work properly with modal forms, as the scripts are not loaded properly inside the modal. + Therefore the script linkages from form.media have been extracted and put inside dal/scripts.html to ensure + these scripts are loaded when needed. + {% endcomment %} + {% include 'dal/scripts.html' %} +{% endblock %} + {% block body %}
{% if table.title %} @@ -83,6 +93,11 @@
+ {% with table.filter.selection_filter.form as form %} + {% include 'filter/query_filter.html' %} + {% endwith %} +
+
{% with table.filter.query_filter.form as form %} {% include 'filter/query_filter.html' %} {% endwith %}