Skip to content

Commit

Permalink
✨ [#100] added reverse lookup fields
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Nov 16, 2023
1 parent 7d284c3 commit 877ffe9
Show file tree
Hide file tree
Showing 27 changed files with 6,649 additions and 1,378 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import uuid

from django.utils.translation import gettext_lazy as _

from django_filters.rest_framework import FilterSet, filters

from openklant.components.klantinteracties.models.klantcontacten import (
Betrokkene,
Klantcontact,
)


class KlantcontactFilterSet(FilterSet):
had_betrokkene__url = filters.CharFilter(
help_text=_("Zoek klantcontract object op basis van het betrokkene url"),
method="filter_betrokkene_url",
)
had_betrokkene__uuid = filters.CharFilter(
help_text=_("Zoek klantcontract object op basis van het betrokkene uuid"),
method="filter_betrokkene_uuid",
)
inhoud = filters.CharFilter(
lookup_expr="icontains",
help_text=_("Zoek klantcontacten met specifieke tekst in inhoud"),
)
onderwerp = filters.CharFilter(
lookup_expr="icontains",
help_text=_("Zoek klantcontacten met specifieke tekst in onderwerp"),
)

class Meta:
model = Klantcontact
fields = (
"had_betrokkene__url",
"had_betrokkene__uuid",
"nummer",
"kanaal",
"inhoud",
"onderwerp",
)

def filter_betrokkene_uuid(self, queryset, name, value):
try:
betrokkene_uuid = uuid.UUID(value)
return queryset.filter(betrokkene__uuid=betrokkene_uuid)
except ValueError:
return queryset.none()

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


class BetrokkeneFilterSet(FilterSet):
klantcontact__nummer = filters.CharFilter(
help_text=_("Zoek betrokkene object op basis van het klantcontact nummer"),
method="filter_klantcontact_nummer",
)
klantcontact__url = filters.CharFilter(
help_text=_("Zoek betrokkene object op basis van het klantcontact url"),
method="filter_klantcontact_url",
)
klantcontact__uuid = filters.CharFilter(
help_text=_("Zoek betrokkene object op basis van het klantcontact uuid"),
method="filter_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(
help_text=_("Zoek betrokkene object op basis van het digitaaladres url"),
method="filter_digitaaladres_url",
)
verstrektedigitaal_adres__uuid = filters.CharFilter(
help_text=_("Zoek betrokkene object op basis van het digitaaladres uuid"),
method="filter_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(
help_text=_("Zoek betrokkene object op basis van het partij url"),
method="filter_partij_url",
)
was_partij__uuid = filters.CharFilter(
help_text=_("Zoek betrokkene object op basis van het partij uuid"),
method="filter_partij_uuid",
)

class Meta:
model = Betrokkene
fields = (
"klantcontact__nummer",
"klantcontact__uuid",
"klantcontact__url",
"verstrektedigitaal_adres__adres",
"verstrektedigitaal_adres__uuid",
"verstrektedigitaal_adres__url",
"was_partij__nummer",
"was_partij__url",
"was_partij__uuid",
)

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

def filter_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_klantcontact_nummer(self, queryset, name, value):
try:
return queryset.filter(klantcontact__nummer=value)
except ValueError:
return queryset.none()

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.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.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()
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ class Meta:

@transaction.atomic
def update(self, instance, validated_data):
if actor := validated_data.pop("actor", None):
validated_data["actor"] = Actor.objects.get(uuid=str(actor.get("uuid")))
if "actor" in validated_data:
if actor := validated_data.pop("actor", None):
validated_data["actor"] = Actor.objects.get(uuid=str(actor.get("uuid")))

return super().update(instance, validated_data)

Expand Down Expand Up @@ -136,8 +137,9 @@ class Meta:

@transaction.atomic
def update(self, instance, validated_data):
if actor := validated_data.pop("actor", None):
validated_data["actor"] = Actor.objects.get(uuid=str(actor.get("uuid")))
if "actor" in validated_data:
if actor := validated_data.pop("actor", None):
validated_data["actor"] = Actor.objects.get(uuid=str(actor.get("uuid")))

return super().update(instance, validated_data)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.db import transaction
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers

from openklant.components.klantinteracties.api.validators import digitaal_adres_exists
Expand All @@ -22,11 +25,25 @@ class Meta:


class DigitaalAdresSerializer(serializers.HyperlinkedModelSerializer):
from openklant.components.klantinteracties.api.serializers.klantcontacten import (
BetrokkeneForeignKeySerializer,
)

betrokkene = BetrokkeneForeignKeySerializer(
required=True,
allow_null=True,
help_text=_(
"Digitaal adres dat een betrokkene bij klantcontact verstrekte voor gebruik bij "
"opvolging van een klantcontact."
),
)

class Meta:
model = DigitaalAdres
fields = (
"uuid",
"url",
"betrokkene",
"adres",
"soort_digitaal_adres",
"omschrijving",
Expand All @@ -39,3 +56,30 @@ class Meta:
"help_text": "De unieke URL van dit digitaal adres binnen deze API.",
},
}

