From 8da55816ea6644ae2dd44d199fa191ca104b8e19 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 18 May 2024 15:22:19 +0200 Subject: [PATCH] Banner messages (#382) --- cms/models.py | 11 +++++--- cms/views.py | 64 +++++++++++++++++++++++++++++++++++++++++++ competition/admin.py | 12 +++++++- competition/models.py | 4 +-- 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/cms/models.py b/cms/models.py index 7f077881..d0d0598b 100644 --- a/cms/models.py +++ b/cms/models.py @@ -141,11 +141,14 @@ class Meta: verbose_name='správa', help_text='Správa sa zobrazí v baneri. Správa musí byť stručná - jedna krátka veta.', max_length=200) - page = models.ForeignKey(FlatPage, on_delete=models.CASCADE, null=True) - event = models.ForeignKey(Event, on_delete=models.CASCADE, null=True) - series = models.ForeignKey(Series, on_delete=models.CASCADE, null=True) + page = models.ForeignKey( + FlatPage, on_delete=models.CASCADE, null=True, blank=True) + event = models.ForeignKey( + Event, on_delete=models.CASCADE, null=True, blank=True) + series = models.ForeignKey( + Series, on_delete=models.CASCADE, null=True, blank=True) message_template = models.ForeignKey( - MessageTemplate, on_delete=models.PROTECT, null=True) + MessageTemplate, on_delete=models.PROTECT, null=True, blank=True) def clean(self): try: diff --git a/cms/views.py b/cms/views.py index 9f7fdb94..a7590337 100644 --- a/cms/views.py +++ b/cms/views.py @@ -1,5 +1,8 @@ +from datetime import datetime + +from django.utils.timezone import now from rest_framework import viewsets from rest_framework.decorators import action from rest_framework.request import Request @@ -10,6 +13,7 @@ from cms.serializers import (InfoBannerSerializer, LogoSerializer, MenuItemShortSerializer, MessageTemplateSerializer, PostSerializer) +from competition.models import Competition, Event, Series class MenuItemViewSet(viewsets.ReadOnlyModelViewSet): @@ -62,6 +66,66 @@ class InfoBannerViewSet(viewsets.ModelViewSet): queryset = InfoBanner.objects.visible() filterset_fields = ['event', 'page', 'series'] + def format_date(self, datetime_: datetime): + return datetime_.strftime("%d.%m.%Y %H:%M") + + @action(methods=['get'], detail=False, url_path=r'series-problems/(?P\d+)') + def series_problems(self, request, series_id: int) -> list[str]: + series_messages = InfoBanner.objects.filter(series=series_id).all() + messages = [message.render_message() for message in series_messages] + series = Series.objects.get(pk=series_id) + if series.complete: + messages.append('Séria je uzavretá') + elif series.can_submit: + messages.append( + f'Termín série: {self.format_date(series.deadline)}' + ) + else: + messages.append('Prebieha opravovanie') + return Response(messages) + + @action(methods=['get'], detail=False, url_path=r'series-results/(?P\d+)') + def series_results(self, request, series_id): + series = Series.objects.get(pk=series_id) + if not series.complete: + return Response(['Poradie nie je uzavreté']) + return Response([]) + + @action(methods=['get'], detail=False, url_path=r'competition/(?P\d+)') + def event(self, request, competition_id: int) -> list[str]: + competition = Competition.objects.get(pk=competition_id) + try: + event = Event.objects.filter( + competition=competition_id, end__gte=now()).earliest('start') + except Event.DoesNotExist: + return Response([]) + event_messages = InfoBanner.objects.filter(event=event).all() + messages = [message.render_message() for message in event_messages] + + if event.registration_link is not None: + if competition.competition_type.name == 'Seminár': + if event.registration_link.start > now(): + messages.append( + 'Prihlasovanie na sústredenie bude spustené ' + f'{self.format_date(event.registration_link.start)}') + elif event.registration_link.end > now(): + messages.append( + 'Prihlasovanie na sústredenie končí ' + f'{self.format_date(event.registration_link.end)}') + else: + if event.registration_link.start > now(): + messages.append( + 'Registrácia bude spustená ' + f'{self.format_date(event.registration_link.start)}') + elif event.registration_link.end > now(): + messages.append( + 'Registrácia bude uzavretá ' + f'{self.format_date(event.registration_link.end)}') + + else: + messages.append('Registrácia ukončená') + return Response(messages) + class MessageTemplateViewSet(viewsets.ModelViewSet): """Templaty správ pre info banner/posty""" diff --git a/competition/admin.py b/competition/admin.py index 2a0451f1..3151a58d 100644 --- a/competition/admin.py +++ b/competition/admin.py @@ -3,7 +3,8 @@ from competition.models import (Comment, Competition, Event, EventRegistration, LateTag, Problem, ProblemCorrection, - Publication, Semester, Series, Solution) + Publication, RegistrationLink, Semester, + Series, Solution) @admin.register(Competition) @@ -187,6 +188,15 @@ class EventRegistrationAdmin(admin.ModelAdmin): ) +@admin.register(RegistrationLink) +class RegistrationLinkAdmin(admin.ModelAdmin): + list_display = ( + 'event', + 'start', + 'end' + ) + + @admin.register(ProblemCorrection) class ProblemCorrectionAdmin(admin.ModelAdmin): list_display = ( diff --git a/competition/models.py b/competition/models.py index 3a5e1ea1..0a997329 100644 --- a/competition/models.py +++ b/competition/models.py @@ -19,8 +19,8 @@ from base.validators import school_year_validator from competition.exceptions import FreezingNotClosedResults from competition.querysets import ActiveQuerySet -from competition.utils.school_year_manipulation import ( - get_school_year_end_by_date) +from competition.utils.school_year_manipulation import \ + get_school_year_end_by_date from personal.models import Profile, School from user.models import User