diff --git a/competition/models.py b/competition/models.py index 99da75d..79afd7e 100644 --- a/competition/models.py +++ b/competition/models.py @@ -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 @@ -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) diff --git a/competition/utils/validations.py b/competition/utils/validations.py new file mode 100644 index 0000000..a68fccf --- /dev/null +++ b/competition/utils/validations.py @@ -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.' + ) diff --git a/competition/views.py b/competition/views.py index 4356205..16aae6c 100644 --- a/competition/views.py +++ b/competition/views.py @@ -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 @@ -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() @@ -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)