Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Banner messages #382

Merged
merged 4 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
64 changes: 64 additions & 0 deletions cms/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +13,7 @@
from cms.serializers import (InfoBannerSerializer, LogoSerializer,
MenuItemShortSerializer,
MessageTemplateSerializer, PostSerializer)
from competition.models import Competition, Event, Series


class MenuItemViewSet(viewsets.ReadOnlyModelViewSet):
Expand Down Expand Up @@ -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<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
12 changes: 11 additions & 1 deletion competition/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = (
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
Loading