Skip to content

Commit

Permalink
✨ [#15] Enable filtering on expanded fields
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed May 14, 2024
1 parent 89e79d8 commit 712d04a
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions backend/src/openarchiefbeheer/zaken/api/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.db.models import Q, QuerySet, Subquery
from decimal import Decimal

from django.db.models import Func, IntegerField, Q, QuerySet, Subquery
from django.utils.translation import gettext_lazy as _

from django_filters import BooleanFilter, FilterSet
from django_filters import BooleanFilter, CharFilter, FilterSet, NumberFilter

from openarchiefbeheer.destruction.constants import ListItemStatus
from openarchiefbeheer.destruction.models import DestructionListItem
Expand All @@ -17,17 +19,44 @@ class ZaakFilter(FilterSet):
"If True, only cases not already included in a destruction list are returned."
),
)
resultaat__resultaattype__url = CharFilter(
help_text=_("Filter on the exact URL of resultaattype."),
)
bewaartermijn = CharFilter(
field_name="bewaartermijn",
method="filter_bewaartermijn",
help_text=_(
"Filter on bewaartermijn. "
"This corresponds to the property 'resultaat.resultaattype.archiefactietermijn'. "
"This field is expressed in Open Zaak as a ISO8601 duration."
),
)
vcs = NumberFilter(
field_name="vcs",
method="filter_vcs",
help_text=_(
"Filter on VCS. This stands for 'Vernietigings-Categorie Selectielijst'. "
"It is obtained through 'zaak.zaaktype.procestype.nummer'."
),
decimal_places=0,
)
heeft_relaties = BooleanFilter(
field_name="heeft_relaties",
method="filter_heeft_relaties",
help_text=_(
"Filter on whether this case has other related cases. "
"This is done by looking at the property 'zaak.relevanteAndereZaken'."
),
)

class Meta:
model = Zaak
fields = {
"uuid": ["exact"],
"url": ["exact"],
"status": ["exact"],
"zaaktype": ["exact", "in"],
"einddatum": ["exact", "gt", "lt", "isnull"],
"hoofdzaak": ["exact"],
"resultaat": ["exact"],
"startdatum": ["exact", "gt", "lt", "gte", "lte"],
"toelichting": ["exact"],
"omschrijving": ["exact"],
Expand Down Expand Up @@ -63,7 +92,6 @@ class Meta:
# "verlenging": ["exact"],
# "opschorting": ["exact"],
# "processobject": ["exact"],
# "relevante_andere_zaken": ["exact"],
# # Geometry Field
# "zaakgeometrie": ["exact"],
}
Expand All @@ -79,3 +107,29 @@ def filter_not_in_destruction_list(
).values_list("zaak", flat=True)

return queryset.exclude(url__in=Subquery(zaken_to_exclude))

def filter_bewaartermijn(
self, queryset: QuerySet[Zaak], name: str, value: str
) -> QuerySet[Zaak]:
# TODO it would be nice to do comparisons for periods such as gt/lt
return queryset.filter(resultaat__resultaattype__archiefactietermijn=value)

def filter_vcs(
self, queryset: QuerySet[Zaak], name: str, value: Decimal
) -> QuerySet[Zaak]:
return queryset.filter(zaaktype__selectielijst_procestype__nummer=int(value))

def filter_heeft_relaties(
self, queryset: QuerySet[Zaak], name: str, value: bool
) -> QuerySet[Zaak]:
annotated_zaken = queryset.annotate(
number_relations=Func(
"relevante_andere_zaken",
function="jsonb_array_length",
output_field=IntegerField(),
)
)
if value:
return annotated_zaken.filter(number_relations__gt=0)

return annotated_zaken.filter(number_relations=0)

0 comments on commit 712d04a

Please sign in to comment.