From 838d508d64bb33f1a9dca2ee05a0d731438f78c6 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 11 Feb 2021 20:23:41 +0100 Subject: Add post_import action for container: put_document_by_complete_identifier --- archaeological_warehouse/models.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'archaeological_warehouse') diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index db08c3467..61b2e3f6d 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -45,6 +45,7 @@ from ishtar_common.models_common import GeneralType, \ from ishtar_common.model_merging import merge_model_objects from ishtar_common.utils import cached_label_changed, \ cached_label_and_geo_changed, get_generated_id +from ishtar_common.data_importer import ImporterError class DivisionContainer(DashboardFormItem): @@ -990,7 +991,25 @@ class Container(DocumentItem, Merge, LightHistorizedItem, @property def get_merge_key(self): - return str(self.location.uuid) + "|" + self._generate_cached_division() + try: + return str(self.location.uuid) + "|" + \ + self._generate_cached_division() + except Warehouse.DoesNotExist: + return + + @post_importer_action + def put_document_by_complete_identifier(self, context, value): + Document = apps.get_model("ishtar_common", "Document") + try: + doc = Document.objects.get(complete_identifier=value) + except Document.DoesNotExist: + raise ImporterError(str(_("Document with complete id: {} does not " + "exists")).format(value)) + doc.container_id = self.pk + doc.container_ref_id = self.pk + doc.skip_history_when_saving = True + doc.save() + put_document_by_complete_identifier.post_save = True def _generate_cached_division(self): parents = [] @@ -1371,8 +1390,11 @@ class Container(DocumentItem, Merge, LightHistorizedItem, if not self.responsibility_id and not self.responsibility: if self.location_id: self.responsibility_id = self.location_id - if self.location: - self.responsibility = self.location + else: + try: + self.responsibility = self.location + except Warehouse.DoesNotExist: + return if self.container_type.stationary: return q = Container.objects.filter(index=self.index, location=self.location) -- cgit v1.2.3