diff options
Diffstat (limited to 'archaeological_warehouse')
-rw-r--r-- | archaeological_warehouse/migrations/0037_auto_20190628_1257.py | 36 | ||||
-rw-r--r-- | archaeological_warehouse/models.py | 32 | ||||
-rw-r--r-- | archaeological_warehouse/tests.py | 85 |
3 files changed, 122 insertions, 31 deletions
diff --git a/archaeological_warehouse/migrations/0037_auto_20190628_1257.py b/archaeological_warehouse/migrations/0037_auto_20190628_1257.py new file mode 100644 index 000000000..82e489a8d --- /dev/null +++ b/archaeological_warehouse/migrations/0037_auto_20190628_1257.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.18 on 2019-06-28 12:57 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_warehouse', '0036_auto_20190627_1321'), + ] + + operations = [ + migrations.AddField( + model_name='collection', + name='need_update', + field=models.BooleanField(default=False, verbose_name='Need update'), + ), + migrations.AddField( + model_name='container', + name='need_update', + field=models.BooleanField(default=False, verbose_name='Need update'), + ), + migrations.AddField( + model_name='warehouse', + name='need_update', + field=models.BooleanField(default=False, verbose_name='Need update'), + ), + migrations.AlterField( + model_name='warehouse', + name='organization', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='warehouses', to='ishtar_common.Organization', verbose_name='Organisation'), + ), + ] diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index a93a0917d..3dba70355 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -35,7 +35,8 @@ from ishtar_common.models import Document, GeneralType, get_external_id, \ document_attached_changed, SearchAltName, DynamicRequest, GeoItem, \ QRCodeItem, SearchVectorConfig, DocumentItem from ishtar_common.model_merging import merge_model_objects -from ishtar_common.utils import cached_label_changed, post_save_geo, task +from ishtar_common.utils import cached_label_changed, \ + cached_label_and_geo_changed class WarehouseType(GeneralType): @@ -80,6 +81,8 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem, ), } GEO_LABEL = "name" + DOWN_MODEL_UPDATE = ["containers"] + CACHED_LABELS = [] objects = ExternalIdManager() @@ -327,30 +330,12 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem, self._cached_label_checked = False self.save() return - if not settings.USE_BACKGROUND_TASK: - update_containers(self) - else: - update_containers.delay(self.pk) - - -@task() -def update_containers(warehouse): - if not settings.USE_BACKGROUND_TASK: - for container in warehouse.containers.all(): - cached_label_changed(Container, instance=container) - return - try: - warehouse = Warehouse.objects.get(pk=warehouse) - except Warehouse.DoesNotExist: - return - for container in warehouse.containers.all(): - cached_label_changed(Container, instance=container) m2m_changed.connect(document_attached_changed, sender=Warehouse.documents.through) -post_save.connect(post_save_geo, sender=Warehouse) +post_save.connect(cached_label_and_geo_changed, sender=Warehouse) class Collection(LightHistorizedItem): @@ -875,12 +860,7 @@ class Container(DocumentItem, LightHistorizedItem, QRCodeItem, GeoItem, loca.delete() -def container_post_save(sender, **kwargs): - cached_label_changed(sender=sender, **kwargs) - post_save_geo(sender=sender, **kwargs) - - -post_save.connect(container_post_save, sender=Container) +post_save.connect(cached_label_and_geo_changed, sender=Container) m2m_changed.connect(document_attached_changed, sender=Container.documents.through) diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py index 73cc166ac..3f4df9fad 100644 --- a/archaeological_warehouse/tests.py +++ b/archaeological_warehouse/tests.py @@ -17,14 +17,13 @@ # See the file COPYING for details. -from django.conf import settings - from archaeological_finds.tests import FindInit from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \ TestCase from archaeological_finds.tests import WAREHOUSE_FIXTURES +from ishtar_common.models import IshtarSiteProfile, SpatialReferenceSystem from archaeological_warehouse import models, views, forms @@ -189,7 +188,7 @@ class WarehouseTest(TestCase): class ContainerTest(FindInit, TestCase): fixtures = WAREHOUSE_FIXTURES - def testFormCreation(self): + def test_form_creation(self): main_warehouse = models.Warehouse.objects.create( name="Main", warehouse_type=models.WarehouseType.objects.all()[0] @@ -208,7 +207,7 @@ class ContainerTest(FindInit, TestCase): self.assertEqual(models.Container.objects.count(), self.container_number + 1) - def testChangeLocation(self): + def test_change_location(self): main_warehouse = models.Warehouse.objects.create( name="Main", warehouse_type=models.WarehouseType.objects.all()[0] @@ -222,13 +221,17 @@ class ContainerTest(FindInit, TestCase): location=main_warehouse, container_type=models.ContainerType.objects.all()[0] ) + container.save() + container = models.Container.objects.get(pk=container.pk) + self.assertIn(main_warehouse.name, container.cached_location) + models.ContainerLocalisation.objects.create( container=container, division=div_link, ) self.assertTrue(models.ContainerLocalisation.objects.filter( division__warehouse=main_warehouse).count()) - # changing location remove unrelevent localisation + # changing location remove irrelevant localisation other_warehouse = models.Warehouse.objects.create( name="Other", warehouse_type=models.WarehouseType.objects.all()[0] @@ -238,3 +241,75 @@ class ContainerTest(FindInit, TestCase): self.assertFalse(models.ContainerLocalisation.objects.filter( division__warehouse=main_warehouse).count()) + def test_update_containers_on_warehouse_update(self): + main_warehouse = models.Warehouse.objects.create( + name="Main", + warehouse_type=models.WarehouseType.objects.all()[0] + ) + container = models.Container.objects.create( + reference="Test", responsible=main_warehouse, + location=main_warehouse, + container_type=models.ContainerType.objects.all()[0] + ) + container.save() + container = models.Container.objects.get(pk=container.pk) + self.assertIn(main_warehouse.name, container.cached_location) + main_warehouse.name = "New name" + main_warehouse.save() + self.assertEqual( + models.Container.objects.filter( + need_update=True + ).count(), 1) + self.assertEqual( + models.Container.objects.filter( + pk=container.pk, + need_update=True + ).count(), 1) + container = models.Container.objects.get(pk=container.pk) + # process pending update + container.skip_history_when_saving = True + container._no_move = True + container.save() + container = models.Container.objects.get(pk=container.pk) + self.assertIn("New name", container.cached_location) + + def test_update_container_localisation_on_warehouse_update(self): + profile, created = IshtarSiteProfile.objects.get_or_create( + slug='default', active=True) + profile.mapping = True + profile.locate_warehouses = True + profile.save() + wgs84 = SpatialReferenceSystem.objects.get(srid=4326) + main_warehouse = models.Warehouse.objects.create( + name="Main", + warehouse_type=models.WarehouseType.objects.all()[0], + ) + container = models.Container.objects.create( + reference="Test", responsible=main_warehouse, + location=main_warehouse, + container_type=models.ContainerType.objects.all()[0] + ) + container.save() + self.assertEqual(container.x, None) + + main_warehouse = models.Warehouse.objects.get(pk=main_warehouse.pk) + main_warehouse.x, main_warehouse.y = 33, 42 + main_warehouse.spatial_reference_system = wgs84 + main_warehouse.save() + # an update is pending + self.assertEqual( + models.Container.objects.filter( + pk=container.pk, + need_update=True + ).count(), 1) + + # process pending update + container = models.Container.objects.get(pk=container.pk) + self.assertEqual(container.x, None) # update has to be done + container.skip_history_when_saving = True + container._no_move = True + container.save() + + container = models.Container.objects.get(pk=container.pk) + self.assertEqual(container.x, 33) + |