From a06b96043520f926307255640d760e56f0b658c3 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Thu, 8 Dec 2022 10:14:39 +0100 Subject: [PATCH] #268 Filter multiple parcelgroups * adds filter support for multiple parcelgroup and district names, separated by ',' --- konova/filters/mixins/geo_reference.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/konova/filters/mixins/geo_reference.py b/konova/filters/mixins/geo_reference.py index 71fa41f5..6d77f6b2 100644 --- a/konova/filters/mixins/geo_reference.py +++ b/konova/filters/mixins/geo_reference.py @@ -19,7 +19,7 @@ class GeoReferencedTableFilterMixin(django_filters.FilterSet): Specialized on filtering GeoReferenced model types """ - # Parcel gmrkng + # District di = django_filters.CharFilter( method="filter_district", label=_(""), @@ -72,7 +72,7 @@ class GeoReferencedTableFilterMixin(django_filters.FilterSet): ), ) - # Parcel counter + # Parcel number pn = django_filters.NumberFilter( method="filter_parcel_number", label=_(""), @@ -112,7 +112,7 @@ class GeoReferencedTableFilterMixin(django_filters.FilterSet): return queryset def filter_district(self, queryset, name, value) -> QuerySet: - """ Filters queryset depending on value for 'Gemarkung' + """ Filters queryset depending on value for 'Kreis' Args: queryset (): @@ -122,10 +122,13 @@ class GeoReferencedTableFilterMixin(django_filters.FilterSet): Returns: """ - matching_districts = District.objects.filter( - Q(name__icontains=value) | - Q(key__icontains=value) - ).distinct() + values = value.split(",") + q = Q() + for val in values: + val = val.strip() + q |= Q(name__icontains=val) | Q(key__icontains=val) + + matching_districts = District.objects.filter(q).distinct() matching_parcels = Parcel.objects.filter( district__in=matching_districts ) @@ -148,9 +151,14 @@ class GeoReferencedTableFilterMixin(django_filters.FilterSet): Returns: """ + values = value.split(",") + q = Q() + for val in values: + val = val.strip() + q |= Q(parcel_group__name__icontains=val) | Q(parcel_group__key__icontains=val) queryset = self._filter_parcel_reference( queryset, - Q(parcel_group__name__icontains=value) | Q(parcel_group__key__icontains=value), + q, ) return queryset