@transaction.atomic
def update(self, instance, validated_data):
from openklant.components.klantinteracties.models.klantcontacten import (
Betrokkene,
)

if "betrokkene" in validated_data:
if betrokkene := validated_data.pop("betrokkene", None):
betrokkene = Betrokkene.objects.get(uuid=str(betrokkene.get("uuid")))

validated_data["betrokkene"] = betrokkene

return super().update(instance, validated_data)

@transaction.atomic
def create(self, validated_data):
from openklant.components.klantinteracties.models.klantcontacten import (
Betrokkene,
)

if betrokkene := validated_data.pop("betrokkene", None):
validated_data["betrokkene"] = Betrokkene.objects.get(
uuid=str(betrokkene.get("uuid"))
)

return super().create(validated_data)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import transaction
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers
Expand All @@ -8,7 +9,10 @@
from openklant.components.klantinteracties.api.serializers.klantcontacten import (
KlantcontactForeignKeySerializer,
)
from openklant.components.klantinteracties.api.validators import internetaak_exists
from openklant.components.klantinteracties.models.actoren import Actor
from openklant.components.klantinteracties.models.internetaken import InterneTaak
from openklant.components.klantinteracties.models.klantcontacten import Klantcontact


class InterneTaakForeignKeySerializer(serializers.HyperlinkedModelSerializer):
Expand All @@ -19,7 +23,7 @@ class Meta:
"url",
)
extra_kwargs = {
"uuid": {"required": True},
"uuid": {"required": True, "validators": [internetaak_exists]},
"url": {
"view_name": "internetaak-detail",
"lookup_field": "uuid",
Expand Down Expand Up @@ -61,3 +65,64 @@ class Meta:
"help_text": "De unieke URL van deze interne taak binnen deze API.",
},
}

@transaction.atomic
def create(self, validated_data):
actor_uuid = str(validated_data.pop("actor").get("uuid"))
klantcontact_uuid = str(validated_data.pop("klantcontact").get("uuid"))

validated_data["actor"] = Actor.objects.get(uuid=actor_uuid)
validated_data["klantcontact"] = Klantcontact.objects.get(
uuid=klantcontact_uuid
)

return super().create(validated_data)

@transaction.atomic
def update(self, instance, validated_data):
if "actor" in validated_data:
if actor := validated_data.pop("actor", None):
validated_data["actor"] = Actor.objects.get(uuid=str(actor.get("uuid")))

if "klantcontact" in validated_data:
if klantcontact := validated_data.pop("klantcontact", None):
validated_data["klantcontact"] = Klantcontact.objects.get(
uuid=str(klantcontact.get("uuid"))
)

return super().update(instance, validated_data)


class KlantcontactInterneTaakSerializer(serializers.HyperlinkedModelSerializer):
actor = ActorForeignKeySerializer(
required=True,
allow_null=False,
help_text=_("Actor die een interne taak toegewezen kreeg."),
)
klantcontact = KlantcontactForeignKeySerializer(
read_only=True,
allow_null=False,
help_text=_("Klantcontact dat leidde tot een interne taak."),
)

class Meta:
model = InterneTaak
fields = (
"uuid",
"url",
"nummer",
"gevraagde_handeling",
"actor",
"klantcontact",
"toelichting",
"status",
"toegewezen_op",
)
extra_kwargs = {
"uuid": {"read_only": True},
"url": {
"view_name": "internetaak-detail",
"lookup_field": "uuid",
"help_text": "De unieke URL van deze interne taak binnen deze API.",
},
}
Loading

0 comments on commit 877ffe9

Please sign in to comment.