From 1ee8d6d16836c25da4f75bcaf59d6e3f5b24a423 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 30 Mar 2021 15:05:00 +0200 Subject: Container: fix index generation when only stationary containers are availables --- archaeological_warehouse/models.py | 13 +++++++------ archaeological_warehouse/tests.py | 8 ++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 7b694b799..5a852a367 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -1708,11 +1708,12 @@ class Container( q = q.exclude(id=self.id) if self.index and not q.count(): return - q = Container.objects.filter(location=self.location) - if q.count(): - self.index = int(q.all().aggregate(Max("index"))["index__max"]) + 1 - else: - self.index = 1 + q = Container.objects.filter(location=self.location, index__isnull=False) + self.index = ( + int(q.all().aggregate(Max("index"))["index__max"]) + 1 + if q.count() + else 1 + ) if not self.cached_division: self.cached_division = self._generate_cached_division() @@ -1738,7 +1739,7 @@ class Container( if not self.index and not self.container_type.stationary: self.skip_history_when_saving = True q = ( - Container.objects.filter(location=self.location) + Container.objects.filter(location=self.location, index__isnull=False) .exclude(pk=self.pk) .order_by("-index") ) diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py index 503057cdf..e18d56fae 100644 --- a/archaeological_warehouse/tests.py +++ b/archaeological_warehouse/tests.py @@ -1056,6 +1056,10 @@ class ContainerTest(FindInit, TestCase): base_index = q.all()[0].index else: base_index = 0 + container_3 = models.Container.objects.create( + reference="Ref 3", location=self.main_warehouse, container_type=ct2 + ) + self.assertEqual(container_3.index, None) container_1 = models.Container.objects.create( reference="Ref 1", location=self.main_warehouse, container_type=ct ) @@ -1068,10 +1072,6 @@ class ContainerTest(FindInit, TestCase): self.assertEqual( models.Container.objects.get(pk=container_2.pk).index, base_index + 2 ) - container_3 = models.Container.objects.create( - reference="Ref 3", location=self.main_warehouse, container_type=ct2 - ) - self.assertEqual(container_3.index, None) self.assertEqual(models.Container.objects.get(pk=container_3.pk).index, None) container_4 = models.Container.objects.create( reference="Ref 4", location=self.main_warehouse, container_type=ct2 -- cgit v1.2.3