diff options
-rw-r--r-- | archaeological_warehouse/models.py | 10 | ||||
-rw-r--r-- | archaeological_warehouse/tests.py | 17 |
2 files changed, 24 insertions, 3 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index a901b48c9..e130671a3 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -813,6 +813,14 @@ class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem, def _get_base_image_path(self): return self.location._get_base_image_path() + "/" + self.external_id + @classmethod + def _change_parent_location(cls, parent): + for child in cls.objects.filter(parent=parent).all(): + if child.location != parent.location: + child.location = parent.location + child.save() + cls._change_parent_location(child) + def merge(self, item, keep_old=False, exclude_fields=None): # merge child containers child_references = {} @@ -829,6 +837,7 @@ class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem, else: child.parent = self child.save() + self._change_parent_location(self) super(Container, self).merge(item, keep_old=keep_old, exclude_fields=exclude_fields) @@ -1126,6 +1135,7 @@ class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem, def save(self, *args, **kwargs): self.pre_save() super(Container, self).save(*args, **kwargs) + self._change_parent_location(self) updated = False if not self.index: diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py index a76c2763f..81b8b3df5 100644 --- a/archaeological_warehouse/tests.py +++ b/archaeological_warehouse/tests.py @@ -531,6 +531,12 @@ class ContainerTest(FindInit, TestCase): ) container.save() container = models.Container.objects.get(pk=container.pk) + container_2 = models.Container.objects.create( + reference="Test2", responsible=self.main_warehouse, + location=self.main_warehouse, + container_type=models.ContainerType.objects.all()[0], + parent=container + ) self.assertIn(self.main_warehouse.name, container.cached_location) models.ContainerLocalisation.objects.create( @@ -548,6 +554,8 @@ class ContainerTest(FindInit, TestCase): container.save() self.assertFalse(models.ContainerLocalisation.objects.filter( division__warehouse=self.main_warehouse).count()) + container_2 = models.Container.objects.get(pk=container_2.pk) + self.assertEqual(container_2.location, other_warehouse) def test_reassign_existing_division_on_warehouse_change(self): container = models.Container.objects.create( @@ -768,7 +776,7 @@ class ContainerTest(FindInit, TestCase): top_container_2 = models.Container.objects.create( reference="Topref 2", - location=self.main_warehouse, + location=self.alt_warehouse, container_type=ct) find1.container = top_container_2 find1.container_ref = top_container_2 @@ -785,13 +793,13 @@ class ContainerTest(FindInit, TestCase): middle_container_2 = models.Container.objects.create( reference="Middle ref", - location=self.main_warehouse, + location=self.alt_warehouse, parent=top_container_2, container_type=ct2) bottom_container_3 = models.Container.objects.create( reference="Bottom ref", - location=self.main_warehouse, + location=self.alt_warehouse, parent=middle_container_2, container_type=ct3) find3.container = bottom_container_3 @@ -810,16 +818,19 @@ class ContainerTest(FindInit, TestCase): self.assertEqual(q.count(), 1) top_ref = q.all()[0] self.assertEqual(top_ref.finds.count(), 2) + self.assertEqual(top_ref.location, self.main_warehouse) q = models.Container.objects.filter(reference="Middle ref") self.assertEqual(q.count(), 1) middle = q.all()[0] self.assertEqual(middle.parent, top_ref) + self.assertEqual(middle.location, self.main_warehouse) q = models.Container.objects.filter(reference="Bottom ref") self.assertEqual(q.count(), 1) bottom = q.all()[0] self.assertEqual(bottom.parent, middle) + self.assertEqual(bottom.location, self.main_warehouse) |