Skip to content

Commit a282a9f

Browse files
committed
🎨 [#182] changed ordered though model to normal though model
1 parent bdf63f9 commit a282a9f

File tree

6 files changed

+41
-57
lines changed

6 files changed

+41
-57
lines changed

src/openklant/components/klantinteracties/admin/actoren.py

-18
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
from django.contrib import admin
22
from django.utils.translation import gettext_lazy as _
33

4-
from ordered_model.admin import OrderedTabularInline
5-
64
from ..models.actoren import (
75
Actor,
86
ActorKlantcontact,
97
GeautomatiseerdeActor,
108
Medewerker,
119
OrganisatorischeEenheid,
1210
)
13-
from ..models.internetaken import InterneTakenActorenThoughModel
1411

1512

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

3936

40-
class ActorThoughTabularInlineAdmin(OrderedTabularInline):
41-
model = InterneTakenActorenThoughModel
42-
fields = (
43-
"actor",
44-
"order",
45-
"move_up_down_links",
46-
)
47-
readonly_fields = (
48-
"order",
49-
"move_up_down_links",
50-
)
51-
ordering = ("order",)
52-
extra = 0
53-
54-
5537
@admin.register(Actor)
5638
class ActorAdmin(admin.ModelAdmin):
5739
list_display = (

src/openklant/components/klantinteracties/admin/internetaken.py

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from ordered_model.admin import OrderedInlineModelAdminMixin
44

55
from ..models.internetaken import InterneTaak
6-
from .actoren import ActorThoughTabularInlineAdmin
76

87

98
class InterneTaakInlineAdmin(admin.StackedInline):
@@ -30,4 +29,3 @@ class InterneTaakAdmin(OrderedInlineModelAdminMixin, admin.ModelAdmin):
3029
"uuid",
3130
"toegewezen_op",
3231
)
33-
inlines = (ActorThoughTabularInlineAdmin,)

src/openklant/components/klantinteracties/api/serializers/internetaken.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ class Meta:
8888
def to_representation(self, instance):
8989
response = super().to_representation(instance)
9090
response["toegewezen_aan_actor"] = ActorForeignKeySerializer(
91-
instance.actoren.order_by("internetakenactorenthoughmodel__order").first(),
91+
instance.actoren.order_by("internetakenactorenthoughmodel__pk").first(),
9292
context={**self.context},
9393
).data
9494

9595
response["toegewezen_aan_actoren"] = ActorForeignKeySerializer(
96-
instance.actoren.all().order_by("internetakenactorenthoughmodel__order"),
96+
instance.actoren.all().order_by("internetakenactorenthoughmodel__pk"),
9797
context={**self.context},
9898
many=True,
9999
).data
@@ -158,11 +158,8 @@ def create(self, validated_data):
158158

159159
internetaak = super().create(validated_data)
160160
if actoren:
161-
bulk_create_instances = [
162-
InterneTakenActorenThoughModel(internetaak=internetaak, actor=actor)
163-
for actor in self._get_actoren(actoren)
164-
]
165-
InterneTakenActorenThoughModel.objects.bulk_create(bulk_create_instances)
161+
for actor in self._get_actoren(actoren):
162+
internetaak.actoren.add(actor)
166163

167164
return internetaak
168165

@@ -171,11 +168,8 @@ def update(self, instance, validated_data):
171168
if "actoren" in validated_data:
172169
actoren = validated_data.pop("actoren")
173170
instance.actoren.clear()
174-
bulk_create_instances = [
175-
InterneTakenActorenThoughModel(internetaak=instance, actor=actor)
176-
for actor in self._get_actoren(actoren)
177-
]
178-
InterneTakenActorenThoughModel.objects.bulk_create(bulk_create_instances)
171+
for actor in self._get_actoren(actoren):
172+
instance.actoren.add(actor)
179173

180174
if "klantcontact" in validated_data:
181175
if klantcontact := validated_data.pop("klantcontact", None):

src/openklant/components/klantinteracties/api/tests/test_internetaken.py

+32-10
Original file line numberDiff line numberDiff line change
@@ -464,29 +464,51 @@ def test_update_internetaak_with_multiple_actoren(self):
464464

465465
response = self.client.put(detail_url, data)
466466
self.assertEqual(response.status_code, status.HTTP_200_OK)
467-
data = response.json()
467+
response_data = response.json()
468468

469-
self.assertEqual(data["toegewezenAanActor"]["uuid"], str(actor3.uuid))
470-
self.assertEqual(len(data["toegewezenAanActoren"]), 2)
469+
self.assertEqual(response_data["toegewezenAanActor"]["uuid"], str(actor3.uuid))
470+
self.assertEqual(len(response_data["toegewezenAanActoren"]), 2)
471471
self.assertEqual(
472-
data["toegewezenAanActoren"][0]["uuid"],
472+
response_data["toegewezenAanActoren"][0]["uuid"],
473473
str(actor3.uuid),
474474
)
475475
self.assertEqual(
476-
data["toegewezenAanActoren"][1]["uuid"],
476+
response_data["toegewezenAanActoren"][1]["uuid"],
477477
str(actor2.uuid),
478478
)
479479
self.assertEqual(
480-
data["aanleidinggevendKlantcontact"]["uuid"], str(klantcontact2.uuid)
480+
response_data["aanleidinggevendKlantcontact"]["uuid"],
481+
str(klantcontact2.uuid),
481482
)
482-
self.assertEqual(data["nummer"], "9999999999")
483-
self.assertEqual(data["gevraagdeHandeling"], "changed")
484-
self.assertEqual(data["toelichting"], "changed")
485-
self.assertEqual(data["status"], "verwerkt")
483+
self.assertEqual(response_data["nummer"], "9999999999")
484+
self.assertEqual(response_data["gevraagdeHandeling"], "changed")
485+
self.assertEqual(response_data["toelichting"], "changed")
486+
self.assertEqual(response_data["status"], "verwerkt")
486487
self.assertTrue(
487488
InterneTaak.objects.filter(afgehandeld_op="2024-01-01T12:00:00Z").exists()
488489
)
489490

491+
with self.subTest(
492+
"update_toegewezen_aan_actor_resoltes_in_one_actor_being_set"
493+
):
494+
# no toegewezen_aan_actoren and toegewezen_aan_actor
495+
del data["toegewezenAanActoren"]
496+
data = {
497+
"toegewezenAanActor": {"uuid": str(actor.uuid)},
498+
}
499+
response = self.client.patch(detail_url, data)
500+
self.assertEqual(response.status_code, status.HTTP_200_OK)
501+
response_data = response.json()
502+
503+
self.assertEqual(
504+
response_data["toegewezenAanActor"]["uuid"], str(actor.uuid)
505+
)
506+
self.assertEqual(len(response_data["toegewezenAanActoren"]), 1)
507+
self.assertEqual(
508+
response_data["toegewezenAanActoren"][0]["uuid"],
509+
str(actor.uuid),
510+
)
511+
490512
def test_partial_update_internetaak(self):
491513
actor = ActorFactory.create()
492514
klantcontact = KlantcontactFactory.create()

src/openklant/components/klantinteracties/migrations/0016_internetaak_actoren.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ class Migration(migrations.Migration):
2222
verbose_name="ID",
2323
),
2424
),
25-
(
26-
"order",
27-
models.PositiveIntegerField(
28-
db_index=True, editable=False, verbose_name="order"
29-
),
30-
),
3125
(
3226
"actor",
3327
models.ForeignKey(
@@ -43,9 +37,7 @@ class Migration(migrations.Migration):
4337
),
4438
),
4539
],
46-
options={
47-
"ordering": ("actor", "order"),
48-
},
40+
options={"ordering": ("pk",)},
4941
),
5042
migrations.AddField(
5143
model_name="internetaak",

src/openklant/components/klantinteracties/models/internetaken.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from django.utils import timezone
66
from django.utils.translation import gettext_lazy as _
77

8-
from ordered_model.models import OrderedModel
9-
108
from openklant.components.utils.number_generator import number_generator
119

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

103101

104-
# Added for deprecated toegewezen_aan_actor field to return correct order
105-
class InterneTakenActorenThoughModel(OrderedModel):
102+
class InterneTakenActorenThoughModel(models.Model):
106103
actor = models.ForeignKey("klantinteracties.Actor", on_delete=models.CASCADE)
107104
internetaak = models.ForeignKey(
108105
"klantinteracties.InterneTaak", on_delete=models.CASCADE
109106
)
110-
order_with_respect_to = "internetaak"
111107

112108
class Meta:
113-
ordering = ("actor", "order")
109+
ordering = ("pk",)

0 commit comments

Comments
 (0)