Skip to content

Commit

Permalink
Pridaná validácia na počet bodov (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacspe authored Nov 24, 2024
1 parent 940118b commit b7a2cb6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion competition/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from competition.querysets import ActiveQuerySet
from competition.utils.school_year_manipulation import \
get_school_year_end_by_date
from competition.utils.validations import validate_points
from personal.models import Profile, School
from user.models import User

Expand Down Expand Up @@ -643,7 +644,7 @@ class Meta:
verbose_name='opravené riešenie', blank=True, upload_to=get_corrected_solution_path)

score = models.PositiveSmallIntegerField(
verbose_name='body', null=True, blank=True)
verbose_name='body', null=True, blank=True, validators=[validate_points])

vote = models.IntegerField(choices=Vote.choices,
default=Vote.NONE)
Expand Down
8 changes: 8 additions & 0 deletions competition/utils/validations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.core.exceptions import ValidationError


def validate_points(value: int):
if value < 0 or value > 9:
raise ValidationError(
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 @@ -388,7 +389,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 @@ -407,7 +412,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 b7a2cb6

Please sign in to comment.