summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2021-02-23 14:20:43 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2021-02-28 12:15:24 +0100
commite41657c0fd0f868488b5a7ab9d9aa8f4229e43cd (patch)
treee0e128bf80472f2947a5f69874b62c705f01b893 /archaeological_warehouse/models.py
parent8b3eae6c7d60a54bc61e29a6ea7539b8f809d26f (diff)
downloadIshtar-e41657c0fd0f868488b5a7ab9d9aa8f4229e43cd.tar.bz2
Ishtar-e41657c0fd0f868488b5a7ab9d9aa8f4229e43cd.zip
Warehouse: localisations importer
Diffstat (limited to 'archaeological_warehouse/models.py')
-rw-r--r--archaeological_warehouse/models.py90
1 files changed, 74 insertions, 16 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py
index e094436e1..71751fcbe 100644
--- a/archaeological_warehouse/models.py
+++ b/archaeological_warehouse/models.py
@@ -256,6 +256,10 @@ post_save.connect(post_save_cache, sender=WarehouseType)
post_delete.connect(post_save_cache, sender=WarehouseType)
+NO_DIVISION_ERROR = _(
+ "The division number {} has not been set for the warehouse {}.")
+
+
class Warehouse(Address, DocumentItem, GeoItem, CompleteIdentifierItem,
OwnPerms, MainItem, DivisionContainer, ValueGetter):
SLUG = 'warehouse'
@@ -361,6 +365,72 @@ class Warehouse(Address, DocumentItem, GeoItem, CompleteIdentifierItem,
def __str__(self):
return self.name
+ def get_container_type_by_place(self, place: int):
+ """
+ Container type by place based on the default organisation of the
+ warehouse
+
+ :param place: place number
+ :return: container type, other location or None, None
+ """
+ q = WarehouseDivisionLink.objects.filter(
+ warehouse=self).order_by('order')
+ previous_container_types = []
+ for idx, division_link in enumerate(q.all()):
+ if idx == place:
+ current_container_type = division_link.container_type
+ break
+ previous_container_types.append(division_link.container_type_id)
+ else:
+ return None, None
+ return current_container_type, previous_container_types
+
+ @post_importer_action
+ def add_localisations(self, __, value):
+ self._add_localisations(value)
+ add_localisations.post_save = True
+
+ def _add_localisations(self, value, return_errors=False):
+ """
+ Add localisations for this warehouse.
+ Get the default localisation types and set each reference from the
+ value separated by ";"
+
+ :param value: references of localisations separated by ;
+ :param return_errors: return error message default is False
+ :return: return an error message if return_errors set to True
+ """
+ value = value.strip()
+ if not value:
+ if return_errors:
+ return None, _("No value")
+ return
+
+ TMP_SEMI_COLON = "|#|#|"
+ value = value.replace("\\;", TMP_SEMI_COLON) # manage ";" used by a ref
+
+ values = value.split(";")
+
+ divisions = list(WarehouseDivisionLink.objects.filter(
+ warehouse=self).order_by('order'))
+ if len(values) > len(divisions):
+ if return_errors:
+ return str(_("{} values for only {} default divisions set for "
+ "warehouse {}")).format(
+ len(values), len(divisions), self.name)
+ return
+
+ parent = None
+ for idx, value in enumerate(values):
+ value = value.replace(TMP_SEMI_COLON, ";").strip()
+ if not value or value == "-":
+ continue
+ parent, __ = Container.objects.get_or_create(
+ location=self,
+ reference=value,
+ container_type_id=divisions[idx].container_type_id,
+ parent=parent)
+
@property
def short_label(self):
return self.name
@@ -1203,23 +1273,11 @@ class Container(DocumentItem, Merge, LightHistorizedItem,
if return_errors:
return None, _("No value")
return
- q = WarehouseDivisionLink.objects.filter(
- warehouse=self.location).order_by('order')
- current_container_type = None
- error_msg = str(
- _("The division number {} has not been set for the warehouse {}.")
- ).format(place + 1, self.location)
- previous_container_types = []
- for idx, division_link in enumerate(q.all()):
- if idx == place:
- current_container_type = division_link.container_type
- break
- previous_container_types.append(division_link.container_type_id)
- else:
- if return_errors:
- return None, error_msg
- return
+ error_msg = str(NO_DIVISION_ERROR).format(place + 1, self.location)
+
+ current_container_type, previous_container_types = \
+ self.location.get_container_type_by_place(place)
if not current_container_type:
# no division link set at this place
if return_errors: