Skip to content

Commit

Permalink
Pridane validacie na REST actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacspe committed Nov 23, 2024
1 parent e07e7d8 commit 970831a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion competition/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import json
from typing import Optional
from typing import Iterable, Optional

from django.conf import settings
from django.contrib.sites.models import Site
Expand Down
2 changes: 1 addition & 1 deletion competition/utils/validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
def validate_points(value: int):
if value < 0 or value > 9:
raise ValidationError(
f'{value} je mimo povoleného rozmedzia pre body'
f'{value} je mimo povoleného rozmedzia pre body. Povolený rozsah je 0-9.'
)
13 changes: 11 additions & 2 deletions competition/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from operator import itemgetter
from typing import Optional

from django.core.exceptions import ValidationError as CoreValidationError
from django.core.files import File
from django.core.mail import send_mail
from django.http import FileResponse, HttpResponse
Expand Down Expand Up @@ -441,7 +442,11 @@ def upload_solutions_with_points(self, request, pk=None):
solution.score = score
solution.corrected_solution = File(corrected_solution)

solution.save()
try:
solution.full_clean()
solution.save()
except CoreValidationError:
return Response(status=status.HTTP_422_UNPROCESSABLE_ENTITY)

return Response()

Expand All @@ -460,7 +465,11 @@ def upload_points(self, request, pk=None):
if solution.problem != problem:
continue
solution.score = solution_dict['score']
solution.save()
try:
solution.full_clean()
solution.save()
except CoreValidationError:
return Response(status=status.HTTP_422_UNPROCESSABLE_ENTITY)
return Response(status=status.HTTP_200_OK)


Expand Down

0 comments on commit 970831a

Please sign in to comment.