|
1 | 1 |
|
2 | 2 |
|
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +from django.utils.timezone import now |
3 | 6 | from rest_framework import viewsets
|
4 | 7 | from rest_framework.decorators import action
|
5 | 8 | from rest_framework.request import Request
|
|
10 | 13 | from cms.serializers import (InfoBannerSerializer, LogoSerializer,
|
11 | 14 | MenuItemShortSerializer,
|
12 | 15 | MessageTemplateSerializer, PostSerializer)
|
| 16 | +from competition.models import Competition, Event, Series |
13 | 17 |
|
14 | 18 |
|
15 | 19 | class MenuItemViewSet(viewsets.ReadOnlyModelViewSet):
|
@@ -62,6 +66,66 @@ class InfoBannerViewSet(viewsets.ModelViewSet):
|
62 | 66 | queryset = InfoBanner.objects.visible()
|
63 | 67 | filterset_fields = ['event', 'page', 'series']
|
64 | 68 |
|
| 69 | + def format_date(self, datetime_: datetime): |
| 70 | + return datetime_.strftime("%d.%m.%Y %H:%M") |
| 71 | + |
| 72 | + @action(methods=['get'], detail=False, url_path=r'series-problems/(?P<series_id>\d+)') |
| 73 | + def series_problems(self, request, series_id: int) -> list[str]: |
| 74 | + series_messages = InfoBanner.objects.filter(series=series_id).all() |
| 75 | + messages = [message.render_message() for message in series_messages] |
| 76 | + series = Series.objects.get(pk=series_id) |
| 77 | + if series.complete: |
| 78 | + messages.append('Séria je uzavretá') |
| 79 | + elif series.can_submit: |
| 80 | + messages.append( |
| 81 | + f'Termín série: {self.format_date(series.deadline)}' |
| 82 | + ) |
| 83 | + else: |
| 84 | + messages.append('Prebieha opravovanie') |
| 85 | + return Response(messages) |
| 86 | + |
| 87 | + @action(methods=['get'], detail=False, url_path=r'series-results/(?P<series_id>\d+)') |
| 88 | + def series_results(self, request, series_id): |
| 89 | + series = Series.objects.get(pk=series_id) |
| 90 | + if not series.complete: |
| 91 | + return Response(['Poradie nie je uzavreté']) |
| 92 | + return Response([]) |
| 93 | + |
| 94 | + @action(methods=['get'], detail=False, url_path=r'competition/(?P<competition_id>\d+)') |
| 95 | + def event(self, request, competition_id: int) -> list[str]: |
| 96 | + competition = Competition.objects.get(pk=competition_id) |
| 97 | + try: |
| 98 | + event = Event.objects.filter( |
| 99 | + competition=competition_id, end__gte=now()).earliest('start') |
| 100 | + except Event.DoesNotExist: |
| 101 | + return Response([]) |
| 102 | + event_messages = InfoBanner.objects.filter(event=event).all() |
| 103 | + messages = [message.render_message() for message in event_messages] |
| 104 | + |
| 105 | + if event.registration_link is not None: |
| 106 | + if competition.competition_type.name == 'Seminár': |
| 107 | + if event.registration_link.start > now(): |
| 108 | + messages.append( |
| 109 | + 'Prihlasovanie na sústredenie bude spustené ' |
| 110 | + f'{self.format_date(event.registration_link.start)}') |
| 111 | + elif event.registration_link.end > now(): |
| 112 | + messages.append( |
| 113 | + 'Prihlasovanie na sústredenie končí ' |
| 114 | + f'{self.format_date(event.registration_link.end)}') |
| 115 | + else: |
| 116 | + if event.registration_link.start > now(): |
| 117 | + messages.append( |
| 118 | + 'Registrácia bude spustená ' |
| 119 | + f'{self.format_date(event.registration_link.start)}') |
| 120 | + elif event.registration_link.end > now(): |
| 121 | + messages.append( |
| 122 | + 'Registrácia bude uzavretá ' |
| 123 | + f'{self.format_date(event.registration_link.end)}') |
| 124 | + |
| 125 | + else: |
| 126 | + messages.append('Registrácia ukončená') |
| 127 | + return Response(messages) |
| 128 | + |
65 | 129 |
|
66 | 130 | class MessageTemplateViewSet(viewsets.ModelViewSet):
|
67 | 131 | """Templaty správ pre info banner/posty"""
|
|
0 commit comments