Skip to content
Snippets Groups Projects
Commit 05bd4c11 authored by Valentin Rigal's avatar Valentin Rigal
Browse files

Update dataset serializer validation

parent 711e8030
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !2276. Comments created here will be created in the context of that merge request.
...@@ -4,7 +4,7 @@ from collections import defaultdict ...@@ -4,7 +4,7 @@ from collections import defaultdict
from textwrap import dedent from textwrap import dedent
from django.db import transaction from django.db import transaction
from django.db.models import Count, Exists, OuterRef, Q from django.db.models import Count, Q
from drf_spectacular.utils import extend_schema_field from drf_spectacular.utils import extend_schema_field
from rest_framework import permissions, serializers from rest_framework import permissions, serializers
from rest_framework.exceptions import PermissionDenied, ValidationError from rest_framework.exceptions import PermissionDenied, ValidationError
...@@ -576,12 +576,13 @@ class DatasetSerializer(serializers.ModelSerializer): ...@@ -576,12 +576,13 @@ class DatasetSerializer(serializers.ModelSerializer):
return set_names return set_names
def validate_unique_elements(self, unique): def validate_unique_elements(self, unique):
if unique is True and self.instance and Exists( if unique is True and self.instance and (
DatasetElement.objects DatasetElement.objects
.filter(set__dataset_id=OuterRef(self.instance.pk)) .filter(set__dataset_id=self.instance.pk)
.values("element") .values("element")
.annotate(dups=Count("element")) .annotate(dups=Count("element"))
.filter(dups__gte=2) .filter(dups__gte=2)
.exists()
): ):
raise ValidationError("Elements are currently contained by multiple sets.") raise ValidationError("Elements are currently contained by multiple sets.")
return unique return unique
...@@ -712,20 +713,24 @@ class DatasetElementSerializer(serializers.ModelSerializer): ...@@ -712,20 +713,24 @@ class DatasetElementSerializer(serializers.ModelSerializer):
self.fields["element_id"].queryset = Element.objects.filter(corpus=dataset.corpus) self.fields["element_id"].queryset = Element.objects.filter(corpus=dataset.corpus)
self.fields["set"].queryset = dataset.sets.all() self.fields["set"].queryset = dataset.sets.all()
def validate(self, data): def validate_element_id(self, element):
data = super().validate(data) dataset = self.context.get("dataset")
dataset = data.pop("dataset") if dataset and dataset.unique_elements and (
if dataset.unique_elements and ( existing_set := (
set := (
dataset.sets dataset.sets
.filter(set_elements__element_id=data["element_id"]) .filter(set_elements__element=element)
.values_list("name", flat=True) .values_list("name", flat=True)
.first() .first()
) )
): ):
raise ValidationError({"element_id": [ raise ValidationError([
f"The dataset prevent duplication and this element is already present in set {set}." f"The dataset prevent duplication and this element is already present in set {existing_set}."
]}) ])
return element
def validate(self, data):
data = super().validate(data)
data.pop("dataset")
return data return data
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment