Skip to content

Commit

Permalink
🎨 [#164] changed inline uuid fields into endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Feb 2, 2024
1 parent c952db1 commit 3f8172c
Show file tree
Hide file tree
Showing 22 changed files with 1,081 additions and 421 deletions.
7 changes: 7 additions & 0 deletions src/openklant/components/klantinteracties/admin/actoren.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ..admin.internezaken import InterneTaakInlineAdmin
from ..models.actoren import (
Actor,
ActorKlantcontact,
GeautomatiseerdeActor,
Medewerker,
OrganisatorischeEenheid,
Expand All @@ -28,6 +29,11 @@ class OrganisatorischeEenheidInlineAdmin(admin.StackedInline):
extra = 0


class ActorKlantcontactInlineAdmin(admin.StackedInline):
model = ActorKlantcontact
extra = 0


@admin.register(Actor)
class ActorAdmin(admin.ModelAdmin):
list_display = (
Expand All @@ -41,6 +47,7 @@ class ActorAdmin(admin.ModelAdmin):
)
search_fields = ("naam",)
inlines = (
ActorKlantcontactInlineAdmin,
GeautomatiseerdeActorInlineAdmin,
MedewerkerInlineAdmin,
OrganisatorischeEenheidInlineAdmin,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from openklant.components.klantinteracties.models.actoren import ActorKlantcontact

from ..admin.internezaken import InterneTaakInlineAdmin
from ..models.klantcontacten import Betrokkene, Bijlage, Klantcontact, Onderwerpobject

Expand Down Expand Up @@ -136,20 +138,25 @@ class BijlageInlineAdmin(admin.StackedInline):
extra = 0


class ActorKlantcontactInlineAdmin(admin.StackedInline):
model = ActorKlantcontact
extra = 0


@admin.register(Klantcontact)
class KlantcontactAdmin(admin.ModelAdmin):
list_display = ["nummer", "kanaal", "indicatie_contact_gelukt", "betrokkene_namen"]
list_filter = [
"indicatie_contact_gelukt",
]
inlines = [
ActorKlantcontactInlineAdmin,
BetrokkeneInlineAdmin,
WasOnderwerpobjectInlineAdmin,
BijlageInlineAdmin,
InterneTaakInlineAdmin,
]
search_fields = ("nummer",)
autocomplete_fields = ["actoren"]
date_hierarchy = "plaatsgevonden_op"

@admin.display(empty_value="---")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from openklant.components.klantinteracties.api.serializers.klantcontacten import (
KlantcontactSerializer,
)
from openklant.components.klantinteracties.models.actoren import ActorKlantcontact
from openklant.components.klantinteracties.models.klantcontacten import (
Betrokkene,
Klantcontact,
Expand Down Expand Up @@ -171,3 +172,39 @@ def filter_partij_uuid(self, queryset, name, value):
return queryset.filter(partij__uuid=partij_uuid)
except ValueError:
return queryset.none()


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

class Meta:
model = ActorKlantcontact
fields = (
"actor__uuid",
"actor__url",
"klantcontact__uuid",
"klantcontact__url",
)

def filter_actor_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.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.split("/")[-1])
return queryset.filter(klantcontact__uuid=url_uuid)
except ValueError:
return queryset.none()
79 changes: 39 additions & 40 deletions src/openklant/components/klantinteracties/api/filterset/partijen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,12 @@
from openklant.components.klantinteracties.models.partijen import (
CategorieRelatie,
Partij,
Vertegenwoordigden,
)
from openklant.components.utils.filters import ExpandFilter


class PartijFilterSet(FilterSet):
werkt_voor_partij__url = filters.CharFilter(
help_text=_("Zoek partij object op basis van het vertegenwoordigde partij url"),
method="filter_werkt_voor_partij_url",
)
werkt_voor_partij__uuid = filters.CharFilter(
help_text=_(
"Zoek partij object op basis van het vertegenwoordigde partij uuid"
),
method="filter_werkt_voor_partij_uuid",
)
werkt_voor_partij__nummer = filters.CharFilter(
help_text=_(
"Zoek partij object op basis van het vertegenwoordigde partij nummer"
),
method="filter_werkt_voor_partij_nummer",
)

partij_identificator__objecttype = filters.CharFilter(
help_text=_(
"Zoek partij object op basis van het partij identificator objecttype"
Expand Down Expand Up @@ -72,9 +56,6 @@ class PartijFilterSet(FilterSet):
class Meta:
model = Partij
fields = (
"werkt_voor_partij__url",
"werkt_voor_partij__uuid",
"werkt_voor_partij__nummer",
"partij_identificator__objecttype",
"partij_identificator__soort_object_id",
"partij_identificator__object_id",
Expand All @@ -96,26 +77,6 @@ class Meta:
"correspondentieadres_land",
)

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

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

def filter_werkt_voor_partij_nummer(self, queryset, name, value):
try:
return queryset.filter(vertegenwoordigde__nummer=value)
except ValueError:
return queryset.none()

def filter_identificator_objecttype(self, queryset, name, value):
try:
return queryset.filter(
Expand Down Expand Up @@ -158,6 +119,44 @@ def filter_categorierelatie_categorie_naam(self, queryset, name, value):
return queryset.none()


class VertegenwoordigdenFilterSet(FilterSet):
vertegenwoordigende_partij__url = filters.CharFilter(
help_text=_(
"Zoek Vertegenwoordigden object op basis van het vertegenwoordigende partij url"
),
method="filter_vertegenwoordigende_partij_url",
)
vertegenwoordigde_partij__url = filters.CharFilter(
help_text=_(
"Zoek Vertegenwoordigden object op basis van het vertegenwoordigde partij url"
),
method="filter_vertegenwoordigde_partij_url",
)

class Meta:
model = Vertegenwoordigden
fields = (
"vertegenwoordigende_partij__uuid",
"vertegenwoordigende_partij__url",
"vertegenwoordigde_partij__uuid",
"vertegenwoordigde_partij__url",
)

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

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


class CategorieRelatieFilterSet(FilterSet):
partij__url = filters.CharFilter(
help_text=_("Zoek categorie relatie object op basis van de partij url"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
DigitaalAdresForeignKeySerializer,
)
from openklant.components.klantinteracties.api.validators import (
actor_klantcontact_exists,
betrokkene_exists,
bijlage_exists,
klantcontact_exists,
onderwerpobject_exists,
)
from openklant.components.klantinteracties.models.actoren import Actor
from openklant.components.klantinteracties.models.actoren import (
Actor,
ActorKlantcontact,
)
from openklant.components.klantinteracties.models.klantcontacten import (
Betrokkene,
Bijlage,
Expand Down Expand Up @@ -118,6 +122,25 @@ class Meta:
}


class ActorKlantcontactForeignkeySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ActorKlantcontact
fields = (
"uuid",
"url",
)
extra_kwargs = {
"uuid": {"required": True, "validators": [actor_klantcontact_exists]},
"url": {
"view_name": "klantinteracties:actorklantcontact-detail",
"lookup_field": "uuid",
"help_text": _(
"De unieke URL van deze actor klantcontact binnen deze API."
),
},
}


class BezoekadresSerializer(GegevensGroepSerializer):
class Meta:
model = Betrokkene
Expand Down Expand Up @@ -257,12 +280,11 @@ class KlantcontactSerializer(serializers.HyperlinkedModelSerializer):
InterneTaakForeignKeySerializer,
)

had_betrokken_actoren = ActorForeignKeySerializer(
required=True,
allow_null=False,
actor_klantcontacten = ActorKlantcontactForeignkeySerializer(
read_only=True,
help_text=_("Actor die bij een klantcontact betrokken was."),
many=True,
source="actoren",
source="actorklantcontact_set",
)
ging_over_onderwerpobjecten = OnderwerpobjectForeignKeySerializer(
read_only=True,
Expand Down Expand Up @@ -294,13 +316,14 @@ class KlantcontactSerializer(serializers.HyperlinkedModelSerializer):
inclusion_serializers = {
# 1 level
"had_betrokkenen": f"{SERIALIZER_PATH}.klantcontacten.BetrokkeneSerializer",
"had_betrokken_actoren": f"{SERIALIZER_PATH}.actoren.ActorSerializer",
"actor_klantcontacten": f"{SERIALIZER_PATH}.klantcontacten.ActorKlantcontactSerializer",
"leidde_tot_interne_taken": f"{SERIALIZER_PATH}.internetaken.InterneTaakSerializer",
"ging_over_onderwerpobjecten": f"{SERIALIZER_PATH}.klantcontacten.OnderwerpobjectSerializer",
"omvatte_bijlagen": f"{SERIALIZER_PATH}.klantcontacten.BijlageSerializer",
# 2 levels
"had_betrokkenen.was_partij": f"{SERIALIZER_PATH}.partijen.PartijSerializer",
"had_betrokkenen.digitale_adressen": f"{SERIALIZER_PATH}.digitaal_adres.DigitaalAdresSerializer",
"actor_klantcontacten.actor": f"{SERIALIZER_PATH}.actoren.ActorSerializer",
}

class Meta:
Expand All @@ -315,7 +338,7 @@ class Meta:
"nummer",
"kanaal",
"onderwerp",
"had_betrokken_actoren",
"actor_klantcontacten",
"inhoud",
"indicatie_contact_gelukt",
"taal",
Expand All @@ -331,23 +354,6 @@ class Meta:
},
}

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

return super().update(instance, validated_data)

@transaction.atomic
def create(self, validated_data):
actoren = [str(actor["uuid"]) for actor in validated_data.pop("actoren")]
validated_data["actoren"] = Actor.objects.filter(uuid__in=actoren)

return super().create(validated_data)


class OnderwerpobjectObjectidentificatorSerializer(GegevensGroepSerializer):
class Meta:
Expand Down Expand Up @@ -493,3 +499,61 @@ def create(self, validated_data):
)

return super().create(validated_data)


class ActorKlantcontactSerializer(serializers.HyperlinkedModelSerializer):
actor = ActorForeignKeySerializer(
required=True,
help_text=_("De gekoppelde 'Actor'."),
)
klantcontact = KlantcontactForeignKeySerializer(
required=True,
help_text=_("De gekoppelde 'Klantcontact'."),
)

class Meta:
model = ActorKlantcontact
fields = (
"uuid",
"url",
"actor",
"klantcontact",
)
extra_kwargs = {
"uuid": {"read_only": True},
"url": {
"view_name": "klantinteracties:actorklantcontact-detail",
"lookup_field": "uuid",
"help_text": _(
"De unieke URL van deze actor klantcontact binnen deze API."
),
},
}

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

validated_data["actor"] = actor

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)

@transaction.atomic
def create(self, validated_data):
actor_uuid = str(validated_data.pop("actor").get("uuid"))
validated_data["actor"] = Actor.objects.get(uuid=actor_uuid)

klantcontact_uuid = str(validated_data.pop("klantcontact").get("uuid"))
validated_data["klantcontact"] = Klantcontact.objects.get(
uuid=klantcontact_uuid
)

return super().create(validated_data)
Loading

0 comments on commit 3f8172c

Please sign in to comment.