diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-04-29 18:43:36 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-04-30 11:17:10 +0200 |
commit | 68190548376cd951d9e0466f385afd7ee94a5256 (patch) | |
tree | 0ae0f380fad9cc24b3380e11fc1948acf0000e71 /archaeological_warehouse/models.py | |
parent | 8b2a8e8aff6f2143a9ae0eb266a6c34c19591b46 (diff) | |
download | Ishtar-68190548376cd951d9e0466f385afd7ee94a5256.tar.bz2 Ishtar-68190548376cd951d9e0466f385afd7ee94a5256.zip |
✨ templates: get containers values from the operation - performance: add cache (refs #5927)
Diffstat (limited to 'archaeological_warehouse/models.py')
-rw-r--r-- | archaeological_warehouse/models.py | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 5df824d8f..5e1477129 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -25,7 +25,7 @@ import uuid from django.contrib.gis.db import models from django.contrib.postgres.indexes import GinIndex from django.core.exceptions import ObjectDoesNotExist -from django.db.models import Q, Max, Count +from django.db.models import Q, Max from django.db.models.signals import post_save, post_delete, m2m_changed, pre_delete from django.template.defaultfilters import slugify from django.urls import reverse @@ -33,7 +33,7 @@ from ishtar_common.utils import ugettext_lazy as _, pgettext_lazy from django.apps import apps from ishtar_common.data_importer import post_importer_action, pre_importer_action, ImporterError -from ishtar_common.model_managers import ExternalIdManager, UUIDModelManager +from ishtar_common.model_managers import UUIDModelManager from ishtar_common.models import ValueGetter, get_current_profile, HistoryModel, Imported from ishtar_common.models_common import ( GeneralType, @@ -45,7 +45,6 @@ from ishtar_common.models_common import ( DashboardFormItem, document_attached_changed, SearchAltName, - DynamicRequest, GeoItem, CompleteIdentifierItem, SearchVectorConfig, @@ -1508,16 +1507,26 @@ class Container( def get_town_polygons(self): return self.location.get_town_polygons() + @property def contained_documents(self): - if not self.pk: - return + """ + Documents contained in this container. + A Document queryset is returned + """ Document = apps.get_model("ishtar_common", "Document") + if not self.pk: + return Document.objects.filter(pk__isnull=True) return Document.objects.filter(container_id=self.pk) + @property def contained_documents_ref(self): - if not self.pk: - return + """ + Documents whose reference container is this container. + A Document queryset is returned + """ Document = apps.get_model("ishtar_common", "Document") + if not self.pk: + return Document.objects.filter(pk__isnull=True) return Document.objects.filter(container_ref_id=self.pk) @property @@ -1811,6 +1820,8 @@ class Container( ("material_types", _("Material types inside the container - string")), ("material_types_code", _("Material types code - string")), ("finds", _("List of associated finds")), + ("contained_documents", _("List of documents contained in this container")), + ("contained_documents_ref", _("List of documents whose reference container is this container")), ] def get_material_types_code(self) -> str: @@ -1835,10 +1846,8 @@ class Container( 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 - ) + def get_extra_values(self, prefix="", no_values=False, filtr=None, **kwargs): + values = {} from_find = prefix.startswith("container_") or prefix.startswith( "container_ref_" ) @@ -1855,6 +1864,16 @@ class Container( f.get_values(prefix=prefix, no_values=True, filtr=filtr, **kwargs) for f in self.finds.distinct().order_by("index").all() ] + if not filtr or prefix + "contained_documents" in filtr: + values[prefix + "contained_documents"] = [ + doc.get_values(prefix=prefix, no_values=True, filtr=filtr, **kwargs) + for doc in self.contained_documents.distinct().order_by("index").all() + ] + if not filtr or prefix + "contained_documents_ref" in filtr: + values[prefix + "contained_documents_ref"] = [ + doc.get_values(prefix=prefix, no_values=True, filtr=filtr, **kwargs) + for doc in self.contained_documents_ref.distinct().order_by("index").all() + ] if not self.finds.count(): return values |