summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2021-02-24 13:17:23 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2021-02-28 12:15:25 +0100
commitfb488e18d189e2bd202f01249d80b327a205c107 (patch)
treef17455d8b5bae5c7d8acecb0738854eafbfe424c /archaeological_warehouse/models.py
parenta63cb992d806dd49face87de78ab8269337a1a6a (diff)
downloadIshtar-fb488e18d189e2bd202f01249d80b327a205c107.tar.bz2
Ishtar-fb488e18d189e2bd202f01249d80b327a205c107.zip
Container templates: add a "material_types" and a "material_types_code"
Diffstat (limited to 'archaeological_warehouse/models.py')
-rw-r--r--archaeological_warehouse/models.py73
1 files changed, 60 insertions, 13 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py
index 3b319881d..c78a25fbc 100644
--- a/archaeological_warehouse/models.py
+++ b/archaeological_warehouse/models.py
@@ -438,7 +438,6 @@ class Warehouse(Address, DocumentItem, GeoItem, CompleteIdentifierItem,
if created and import_object:
parent.imports.add(import_object)
-
@property
def short_label(self):
return self.name
@@ -1423,6 +1422,42 @@ class Container(DocumentItem, Merge, LightHistorizedItem,
return self.set_static_localisation(8, value)
set_static_localisation_9.post_save = True
+ DOC_VALUES = [
+ ("operation_", _("Associated operation - use it with caution, "
+ "only return the first found operation")),
+ ("context_record_",
+ _("Associated context record - use it with caution, "
+ "only return the first found context_record")),
+ ("material_types",
+ _("Material types inside the container - sorted by alphabetical "
+ "order")),
+ ("material_types_code",
+ _("Material types code inside the container - sorted by "
+ "alphabetical order")),
+ ]
+
+ def get_material_types_code(self) -> str:
+ """
+ Return dash separated material type code inside a container
+ """
+ materials = set()
+ for material in self.finds.exclude(
+ material_types__code__isnull=True).values_list(
+ "material_types__code", flat=True):
+ materials.add(material)
+ return "|".join(sorted(materials))
+
+ def get_material_types(self) -> str:
+ """
+ Return comma separated string of material types inside a container
+ """
+ materials = set()
+ for material in self.finds.exclude(
+ material_types__label__isnull=True).values_list(
+ "material_types__label", flat=True):
+ materials.add(material)
+ return ", ".join(sorted(materials))
+
def get_values(self, prefix='', no_values=False, filtr=None, **kwargs):
values = super(Container, self).get_values(
prefix=prefix, no_values=no_values, filtr=filtr, **kwargs)
@@ -1435,26 +1470,38 @@ class Container(DocumentItem, Merge, LightHistorizedItem,
f.get_values(
prefix=prefix, no_values=True, filtr=None, **kwargs)
for f in self.finds.distinct().all()]
+
+ operation_in_filter = any(
+ k for k in filtr if k.startswith(prefix + 'operation'))
+ cr_in_filter = any(
+ k for k in filtr if k.startswith(prefix + 'context_record'))
+ if not self.finds.count():
+ return
+ if not filtr or prefix + 'material_types' in filtr:
+ values[prefix + 'material_types'] = self.get_material_types()
+ if not filtr or prefix + 'material_types_code' in filtr:
+ values[prefix + 'material_types_code'] = \
+ self.get_material_types_code()
if not from_find and (
- not filtr or prefix + 'operation' in filtr or
- prefix + "context_record" in filtr) and self.finds.count():
- # assume that only one operation is in this container
+ not filtr or operation_in_filter or cr_in_filter):
+ # assume that only one operation is in this container...
# you should know what you are doing when using theses variables
f = self.finds.all()[0]
bf = f.get_first_base_find()
if bf:
cr = bf.context_record
- if not filtr or prefix + "context_record" in filtr:
+ if not filtr or cr_in_filter:
kwargs["exclude"] = [prefix + "operation"]
- values[prefix + 'context_record'] = \
- cr.get_values(
- prefix=prefix, no_values=True, filtr=None, **kwargs)
- if not filtr or prefix + "operation" in filtr:
+ for k, v in cr.get_values(
+ prefix=prefix, no_values=True, filtr=None,
+ **kwargs):
+ values[prefix + 'context_record_' + k] = v
+ if not filtr or operation_in_filter:
kwargs["exclude"] = [prefix + "context_records"]
- values[prefix + 'operation'] = \
- cr.operation.get_values(
- prefix=prefix, no_values=True, filtr=None, **kwargs)
- return values
+ for k, v in cr.operation.get_values(
+ prefix=prefix, no_values=True, filtr=None,
+ **kwargs):
+ values[prefix + 'operation_' + k] = v
def get_extra_actions(self, request):
"""