Skip to content

Commit

Permalink
Added calendar API.
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRomaa committed Mar 10, 2021
1 parent 7ab947c commit e4fef5c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
19 changes: 19 additions & 0 deletions apps/calendar/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from rest_framework import serializers

from apps.events.models import Event
from apps.training.models import TrainingSession
from apps.users.serializers import BaseUserSerializer


class EventCalendarSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = ['id', 'name', 'host', 'start', 'end']


class TrainingCalendarSerializer(serializers.ModelSerializer):
student = BaseUserSerializer()

class Meta:
model = TrainingSession
fields = ['id', 'student', 'level', 'type', 'position', 'start', 'end']
10 changes: 10 additions & 0 deletions apps/calendar/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path

from . import views

urlpatterns = [
path('events/', views.EventCalendarView.as_view()),
path('events/<int:year>/<int:month>/', views.EventCalendarView.as_view()),
path('training/', views.TrainingCalendarView.as_view()),
path('training/<int:year>/<int:month>/', views.TrainingCalendarView.as_view()),
]
38 changes: 38 additions & 0 deletions apps/calendar/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from datetime import datetime
from rest_framework.response import Response
from rest_framework.views import APIView

from zhu_core.permissions import ReadOnly
from .serializers import *
from ..training.models import Status


class EventCalendarView(APIView):
permission_classes = [ReadOnly]

def get(self, request, year=datetime.now().year, month=datetime.now().month):
"""
Get all events for given month and year.
Defaults to current month of current year.
"""
events = Event.objects.filter(start__month=month, start__year=year)

if not (request.user.is_authenticated and request.user.is_staff):
events = events.exclude(hidden=True)

serializer = EventCalendarSerializer(events, many=True)
return Response(serializer.data)


class TrainingCalendarView(APIView):
permission_classes = [ReadOnly]

def get(self, request, year=datetime.now().year, month=datetime.now().month):
"""
Get all training sessions for given month and year.
Defaults to current month of current year.
"""
sessions = TrainingSession.objects.filter(start__month=month, start__year=year).exclude(status=Status.CANCELLED)

serializer = TrainingCalendarSerializer(sessions, many=True)
return Response(serializer.data)
1 change: 1 addition & 0 deletions zhu_core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
'rest_framework',
'rest_framework.authtoken',
'apps.announcements',
'apps.calendar',
'apps.connections',
'apps.events',
'apps.feedback',
Expand Down
1 change: 1 addition & 0 deletions zhu_core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

urlpatterns = [
path('api/announcements/', include('apps.announcements.urls')),
path('api/calendar/', include('apps.calendar.urls')),
path('api/connections/', include('apps.connections.urls')),
path('auth/', include('apps.vatsim.urls')),
path('api/events/', include('apps.events.urls')),
Expand Down

0 comments on commit e4fef5c

Please sign in to comment.