Skip to content
Snippets Groups Projects

New DatasetSet model

Merged ml bonhomme requested to merge dataset-sets-reset into master
3 files
+ 7
4
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -4,7 +4,6 @@ from urllib.parse import quote, unquote
import bleach
from django.contrib.gis.geos import Point
from django.db.models import Count
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers
@@ -12,6 +11,7 @@ from arkindex.documents.models import MetaType
from arkindex.ponos.utils import get_process_from_task_auth
from arkindex.process.models import ProcessMode, WorkerRun
from arkindex.project.gis import ensure_linear_ring
from arkindex.project.tools import is_prefetched
class EnumField(serializers.ChoiceField):
@@ -269,7 +269,9 @@ class ArchivedField(serializers.BooleanField):
class DatasetSetsCountField(serializers.DictField):
"""
Serialize the number of element per set on a dataset.
This value can be disabled by setting `sets_count` to False in the context.
This field is None, unless the sets have been prefetched
with a `element_count` annotation holding the number of elements per set.
"""
def __init__(self, **kwargs):
@@ -281,16 +283,17 @@ class DatasetSetsCountField(serializers.DictField):
)
def get_attribute(self, instance):
if not self.context.get("sets_count", True):
# Skip this field if sets are not prefetched, or if they are missing a count
if (
not is_prefetched(instance.sets)
or not all(hasattr(set, "element_count") for set in instance.sets.all())
):
return None
elts_count = {k: 0 for k in instance.sets}
elts_count.update(
instance.dataset_elements
.values("set")
.annotate(count=Count("id"))
.values_list("set", "count")
)
return elts_count
return {
set.name: set.element_count
for set in instance.sets.all()
}
class NullField(serializers.CharField):
Loading