From e34db082df3a7df2af7b4f73dece188c18d97f3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mih=C3=A1lik?= Date: Sun, 12 Nov 2023 15:02:08 +0100 Subject: [PATCH] Optional registration link when creating event --- base/management/commands/restoredb.py | 1 + competition/fixtures/registration_link.json | 3 --- competition/fixtures/semesters.json | 9 +++++--- competition/models.py | 11 +++++++--- competition/serializers.py | 23 ++++++++++++++++++--- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/base/management/commands/restoredb.py b/base/management/commands/restoredb.py index 98a6f600..b535eda3 100644 --- a/base/management/commands/restoredb.py +++ b/base/management/commands/restoredb.py @@ -67,6 +67,7 @@ def handle(self, *args, **options): 'grades', 'late_tags', 'semesters', + 'registration_link', 'event_registrations', 'event_registrations_more', 'solutions', diff --git a/competition/fixtures/registration_link.json b/competition/fixtures/registration_link.json index 63797d8e..5c54e0e5 100644 --- a/competition/fixtures/registration_link.json +++ b/competition/fixtures/registration_link.json @@ -3,7 +3,6 @@ "model": "competition.RegistrationLink", "pk": 0, "fields": { - "event": 0, "start": "2020-04-01T20:00:00.000Z", "end": "2021-10-01T20:00:00.000Z", "url": "https://seminar.strom.sk/sk/prispevky/", @@ -14,7 +13,6 @@ "model": "competition.RegistrationLink", "pk": 1, "fields": { - "event": 1, "start": "2021-11-01T20:00:00.000Z", "end": "2021-12-01T20:00:00.000Z", "url": "https://seminar.strom.sk/sk/prispevky/", @@ -25,7 +23,6 @@ "model": "competition.RegistrationLink", "pk": 2, "fields": { - "event": 2, "start": "2020-04-01T20:00:00.000Z", "end": "2020-05-01T20:00:00.000Z", "url": "https://seminar.strom.sk/sk/prispevky/", diff --git a/competition/fixtures/semesters.json b/competition/fixtures/semesters.json index fc1f2770..01ffd8fd 100644 --- a/competition/fixtures/semesters.json +++ b/competition/fixtures/semesters.json @@ -8,7 +8,8 @@ "school_year": "2019/2020", "start": "2020-01-01T20:00:00+02:00", "end": "2025-06-01T20:00:00+02:00", - "season_code": 1 + "season_code": 1, + "registration_link": 0 } }, { @@ -177,7 +178,8 @@ "school_year": "2020/2021", "start": "2020-10-08T00:00:00+02:00", "end": "2020-12-02T00:00:00+02:00", - "season_code": 0 + "season_code": 0, + "registration_link": 1 } }, { @@ -324,7 +326,8 @@ "school_year": "2018/2019", "start": "2019-02-26T00:00:00+02:00", "end": "2019-04-24T00:00:00+02:00", - "season_code": 1 + "season_code": 1, + "registration_link": 2 } }, { diff --git a/competition/models.py b/competition/models.py index f3bbab87..6dff6385 100644 --- a/competition/models.py +++ b/competition/models.py @@ -150,7 +150,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.OneToOneField( + "competition.RegistrationLink", + on_delete=models.SET_NULL, + null=True, + ) objects = ActiveQuerySet.as_manager() @@ -641,14 +647,13 @@ 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): + # pylint: disable=no-member return self.event.can_user_modify(user) diff --git a/competition/serializers.py b/competition/serializers.py index 70151eb7..55410dc5 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,30 @@ 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 = validated_data.pop('registration_link', None) + + if registration_link is not None: + registration_link = RegistrationLink.objects.create( + **registration_link, + ) + + return Event.objects.create( + registration_link=registration_link, + **validated_data, + ) + @ts_interface(context='competition') class CompetitionTypeSerializer(serializers.ModelSerializer):