Skip to content

Commit

Permalink
🎨 [#256] improved way we filter on urls in api queryparams
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Nov 21, 2024
1 parent e6d0247 commit 3ec6e17
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 389 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from openklant.components.klantinteracties.models.digitaal_adres import DigitaalAdres
from openklant.components.utils.filters import ExpandFilter

from .filters import URLViewFilter


class DigitaalAdresDetailFilterSet(FilterSet):
expand = ExpandFilter(serializer_class=DigitaalAdresSerializer)
Expand All @@ -22,11 +24,11 @@ class DigitaalAdresFilterSet(FilterSet):
),
field_name="betrokkene__uuid",
)
verstrekt_door_betrokkene__url = filters.CharFilter(
verstrekt_door_betrokkene__url = URLViewFilter(
help_text=_(
"Zoek digitaal adres(sen) object(en) op basis van de betrokkene url."
),
method="filter_betrokkene_url",
field_name="betrokkene__uuid",
)
verstrekt_door_betrokkene__rol = filters.ChoiceFilter(
help_text=_(
Expand All @@ -36,11 +38,11 @@ class DigitaalAdresFilterSet(FilterSet):
)
verstrekt_door_partij__uuid = filters.UUIDFilter(
help_text=_("Zoek digitaal adres(sen) object(en) op basis van de partij uuid."),
field_name="betrokkene__uuid",
field_name="partij__uuid",
)
verstrekt_door_partij__url = filters.CharFilter(
verstrekt_door_partij__url = URLViewFilter(
help_text=_("Zoek digitaal adres(sen) object(en) op basis van de partij url."),
method="filter_partij_url",
field_name="partij__uuid",
)
verstrekt_door_partij__soort_partij = filters.ChoiceFilter(
help_text=_(
Expand Down
16 changes: 16 additions & 0 deletions src/openklant/components/klantinteracties/api/filterset/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import uuid

from django import forms

from django_filters.rest_framework import filters
from drf_spectacular.types import OpenApiTypes


class URLViewFilter(filters.Filter):
field_class = forms.URLField

def filter(self, qs, value: OpenApiTypes.URI):
if value:
value = uuid.UUID(value.rstrip("/").split("/")[-1])

return super().filter(qs, value)
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import uuid

from django.utils.translation import gettext_lazy as _

from django_filters.rest_framework import FilterSet, filters

from openklant.components.klantinteracties.models.internetaken import InterneTaak

from .filters import URLViewFilter


class InternetaakFilterSet(FilterSet):
toegewezen_aan_actor__uuid = filters.UUIDFilter(
help_text=_("Zoek internetaak object op basis van het toegewezen actor uuid."),
field_name="actoren__uuid",
)
toegewezen_aan_actor__url = filters.CharFilter(
toegewezen_aan_actor__url = URLViewFilter(
help_text=_("Zoek internetaak object op basis van het toegewezen actor url."),
method="filter_toegewezen_aan_actor_url",
field_name="actoren__uuid",
)
aanleidinggevend_klantcontact__uuid = filters.UUIDFilter(
help_text=_(
"Zoek internetaak object op basis van het aanleidingevende klantcontact uuid."
),
field_name="klantcontact__uuid",
)
aanleidinggevend_klantcontact__url = filters.CharFilter(
aanleidinggevend_klantcontact__url = URLViewFilter(
help_text=_(
"Zoek internetaak object op basis van het aanleidingevende klantcontact url."
),
method="filter_aanleidinggevend_klantcontact_url",
field_name="klantcontact__uuid",
)

class Meta:
Expand All @@ -43,17 +43,3 @@ class Meta:
"aanleidinggevend_klantcontact__uuid",
"aanleidinggevend_klantcontact__url",
)

def filter_toegewezen_aan_actor_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(actoren__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_aanleidinggevend_klantcontact_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(klantcontact__uuid=url_uuid)
except ValueError:
return queryset.none()
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import uuid

from django.utils.translation import gettext_lazy as _

from django_filters.rest_framework import FilterSet, filters
Expand All @@ -14,37 +12,39 @@
)
from openklant.components.utils.filters import ExpandFilter

from .filters import URLViewFilter


class KlantcontactDetailFilterSet(FilterSet):
expand = ExpandFilter(serializer_class=KlantcontactSerializer)


class KlantcontactFilterSet(FilterSet):
had_betrokkene__url = filters.CharFilter(
had_betrokkene__url = URLViewFilter(
help_text=_("Zoek klantcontact object op basis van het betrokkene url."),
method="filter_betrokkene_url",
field_name="betrokkene__uuid",
)
had_betrokkene__uuid = filters.UUIDFilter(
help_text=_("Zoek klantcontact object op basis van het betrokkene uuid."),
field_name="betrokkene__uuid",
)
had_betrokkene__was_partij__url = filters.CharFilter(
had_betrokkene__was_partij__url = URLViewFilter(
help_text=_("Zoek klantcontact object op basis van de partij url."),
method="filter_partij_url",
field_name="betrokkene__partij__uuid",
)
had_betrokkene__was_partij__uuid = filters.UUIDFilter(
help_text=_("Zoek klantcontact object op basis van de partij uuid."),
field_name="betrokkene__partij__uuid",
)
onderwerpobject__url = filters.CharFilter(
onderwerpobject__url = URLViewFilter(
help_text=_("Zoek klantcontact object op basis van het onderwerpobject url."),
method="filter_onderwerpobject_url",
field_name="onderwerpobject__uuid",
)
was_onderwerpobject__url = filters.CharFilter(
was_onderwerpobject__url = URLViewFilter(
help_text=_(
"Zoek was klantcontact object op basis van het onderwerpobject url."
),
method="filter_was_onderwerpobject_url",
field_name="was_onderwerpobject__uuid",
)
inhoud = filters.CharFilter(
lookup_expr="icontains",
Expand Down Expand Up @@ -85,72 +85,44 @@ class Meta:
"plaatsgevonden_op",
)

def filter_betrokkene_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(betrokkene__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_partij_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(betrokkene__partij__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_onderwerpobject_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(onderwerpobject__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_was_onderwerpobject_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(was_onderwerpobject__uuid=url_uuid)
except ValueError:
return queryset.none()


class BetrokkeneFilterSet(FilterSet):
had_klantcontact__nummer = filters.CharFilter(
help_text=_("Zoek betrokkene object op basis van het klantcontact nummer"),
method="filter_had_klantcontact_nummer",
)
had_klantcontact__url = filters.CharFilter(
had_klantcontact__url = URLViewFilter(
help_text=_("Zoek betrokkene object op basis van het klantcontact url"),
method="filter_had_klantcontact_url",
field_name="klantcontact__uuid",
)
had_klantcontact__uuid = filters.CharFilter(
had_klantcontact__uuid = filters.UUIDFilter(
help_text=_("Zoek betrokkene object op basis van het klantcontact uuid"),
method="filter_had_klantcontact_uuid",
field_name="klantcontact__uuid",
)
verstrektedigitaal_adres__adres = filters.CharFilter(
help_text=_("Zoek betrokkene object op basis van het digitaaladres adres"),
method="filter_digitaaladres_adres",
)
verstrektedigitaal_adres__url = filters.CharFilter(
verstrektedigitaal_adres__url = URLViewFilter(
help_text=_("Zoek betrokkene object op basis van het digitaaladres url"),
method="filter_digitaaladres_url",
field_name="digitaaladres__uuid",
)
verstrektedigitaal_adres__uuid = filters.CharFilter(
verstrektedigitaal_adres__uuid = filters.UUIDFilter(
help_text=_("Zoek betrokkene object op basis van het digitaaladres uuid"),
method="filter_digitaaladres_uuid",
field_name="digitaaladres__uuid",
)

was_partij__nummer = filters.CharFilter(
help_text=_("Zoek betrokkene object op basis van het partij nummer"),
method="filter_partij_nummer",
)
was_partij__url = filters.CharFilter(
was_partij__url = URLViewFilter(
help_text=_("Zoek betrokkene object op basis van het partij url"),
method="filter_partij_url",
field_name="partij__uuid",
)
was_partij__uuid = filters.CharFilter(
was_partij__uuid = filters.UUIDFilter(
help_text=_("Zoek betrokkene object op basis van het partij uuid"),
method="filter_partij_uuid",
field_name="partij__uuid",
)

class Meta:
Expand All @@ -172,20 +144,6 @@ class Meta:
"organisatienaam",
)

def filter_had_klantcontact_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(klantcontact__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_had_klantcontact_uuid(self, queryset, name, value):
try:
klantcontact_uuid = uuid.UUID(value)
return queryset.filter(klantcontact__uuid=klantcontact_uuid)
except ValueError:
return queryset.none()

def filter_had_klantcontact_nummer(self, queryset, name, value):
try:
return queryset.filter(klantcontact__nummer=value)
Expand All @@ -195,48 +153,20 @@ def filter_had_klantcontact_nummer(self, queryset, name, value):
def filter_digitaaladres_adres(self, queryset, name, value):
return queryset.filter(digitaaladres__adres=value)

def filter_digitaaladres_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(digitaaladres__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_digitaaladres_uuid(self, queryset, name, value):
try:
digitaaladres_uuid = uuid.UUID(value)
return queryset.filter(digitaaladres__uuid=digitaaladres_uuid)
except ValueError:
return queryset.none()

def filter_partij_nummer(self, queryset, name, value):
return queryset.filter(partij__nummer=value)

def filter_partij_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(partij__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_partij_uuid(self, queryset, name, value):
try:
partij_uuid = uuid.UUID(value)
return queryset.filter(partij__uuid=partij_uuid)
except ValueError:
return queryset.none()


class ActorKlantcontactFilterSet(FilterSet):
actor__url = filters.CharFilter(
actor__url = URLViewFilter(
help_text=_("Zoek actor klantcontract object op basis van het actor url"),
method="filter_actor_url",
field_name="actor__uuid",
)
klantcontact__url = filters.CharFilter(
klantcontact__url = URLViewFilter(
help_text=_(
"Zoek actor klantcontract object op basis van het klantcontact url"
),
method="filter_klantcontact_url",
field_name="klantcontact__uuid",
)

class Meta:
Expand All @@ -247,17 +177,3 @@ class Meta:
"klantcontact__uuid",
"klantcontact__url",
)

def filter_actor_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(actor__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_klantcontact_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.rstrip("/").split("/")[-1])
return queryset.filter(klantcontact__uuid=url_uuid)
except ValueError:
return queryset.none()
Loading

0 comments on commit 3ec6e17

Please sign in to comment.