Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pridaná validácia na počet bodov #449

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion competition/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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 @@ -657,7 +658,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 @@ -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