Skip to content

Commit

Permalink
✨ [#182] changed internetaken actor field to actoren
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Aug 8, 2024
1 parent 81f16aa commit 69cf949
Show file tree
Hide file tree
Showing 13 changed files with 426 additions and 44 deletions.
20 changes: 18 additions & 2 deletions src/openklant/components/klantinteracties/admin/actoren.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from ordered_model.admin import OrderedTabularInline

from ..models.actoren import (
Actor,
ActorKlantcontact,
GeautomatiseerdeActor,
Medewerker,
OrganisatorischeEenheid,
)
from .internetaken import InterneTaakInlineAdmin
from ..models.internetaken import InterneActorenThoughModel


class GeautomatiseerdeActorInlineAdmin(admin.StackedInline):
Expand All @@ -35,6 +37,21 @@ class ActorKlantcontactInlineAdmin(admin.StackedInline):
extra = 0


class ActorThoughTabularInlineAdmin(OrderedTabularInline):
model = InterneActorenThoughModel
fields = (
"actor",
"order",
"move_up_down_links",
)
readonly_fields = (
"order",
"move_up_down_links",
)
ordering = ("order",)
extra = 0


@admin.register(Actor)
class ActorAdmin(admin.ModelAdmin):
list_display = (
Expand All @@ -52,7 +69,6 @@ class ActorAdmin(admin.ModelAdmin):
GeautomatiseerdeActorInlineAdmin,
MedewerkerInlineAdmin,
OrganisatorischeEenheidInlineAdmin,
InterneTaakInlineAdmin,
)
fieldsets = (
(
Expand Down
13 changes: 10 additions & 3 deletions src/openklant/components/klantinteracties/admin/internetaken.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.contrib import admin

from ordered_model.admin import OrderedInlineModelAdminMixin

from ..models.internetaken import InterneTaak
from .actoren import ActorThoughTabularInlineAdmin


class InterneTaakInlineAdmin(admin.StackedInline):
Expand All @@ -11,7 +14,7 @@ class InterneTaakInlineAdmin(admin.StackedInline):


@admin.register(InterneTaak)
class InterneTaakAdmin(admin.ModelAdmin):
class InterneTaakAdmin(OrderedInlineModelAdminMixin, admin.ModelAdmin):
readonly_fields = ("uuid",)
list_display = (
"nummer",
Expand All @@ -20,7 +23,11 @@ class InterneTaakAdmin(admin.ModelAdmin):
"afgehandeld_op",
)
list_filter = (
"actor",
"actoren",
"status",
)
readonly_fields = ("toegewezen_op",)
readonly_fields = (
"uuid",
"toegewezen_op",
)
inlines = (ActorThoughTabularInlineAdmin,)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class InternetaakFilterSet(FilterSet):
toegewezen_aan_actor__uuid = filters.UUIDFilter(
help_text=_("Zoek internetaak object op basis van het toegewezen actor uuid"),
field_name="actor__uuid",
field_name="actoren__uuid",
)
toegewezen_aan_actor__url = filters.CharFilter(
help_text=_("Zoek internetaak object op basis van het toegewezen actor url"),
Expand All @@ -35,7 +35,7 @@ class Meta:
"nummer",
"status",
"toegewezen_op",
"actor__naam",
"actoren__naam",
"klantcontact__uuid",
"klantcontact__nummer",
"toegewezen_aan_actor__uuid",
Expand All @@ -47,7 +47,7 @@ class Meta:
def filter_toegewezen_aan_actor_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.split("/")[-1])
return queryset.filter(actor__uuid=url_uuid)
return queryset.filter(actoren__uuid=url_uuid)
except ValueError:
return queryset.none()

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

from drf_spectacular.utils import extend_schema_serializer
from glom import PathAccessError, glom
from rest_framework import serializers

from openklant.components.klantinteracties.api.serializers.actoren import (
Expand All @@ -12,7 +14,10 @@
from openklant.components.klantinteracties.api.validators import internetaak_exists
from openklant.components.klantinteracties.models.actoren import Actor
from openklant.components.klantinteracties.models.constants import Taakstatus
from openklant.components.klantinteracties.models.internetaken import InterneTaak
from openklant.components.klantinteracties.models.internetaken import (
InterneActorenThoughModel,
InterneTaak,
)
from openklant.components.klantinteracties.models.klantcontacten import Klantcontact


Expand All @@ -33,12 +38,21 @@ class Meta:
}


@extend_schema_serializer(deprecate_fields=["toegewezen_aan_actor"])
class InterneTaakSerializer(serializers.HyperlinkedModelSerializer):
toegewezen_aan_actor = ActorForeignKeySerializer(
required=True,
required=False,
allow_null=False,
help_text=_("Actor die een interne taak toegewezen kreeg."),
source="actor",
help_text=_("Eerste actor die een interne taak toegewezen kreeg."),
source="actoren.first",
many=False,
)
toegewezen_aan_actoren = ActorForeignKeySerializer(
required=False,
allow_null=False,
help_text=_("Actoren die een interne taak toegewezen kreeg."),
source="actoren",
many=True,
)
aanleidinggevend_klantcontact = KlantcontactForeignKeySerializer(
required=True,
Expand All @@ -56,6 +70,7 @@ class Meta:
"gevraagde_handeling",
"aanleidinggevend_klantcontact",
"toegewezen_aan_actor",
"toegewezen_aan_actoren",
"toelichting",
"status",
"toegewezen_op",
Expand All @@ -70,7 +85,52 @@ class Meta:
},
}

def to_representation(self, instance):
response = super().to_representation(instance)
response["toegewezen_aan_actor"] = ActorForeignKeySerializer(
instance.actoren.order_by("interneactorenthoughmodel__order").first(),
context={**self.context},
).data

response["toegewezen_aan_actoren"] = ActorForeignKeySerializer(
instance.actoren.all().order_by("interneactorenthoughmodel__order"),
context={**self.context},
many=True,
).data
return response

# TODO: remove when depricated actoren field gets removed
def _validate_actoren(self):
toegewezen_aan_actor = "toegewezen_aan_actor" in self.initial_data
toegewezen_aan_actoren = "toegewezen_aan_actoren" in self.initial_data

if toegewezen_aan_actor == toegewezen_aan_actoren:
if toegewezen_aan_actor:
message = _(
"`toegewezen_aan_actor` en `toegewezen_aan_actoren` mag niet beiden gebruikt worden."
)
else:
message = _(
"`toegewezen_aan_actor` of `toegewezen_aan_actoren` is required (mag niet beiden gebruiken)."
)

# If patch don't raise required error
if self.context.get("request").method == "PATCH":
return

raise serializers.ValidationError(message)

# TODO: remove when depricated actoren field gets removed
def _get_actoren(self, actoren):
if isinstance(actoren, list):
actor_uuids = [str(actor.get("uuid")) for actor in actoren]
else:
actor_uuids = [glom(actoren, "first.uuid", skip_exc=PathAccessError)]

return [Actor.objects.get(uuid=uuid) for uuid in actor_uuids]

def validate(self, attrs):
self._validate_actoren()
status = attrs.get("status", None)
if status is None and self.instance is not None:
status = self.instance.status
Expand All @@ -89,21 +149,33 @@ def validate(self, attrs):

@transaction.atomic
def create(self, validated_data):
actor_uuid = str(validated_data.pop("actor").get("uuid"))
actoren = validated_data.pop("actoren", None)
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)
internetaak = super().create(validated_data)
if actoren:
bulk_create_instances = [
InterneActorenThoughModel(internetaak=internetaak, actor=actor)
for actor in self._get_actoren(actoren)
]
InterneActorenThoughModel.objects.bulk_create(bulk_create_instances)

return internetaak

@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 "actoren" in validated_data:
actoren = validated_data.pop("actoren")
instance.actoren.clear()
bulk_create_instances = [
InterneActorenThoughModel(internetaak=instance, actor=actor)
for actor in self._get_actoren(actoren)
]
InterneActorenThoughModel.objects.bulk_create(bulk_create_instances)

if "klantcontact" in validated_data:
if klantcontact := validated_data.pop("klantcontact", None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1093,19 +1093,19 @@ def setUp(self):
self.klantcontact5,
) = KlantcontactFactory.create_batch(5)
self.internetaak = InterneTaakFactory.create(
actor=self.actor, klantcontact=self.klantcontact
actoren=[self.actor], klantcontact=self.klantcontact
)
self.internetaak2 = InterneTaakFactory.create(
actor=self.actor2, klantcontact=self.klantcontact2
actoren=[self.actor2], klantcontact=self.klantcontact2
)
self.internetaak3 = InterneTaakFactory.create(
actor=self.actor3, klantcontact=self.klantcontact3
actoren=[self.actor3], klantcontact=self.klantcontact3
)
self.internetaak4 = InterneTaakFactory.create(
actor=self.actor4, klantcontact=self.klantcontact4
actoren=[self.actor4], klantcontact=self.klantcontact4
)
self.internetaak5 = InterneTaakFactory.create(
actor=self.actor5, klantcontact=self.klantcontact5
actoren=[self.actor5], klantcontact=self.klantcontact5
)

def test_filter_toegewezen_aan_actor_url(self):
Expand Down
Loading

0 comments on commit 69cf949

Please sign in to comment.