diff options
Diffstat (limited to 'archaeological_warehouse')
-rw-r--r-- | archaeological_warehouse/forms.py | 15 | ||||
-rw-r--r-- | archaeological_warehouse/models.py | 11 | ||||
-rw-r--r-- | archaeological_warehouse/tests.py | 104 | ||||
-rw-r--r-- | archaeological_warehouse/views.py | 2 |
4 files changed, 123 insertions, 9 deletions
diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py index 6ea1ae4f6..170edce1f 100644 --- a/archaeological_warehouse/forms.py +++ b/archaeological_warehouse/forms.py @@ -596,7 +596,7 @@ class QAContainerFormMulti(QAForm): self.items = kwargs["items"] super(QAContainerFormMulti, self).__init__(*args, **kwargs) locations = set([item.location_id for item in self.items]) - if len(locations) == 1: + if len(locations) == 1 and "qalocation" in self.fields: self.fields["qalocation"].initial = locations.pop() def _get_qalocation(self, value): @@ -641,7 +641,7 @@ class QAContainerFormMulti(QAForm): _("Cannot do this changes because it would generate " "many containers with location: {}, container type: " "{}, parent: {} {} and reference: {}. " - "Merge theses containers first?")).format( + "Merge these containers first?")).format( models.Warehouse.objects.get(pk=vals["location_id"]), models.ContainerType.objects.get( pk=vals["container_type_id"]), @@ -650,3 +650,14 @@ class QAContainerFormMulti(QAForm): ) ) return self.cleaned_data + + def save(self, items, user): + super(QAContainerFormMulti, self).save(items, user) + if self.cleaned_data.get("qaparent", None): + return + for item in items: + item = models.Container.objects.get(pk=item.pk) + # remove parent if do not share the same location + if item.parent and item.parent.location != item.location: + item.parent = None + item.save() diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 565b64af2..04936d7a5 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -821,12 +821,12 @@ class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem, return self.location._get_base_image_path() + "/" + self.external_id @classmethod - def _change_parent_location(cls, parent): + def _change_child_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) + cls._change_child_location(child) def merge(self, item, keep_old=False, exclude_fields=None): # merge child containers @@ -844,7 +844,7 @@ class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem, else: child.parent = self child.save() - self._change_parent_location(self) + self._change_child_location(self) super(Container, self).merge(item, keep_old=keep_old, exclude_fields=exclude_fields) @@ -980,7 +980,8 @@ class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem, return # modify existing - current_localisations = self.get_localisations() + ## first localisation is the warehouse + current_localisations = list(self.get_localisations())[1:] current_localisation, current_parent = None, None for loca in current_localisations: if loca.container_type == current_container_type: @@ -1143,7 +1144,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) + self._change_child_location(self) updated = False if not self.index: diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py index 81b8b3df5..126cc0420 100644 --- a/archaeological_warehouse/tests.py +++ b/archaeological_warehouse/tests.py @@ -19,10 +19,15 @@ import json +from django.contrib.auth.models import Permission +from django.core.urlresolvers import reverse +from django.test.client import Client + from archaeological_finds.tests import FindInit from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \ - TestCase, WAREHOUSE_FIXTURES, GenericSerializationTest, COMMON_FIXTURES + TestCase, WAREHOUSE_FIXTURES, GenericSerializationTest, COMMON_FIXTURES, \ + create_user from ishtar_common.models import IshtarSiteProfile, SpatialReferenceSystem from archaeological_operations.models import Operation @@ -832,5 +837,102 @@ class ContainerTest(FindInit, TestCase): self.assertEqual(bottom.parent, middle) self.assertEqual(bottom.location, self.main_warehouse) + def test_bulk_update(self): + username, password, user = create_user() + user.user_permissions.add(Permission.objects.get( + codename='change_container')) + client = Client() + + ct = models.ContainerType.objects.all()[0] + ct2 = models.ContainerType.objects.all()[1] + container_parent = models.Container.objects.create( + reference="Parent container", + location=self.main_warehouse, + container_type=ct) + container_parent2 = models.Container.objects.create( + reference="Parent container 2", + location=self.alt_warehouse, + container_type=ct) + + container = models.Container.objects.create( + reference="Test", + location=self.main_warehouse, + parent=container_parent, + container_type=ct2) + + # base modification + url = reverse('container-qa-bulk-update', args=[container.pk]) + data = { + "qacontainer_type": ct.pk, + "qalocation": '', + "qaparent": '' + } + response = client.post(url, data) + self.assertRedirects(response, '/') + client.login(username=username, password=password) + response = client.post(url, data) + self.assertEqual(response.status_code, 200) + confirm_url = reverse( + 'container-qa-bulk-update-confirm', args=[container.pk]) + response = client.post(confirm_url, data) + self.assertRedirects(response, '/success/') + + container = models.Container.objects.get(pk=container.pk) + self.assertEqual(container.container_type_id, ct.pk) + + container.container_type = ct2 + container.save() + + data = { + "qacontainer_type": '', + "qalocation": self.alt_warehouse.pk, + "qaparent": container_parent2.pk + } + response = client.post(confirm_url, data) + self.assertRedirects(response, '/success/') + + container = models.Container.objects.get(pk=container.pk) + self.assertEqual(container.location, self.alt_warehouse) + self.assertEqual(container.parent, container_parent2) + + container.location = self.main_warehouse + container.parent = container_parent + container.save() + + # change location of children + + data = { + "qacontainer_type": '', + "qalocation": self.alt_warehouse.pk, + "qaparent": '' + } + confirm_parent_url = reverse( + 'container-qa-bulk-update-confirm', args=[container_parent.pk]) + response = client.post(confirm_parent_url, data) + self.assertRedirects(response, '/success/') + + container_parent = models.Container.objects.get(pk=container_parent.pk) + self.assertEqual(container_parent.location, self.alt_warehouse) + container = models.Container.objects.get(pk=container.pk) + self.assertEqual(container.location, self.alt_warehouse) + + container_parent.location = self.main_warehouse + container_parent.save() + container.location = self.main_warehouse + container.parent = container_parent + container.save() + + # reinit parent when not provided and location changed + + data = { + "qacontainer_type": '', + "qalocation": self.alt_warehouse.pk, + "qaparent": '' + } + response = client.post(confirm_url, data) + self.assertRedirects(response, '/success/') + container = models.Container.objects.get(pk=container.pk) + self.assertEqual(container.location, self.alt_warehouse) + self.assertEqual(container.parent, None) diff --git a/archaeological_warehouse/views.py b/archaeological_warehouse/views.py index 6675addca..3a11c0e05 100644 --- a/archaeological_warehouse/views.py +++ b/archaeological_warehouse/views.py @@ -302,6 +302,6 @@ class QAContainerForm(QAItemEditForm): def get_form_kwargs(self): kwargs = super(QAContainerForm, self).get_form_kwargs() - # item list is necessary to verify unicity rules + # item list is necessary to verify uniqueness rules kwargs['items'] = self.items return kwargs |