Skip to content

Commit

Permalink
Merge pull request #228 from open-zaak/feature/optimize_db_queries_fo…
Browse files Browse the repository at this point in the history
…r_statustype

optimize db queries - part 5 (statustype resource)
  • Loading branch information
joeribekker committed Nov 19, 2019
2 parents c77385e + 728f5c5 commit 7a0840e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/openzaak/components/catalogi/api/viewsets/statustype.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ class StatusTypeViewSet(
concept betreft.
"""

queryset = StatusType.objects.all().order_by("-pk")
queryset = (
StatusType.objects.select_related("zaaktype")
.prefetch_related("zaaktype__statustypen")
.order_by("-pk")
.all()
)
serializer_class = StatusTypeSerializer
filterset_class = StatusTypeFilter
lookup_field = "uuid"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 2.2.4 on 2019-11-12 09:20

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("catalogi", "0006_auto_20191024_1000"),
]

operations = [
migrations.AlterModelOptions(
name="statustype",
options={
"ordering": ("zaaktype", "-statustypevolgnummer"),
"verbose_name": "Statustype",
"verbose_name_plural": "Statustypen",
},
),
]
14 changes: 4 additions & 10 deletions src/openzaak/components/catalogi/models/statustype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import Max
from django.utils.translation import ugettext_lazy as _


Expand Down Expand Up @@ -87,19 +86,14 @@ class Meta:
unique_together = ("zaaktype", "statustypevolgnummer")
verbose_name = _("Statustype")
verbose_name_plural = _("Statustypen")
ordering = ("zaaktype", "-statustypevolgnummer")

def is_eindstatus(self):
"""
Een `StatusType` betreft een eindstatus als het volgnummer van het
`StatusType` de hoogste is binnen het `ZaakType`.
# TODO: Can be cached on the model.
Sorting by statustypevolgnummer desc is performed in StatusType.Meta.ordering
"""
max_statustypevolgnummer = self.zaaktype.statustypen.aggregate(
result=Max("statustypevolgnummer")
)["result"]

return max_statustypevolgnummer == self.statustypevolgnummer
last_statustype = self.zaaktype.statustypen.first()
return last_statustype == self

def __str__(self):
return "{} - {}".format(self.zaaktype, self.statustypevolgnummer)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.test import TestCase

from ..factories import StatusTypeFactory, ZaakTypeFactory


class StatustypeTests(TestCase):
def test_fill_in_default_archiefnominatie(self):
"""
Assert that the is_eindstatus is calculated correctly
"""
zaaktype = ZaakTypeFactory.create()
statustype1 = StatusTypeFactory.create(zaaktype=zaaktype)
statustype2 = StatusTypeFactory.create(zaaktype=zaaktype)
statustype3 = StatusTypeFactory.create(zaaktype=zaaktype)

self.assertEqual(statustype1.is_eindstatus(), False)
self.assertEqual(statustype2.is_eindstatus(), False)
self.assertEqual(statustype3.is_eindstatus(), True)

0 comments on commit 7a0840e

Please sign in to comment.