Skip to content

Commit

Permalink
fix: docker image validation
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutV committed May 21, 2024
1 parent 1fb9ec5 commit 9a34516
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
23 changes: 19 additions & 4 deletions backend/api/serializers/checks_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand All @@ -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"))

Expand Down
13 changes: 1 addition & 12 deletions backend/api/views/project_view.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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={
Expand All @@ -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")
Expand Down

0 comments on commit 9a34516

Please sign in to comment.