From aafd56fb6dd286e532fba5751001e55b4a736f42 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 14 Dec 2024 16:39:55 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Pridan=C3=A1=20valid=C3=A1cia=20za=C4=8Diat?= =?UTF-8?q?ku=20a=20konca=20semestra=20podla=20sk.=20roku?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- competition/serializers.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/competition/serializers.py b/competition/serializers.py index 3222368..4667a09 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -1,8 +1,12 @@ +import datetime + from django.contrib.auth.models import AnonymousUser +from django.core import exceptions from django_typomatic import ts_interface from rest_framework import serializers from rest_framework.exceptions import ValidationError +from base.validators import school_year_validator from competition import models from personal.serializers import ProfileShortSerializer, SchoolShortSerializer @@ -405,13 +409,35 @@ class Meta: read_only_fields = ['complete'] validators = [] - def validate(self, attrs): - if attrs.get('season_code', 0) not in (0, 1): + def validate_season_code(self, value: int): + if value not in (0, 1): raise ValidationError( 'Seminár musí byť zimný alebo letný(season_code 0 alebo 1)') - competition: models.Competition | None = attrs.get('competition') - if competition and competition.competition_type.name != "Seminár": + return value + + def validate_competition(self, value: models.Competition): + if value and value.competition_type.name != "Seminár": raise ValidationError('Súťaž nie je typu seminár') + return value + + def validate_school_year(self, value: str): + try: + school_year_validator(value) + return value + except exceptions.ValidationError as exc: + raise ValidationError('Nesprávny formát šk. roku') from exc + + def validate(self, attrs): + school_year = attrs.get('school_year') + start_year, end_year = school_year.split('/') + start = attrs.get('start') + end = attrs.get('end') + if start and start.date() < datetime.date(year=int(start_year), month=7, day=1): + raise ValidationError( + f'Začiatok súťaže ({start}) nie je v školskom roku {school_year}') + if end and end.date() > datetime.date(year=int(end_year), month=8, day=31): + raise ValidationError( + f'Koniec súťaže ({end}) nie je v školskom roku {school_year}') return super().validate(attrs) def get_complete(self, obj: models.Semester): From 67bc3ca655ee03734e8453602c2b9275b12c707f Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 14 Dec 2024 16:44:08 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Pridana=20valid=C3=A1cia=20datumov=20na=20e?= =?UTF-8?q?vent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- competition/serializers.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/competition/serializers.py b/competition/serializers.py index 4667a09..5864adb 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -71,6 +71,26 @@ class Meta: model = models.Event fields = '__all__' + def validate_school_year(self, value: str): + try: + school_year_validator(value) + return value + except exceptions.ValidationError as exc: + raise ValidationError('Nesprávny formát šk. roku') from exc + + def validate(self, attrs): + school_year = attrs.get('school_year') + start_year, end_year = school_year.split('/') + start = attrs.get('start') + end = attrs.get('end') + if start and start.date() < datetime.date(year=int(start_year), month=7, day=1): + raise ValidationError( + f'Začiatok súťaže ({start}) nie je v školskom roku {school_year}') + if end and end.date() > datetime.date(year=int(end_year), month=8, day=31): + raise ValidationError( + f'Koniec súťaže ({end}) nie je v školskom roku {school_year}') + return super().validate(attrs) + def create(self, validated_data): registration_link = validated_data.pop('registration_link', None) From 4dd68654c0749bac3823a1a12b4e33b1a9f0291f Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 14 Dec 2024 19:43:12 +0100 Subject: [PATCH 3/3] Check school year only if it is present --- competition/serializers.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/competition/serializers.py b/competition/serializers.py index 5864adb..36f4add 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -449,15 +449,16 @@ def validate_school_year(self, value: str): def validate(self, attrs): school_year = attrs.get('school_year') - start_year, end_year = school_year.split('/') - start = attrs.get('start') - end = attrs.get('end') - if start and start.date() < datetime.date(year=int(start_year), month=7, day=1): - raise ValidationError( - f'Začiatok súťaže ({start}) nie je v školskom roku {school_year}') - if end and end.date() > datetime.date(year=int(end_year), month=8, day=31): - raise ValidationError( - f'Koniec súťaže ({end}) nie je v školskom roku {school_year}') + if school_year is not None: + start = attrs.get('start') + end = attrs.get('end') + start_year, end_year = school_year.split('/') + if start and start.date() < datetime.date(year=int(start_year), month=7, day=1): + raise ValidationError( + f'Začiatok súťaže ({start}) nie je v školskom roku {school_year}') + if end and end.date() > datetime.date(year=int(end_year), month=8, day=31): + raise ValidationError( + f'Koniec súťaže ({end}) nie je v školskom roku {school_year}') return super().validate(attrs) def get_complete(self, obj: models.Semester):