From 9a3451605952424736e139284802c82fcc7768b5 Mon Sep 17 00:00:00 2001 From: EwoutV Date: Tue, 21 May 2024 21:33:57 +0200 Subject: [PATCH] fix: docker image validation --- backend/api/serializers/checks_serializer.py | 23 ++++++++++++++++---- backend/api/views/project_view.py | 13 +---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/backend/api/serializers/checks_serializer.py b/backend/api/serializers/checks_serializer.py index 1ff44824..4713c45d 100644 --- a/backend/api/serializers/checks_serializer.py +++ b/backend/api/serializers/checks_serializer.py @@ -3,7 +3,7 @@ from api.models.project import Project from django.utils.translation import gettext as _ from rest_framework import serializers -from rest_framework.exceptions import ValidationError +from rest_framework.exceptions import ValidationError, NotFound class FileExtensionSerializer(serializers.ModelSerializer): @@ -83,8 +83,7 @@ class Meta: class ExtraCheckSerializer(serializers.ModelSerializer): project = serializers.HyperlinkedIdentityField( - view_name="project-detail", - read_only=True + view_name="project-detail" ) docker_image = serializers.HyperlinkedIdentityField( @@ -95,9 +94,25 @@ class Meta: model = ExtraCheck fields = "__all__" - def validate(self, attrs): + def validate(self, attrs: dict) -> dict: + """Validate the extra check""" data = super().validate(attrs) + # Check if the docker image is provided + if not "docker_image" in self.initial_data: + raise serializers.ValidationError(_("extra_check.error.docker_image")) + + # Check if the docker image exists + image = DockerImage.objects.get( + id=self.initial_data["docker_image"] + ) + + if image is None: + raise NotFound(_("extra_check.error.docker_image")) + + data["docker_image"] = image + + # Check if the time limit and memory limit are in the correct range if "time_limit" in data and not 10 <= data["time_limit"] <= 1000: raise serializers.ValidationError(_("extra_check.error.time_limit")) diff --git a/backend/api/views/project_view.py b/backend/api/views/project_view.py index 4d01e5e4..dc2243d9 100644 --- a/backend/api/views/project_view.py +++ b/backend/api/views/project_view.py @@ -1,8 +1,3 @@ -import logging - -from rest_framework.exceptions import ValidationError -from rest_framework.parsers import MultiPartParser - from api.models.group import Group from api.models.project import Project from api.models.submission import Submission @@ -25,10 +20,6 @@ from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet - -logger = logging.getLogger("ypovoli") - - # TODO: Error message when creating a project with wrongly formatted date looks a bit weird class ProjectViewSet(RetrieveModelMixin, UpdateModelMixin, @@ -168,8 +159,6 @@ def _add_extra_check(self, request: Request, **_): project: Project = self.get_object() - logger.info(request.POST.dict()) - serializer = ExtraCheckSerializer( data=request.data, context={ @@ -179,7 +168,7 @@ def _add_extra_check(self, request: Request, **_): ) if serializer.is_valid(raise_exception=True): - serializer.save(project=project, docker_image_id=request.data.get('docker_image')) + serializer.save(project=project) return Response({ "message": gettext("project.success.extra_check.add")