diff --git a/competition/models.py b/competition/models.py index e5cc3ec0..ff4b0054 100644 --- a/competition/models.py +++ b/competition/models.py @@ -119,6 +119,20 @@ def __str__(self): return self.name +class RegistrationLink(models.Model): + class Meta: + verbose_name = 'link na registráciu' + verbose_name_plural = 'linky na registráciu' + + url = models.URLField(verbose_name='url registrácie') + start = models.DateTimeField(verbose_name='Začiatok registrácie') + end = models.DateTimeField(verbose_name='Koniec registrácie') + additional_info = models.TextField(verbose_name='Doplňujúce informácie') + + def can_user_modify(self, user): + return self.event.can_user_modify(user) + + class Event(models.Model): """ Slúži na označenie instancie súťaže (Napríklad: Matboj 2020, @@ -150,7 +164,13 @@ class Meta: start = models.DateTimeField(verbose_name='dátum začiatku súťaže') end = models.DateTimeField(verbose_name='dátum konca súťaže') additional_name = models.CharField( - max_length=50, verbose_name='Prísvlastok súťaže', null=True, blank=True) + max_length=50, verbose_name='Prívlastok súťaže', null=True, blank=True) + + registration_link = models.ForeignKey( + RegistrationLink, + on_delete=models.SET_NULL, + null=True, + ) objects = ActiveQuerySet.as_manager() @@ -629,22 +649,6 @@ def make_name_on_creation(sender, instance, created, **kwargs): instance.generate_name() -class RegistrationLink(models.Model): - class Meta: - verbose_name = 'link na registráciu' - verbose_name_plural = 'linky na registráciu' - - event = models.OneToOneField( - Event, related_name='registration_link', on_delete=models.CASCADE) - url = models.URLField(verbose_name='url registrácie') - start = models.DateTimeField(verbose_name='Začiatok registrácie') - end = models.DateTimeField(verbose_name='Koniec registrácie') - additional_info = models.TextField(verbose_name='Doplňujúce informácie') - - def can_user_modify(self, user): - return self.event.can_user_modify(user) - - class ProblemCorrection(models.Model): # TODO: Add images class Meta: diff --git a/competition/serializers.py b/competition/serializers.py index 655d8f9e..404daf73 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -3,7 +3,7 @@ from rest_framework import serializers from competition import models -from competition.models import Problem +from competition.models import Problem, RegistrationLink, Event from personal.serializers import ProfileShortSerializer, SchoolShortSerializer @@ -43,13 +43,27 @@ class Meta: @ts_interface(context='competition') class EventSerializer(ModelWithParticipationSerializer): - publication_set = PublicationSerializer(many=True) - registration_link = RegistrationLinkSerializer(many=False) + publication_set = PublicationSerializer(many=True, read_only=True) + registration_link = RegistrationLinkSerializer( + many=False, + required=False, + allow_null=True, + ) class Meta: model = models.Event fields = '__all__' + def create(self, validated_data): + registration_link = RegistrationLink.objects.create( + **validated_data.pop('registration_link'), + ) + + return Event.objects.create( + registration_link=registration_link, + **validated_data, + ) + @ts_interface(context='competition') class CompetitionTypeSerializer(serializers.ModelSerializer):