diff options
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 |
commit | a4f8ceb10cb00e6b2b0906c79eacb59162e4e7be (patch) | |
tree | f17455d8b5bae5c7d8acecb0738854eafbfe424c /archaeological_warehouse | |
parent | 5adeeeab51621d0a9def271c9945094be66e2d64 (diff) | |
download | Ishtar-a4f8ceb10cb00e6b2b0906c79eacb59162e4e7be.tar.bz2 Ishtar-a4f8ceb10cb00e6b2b0906c79eacb59162e4e7be.zip |
Container templates: add a "material_types" and a "material_types_code"
Diffstat (limited to 'archaeological_warehouse')
-rw-r--r-- | archaeological_warehouse/models.py | 73 | ||||
-rw-r--r-- | archaeological_warehouse/tests.py | 47 |
2 files changed, 106 insertions, 14 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): """ diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py index 25937ed6a..e6b3a985d 100644 --- a/archaeological_warehouse/tests.py +++ b/archaeological_warehouse/tests.py @@ -32,7 +32,7 @@ from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \ from ishtar_common.models import IshtarSiteProfile, SpatialReferenceSystem from archaeological_operations.models import Operation from archaeological_context_records.models import ContextRecord -from archaeological_finds.models import Find +from archaeological_finds.models import Find, MaterialType from archaeological_warehouse import models, views, forms, serializers @@ -1098,3 +1098,48 @@ class ContainerTest(FindInit, TestCase): container_type=ct) self.assertEqual(models.Container.objects.get(pk=container_5.pk).index, base_index + 3) + + def test_get_material_types(self): + mat0 = MaterialType.objects.all()[0] + mat1 = MaterialType.objects.all()[1] + mat2 = MaterialType.objects.all()[2] + ct = models.ContainerType.objects.all()[0] + self.create_finds() + self.create_finds() + self.create_finds() + find0 = self.finds[0] + find0.material_types.add(mat0) + find0.material_types.add(mat1) + find1 = self.finds[1] + find0.material_types.add(mat1) + find0.material_types.add(mat2) + find2 = self.finds[2] + + container_1 = models.Container.objects.create( + reference="Test 1", + location=self.main_warehouse, + container_type=ct) + container_2 = models.Container.objects.create( + reference="Test 2", + location=self.alt_warehouse, + container_type=ct) + find0.container = container_1 + find0.save() + find1.container = container_1 + find1.container_ref = container_2 + find1.save() + find2.container = container_2 + find1.save() + + # no material inside this container + self.assertEqual( + container_2.get_material_types(), "") + + self.assertEqual( + container_1.get_material_types(), + ", ".join(sorted([mat0.label, mat1.label, mat2.label])) + ) + self.assertEqual( + container_1.get_material_types_code(), + "|".join(sorted([mat0.code, mat1.code, mat2.code]))) + |