diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-02-24 19:14:37 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-02-28 12:15:25 +0100 |
commit | 16a47f887447ecccd56fc848aee95fa56af92548 (patch) | |
tree | f96e518b2766d2a300829d0e5f9fac491d6d065d /archaeological_operations | |
parent | 0eab67538b8f2af9283bf1e77438763914e355b7 (diff) | |
download | Ishtar-16a47f887447ecccd56fc848aee95fa56af92548.tar.bz2 Ishtar-16a47f887447ecccd56fc848aee95fa56af92548.zip |
get_values: get containers from operation - material_types_code from finds
Diffstat (limited to 'archaeological_operations')
-rw-r--r-- | archaeological_operations/models.py | 26 | ||||
-rw-r--r-- | archaeological_operations/tests.py | 28 |
2 files changed, 51 insertions, 3 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index b7841450c..240283d1a 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -22,6 +22,7 @@ import datetime from itertools import groupby import uuid +from django.apps import apps from django.conf import settings from django.contrib.gis.db import models from django.contrib.gis.db.models.aggregates import Union @@ -1234,6 +1235,22 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, def __str__(self): return self.cached_label or "" + DOC_VALUES = [ + ("context_records.", _("List of associated context records")), + ("containers.", _("List of associated containers")), + ] + + def get_containers_values(self, filtr, exclude) -> list: # Container value + # list + Container = apps.get_model("archaeological_warehouse", "Container") + containers = [] + q = Container.objects.filter( + finds__base_finds__context_record__operation=self).distinct("index") + exclude += ["operation", "context_record"] + for c in q.order_by("index").all(): + containers.append(c.get_values(filtr=filtr, exclude=exclude)) + return containers + def get_values(self, prefix='', no_values=False, filtr=None, **kwargs): values = super(Operation, self).get_values( prefix=prefix, no_values=no_values, filtr=filtr, **kwargs) @@ -1241,14 +1258,17 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, exclude = kwargs.get("exclude", []) if prefix: return values - if (not filtr or prefix + 'context_records' in filtr) and \ - prefix + "context_records" not in exclude: + if (not filtr or 'context_records' in filtr) and \ + "context_records" not in exclude: kwargs["no_base_finds"] = False - values[prefix + 'context_records'] = [ + values['context_records'] = [ cr.get_values(prefix=prefix, no_values=True, filtr=None, **kwargs) for cr in self.context_record.all() ] + if (not filtr or "containers" in filtr) \ + and "context_records" not in exclude: + values["containers"] = self.get_containers_values(filtr, exclude) return values def public_representation(self): diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index d8c61c642..650a839aa 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -25,6 +25,7 @@ import tempfile import locale import zipfile +from django.apps import apps from django.conf import settings from django.contrib.auth.models import Group from django.contrib.contenttypes.models import ContentType @@ -1732,6 +1733,33 @@ class OperationTest(TestCase, OperationInitTest): doc2 = Document.objects.get(pk=doc2.pk) self.assertEqual(c_index + 2, doc2.index) + def test_get_containers(self): + Find = apps.get_model("archaeological_finds", "Find") + BaseFind = apps.get_model("archaeological_finds", "BaseFind") + Warehouse = apps.get_model("archaeological_warehouse", "Warehouse") + WarehouseType = apps.get_model("archaeological_warehouse", + "WarehouseType") + Container = apps.get_model("archaeological_warehouse", "Container") + ContainerType = apps.get_model("archaeological_warehouse", + "ContainerType") + operation = self.operations[0] + hc, __ = Unit.objects.get_or_create(txt_idx='not-in-context', order=10) + cr = ContextRecord.objects.create( + operation=operation, unit=hc) + bf = BaseFind.objects.create(context_record=cr) + f = Find.objects.create() + f.base_finds.add(bf) + wt = WarehouseType.objects.create(label='WT') + w = Warehouse.objects.create(name="Warehouse", warehouse_type=wt) + ct = ContainerType.objects.create(label='CT') + c = Container.objects.create(reference="Test", location=w, + container_type=ct) + f.container = c + f.save() + + values = operation.get_containers_values([], []) + self.assertEqual(len(values), 1) + class LockTest(TestCase, OperationInitTest): fixtures = FILE_FIXTURES |