Skip to content

Commit

Permalink
🎨 [#182] changed ordered though model to normal though model
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Aug 30, 2024
1 parent bdf63f9 commit a282a9f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 57 deletions.
18 changes: 0 additions & 18 deletions src/openklant/components/klantinteracties/admin/actoren.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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 ..models.internetaken import InterneTakenActorenThoughModel


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


class ActorThoughTabularInlineAdmin(OrderedTabularInline):
model = InterneTakenActorenThoughModel
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from ordered_model.admin import OrderedInlineModelAdminMixin

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


class InterneTaakInlineAdmin(admin.StackedInline):
Expand All @@ -30,4 +29,3 @@ class InterneTaakAdmin(OrderedInlineModelAdminMixin, admin.ModelAdmin):
"uuid",
"toegewezen_op",
)
inlines = (ActorThoughTabularInlineAdmin,)
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ class Meta:
def to_representation(self, instance):
response = super().to_representation(instance)
response["toegewezen_aan_actor"] = ActorForeignKeySerializer(
instance.actoren.order_by("internetakenactorenthoughmodel__order").first(),
instance.actoren.order_by("internetakenactorenthoughmodel__pk").first(),
context={**self.context},
).data

response["toegewezen_aan_actoren"] = ActorForeignKeySerializer(
instance.actoren.all().order_by("internetakenactorenthoughmodel__order"),
instance.actoren.all().order_by("internetakenactorenthoughmodel__pk"),
context={**self.context},
many=True,
).data
Expand Down Expand Up @@ -158,11 +158,8 @@ def create(self, validated_data):

internetaak = super().create(validated_data)
if actoren:
bulk_create_instances = [
InterneTakenActorenThoughModel(internetaak=internetaak, actor=actor)
for actor in self._get_actoren(actoren)
]
InterneTakenActorenThoughModel.objects.bulk_create(bulk_create_instances)
for actor in self._get_actoren(actoren):
internetaak.actoren.add(actor)

return internetaak

Expand All @@ -171,11 +168,8 @@ def update(self, instance, validated_data):
if "actoren" in validated_data:
actoren = validated_data.pop("actoren")
instance.actoren.clear()
bulk_create_instances = [
InterneTakenActorenThoughModel(internetaak=instance, actor=actor)
for actor in self._get_actoren(actoren)
]
InterneTakenActorenThoughModel.objects.bulk_create(bulk_create_instances)
for actor in self._get_actoren(actoren):
instance.actoren.add(actor)

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 @@ -464,29 +464,51 @@ def test_update_internetaak_with_multiple_actoren(self):

response = self.client.put(detail_url, data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.json()
response_data = response.json()

self.assertEqual(data["toegewezenAanActor"]["uuid"], str(actor3.uuid))
self.assertEqual(len(data["toegewezenAanActoren"]), 2)
self.assertEqual(response_data["toegewezenAanActor"]["uuid"], str(actor3.uuid))
self.assertEqual(len(response_data["toegewezenAanActoren"]), 2)
self.assertEqual(
data["toegewezenAanActoren"][0]["uuid"],
response_data["toegewezenAanActoren"][0]["uuid"],
str(actor3.uuid),
)
self.assertEqual(
data["toegewezenAanActoren"][1]["uuid"],
response_data["toegewezenAanActoren"][1]["uuid"],
str(actor2.uuid),
)
self.assertEqual(
data["aanleidinggevendKlantcontact"]["uuid"], str(klantcontact2.uuid)
response_data["aanleidinggevendKlantcontact"]["uuid"],
str(klantcontact2.uuid),
)
self.assertEqual(data["nummer"], "9999999999")
self.assertEqual(data["gevraagdeHandeling"], "changed")
self.assertEqual(data["toelichting"], "changed")
self.assertEqual(data["status"], "verwerkt")
self.assertEqual(response_data["nummer"], "9999999999")
self.assertEqual(response_data["gevraagdeHandeling"], "changed")
self.assertEqual(response_data["toelichting"], "changed")
self.assertEqual(response_data["status"], "verwerkt")
self.assertTrue(
InterneTaak.objects.filter(afgehandeld_op="2024-01-01T12:00:00Z").exists()
)

with self.subTest(
"update_toegewezen_aan_actor_resoltes_in_one_actor_being_set"
):
# no toegewezen_aan_actoren and toegewezen_aan_actor
del data["toegewezenAanActoren"]
data = {
"toegewezenAanActor": {"uuid": str(actor.uuid)},
}
response = self.client.patch(detail_url, data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()

self.assertEqual(
response_data["toegewezenAanActor"]["uuid"], str(actor.uuid)
)
self.assertEqual(len(response_data["toegewezenAanActoren"]), 1)
self.assertEqual(
response_data["toegewezenAanActoren"][0]["uuid"],
str(actor.uuid),
)

def test_partial_update_internetaak(self):
actor = ActorFactory.create()
klantcontact = KlantcontactFactory.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
(
"order",
models.PositiveIntegerField(
db_index=True, editable=False, verbose_name="order"
),
),
(
"actor",
models.ForeignKey(
Expand All @@ -43,9 +37,7 @@ class Migration(migrations.Migration):
),
),
],
options={
"ordering": ("actor", "order"),
},
options={"ordering": ("pk",)},
),
migrations.AddField(
model_name="internetaak",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from ordered_model.models import OrderedModel

from openklant.components.utils.number_generator import number_generator

from .constants import Taakstatus
Expand Down Expand Up @@ -101,13 +99,11 @@ def __str__(self):
return f"{self.klantcontact} - ({self.nummer})"


# Added for deprecated toegewezen_aan_actor field to return correct order
class InterneTakenActorenThoughModel(OrderedModel):
class InterneTakenActorenThoughModel(models.Model):
actor = models.ForeignKey("klantinteracties.Actor", on_delete=models.CASCADE)
internetaak = models.ForeignKey(
"klantinteracties.InterneTaak", on_delete=models.CASCADE
)
order_with_respect_to = "internetaak"

class Meta:
ordering = ("actor", "order")
ordering = ("pk",)

0 comments on commit a282a9f

Please sign in to comment.