Skip to content

Commit

Permalink
Add banner endpoints for customized messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacspe committed May 18, 2024
1 parent 808733d commit 4136864
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
67 changes: 67 additions & 0 deletions cms/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@


from datetime import datetime

from django.conf import settings
from django.shortcuts import render
from django.templatetags.l10n import localize
from django.utils.timezone import now
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.request import Request
Expand All @@ -10,6 +16,7 @@
from cms.serializers import (InfoBannerSerializer, LogoSerializer,
MenuItemShortSerializer,
MessageTemplateSerializer, PostSerializer)
from competition.models import Competition, Event, Semester, Series


class MenuItemViewSet(viewsets.ReadOnlyModelViewSet):
Expand Down Expand Up @@ -62,6 +69,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<series_id>\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<series_id>\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<competition_id>\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"""
Expand Down
4 changes: 2 additions & 2 deletions competition/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4136864

Please sign in to comment.