From fb488e18d189e2bd202f01249d80b327a205c107 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 24 Feb 2021 13:17:23 +0100 Subject: Container templates: add a "material_types" and a "material_types_code" --- archaeological_warehouse/models.py | 73 +++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 13 deletions(-) (limited to 'archaeological_warehouse/models.py') 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): """ -- cgit v1.2.3