Unverified Commit 841970ff authored by Luuk Scholten's avatar Luuk Scholten Committed by Thom Wiggers

Add api for getting events in date range

parent 26767d7d
from django.utils import timezone
from django.urls import reverse
from rest_framework import serializers
from events.models import Event
class CalenderJSSerializer(serializers.ModelSerializer):
class Meta:
fields = (
'start', 'end', 'all_day', 'is_birthday',
'url', 'title', 'description',
'background_color', 'text_color', 'target_blank'
)
start = serializers.SerializerMethodField('_start')
end = serializers.SerializerMethodField('_end')
all_day = serializers.SerializerMethodField('_all_day')
is_birthday = serializers.SerializerMethodField('_is_birthday')
url = serializers.SerializerMethodField('_url')
title = serializers.SerializerMethodField('_title')
description = serializers.SerializerMethodField('_description')
background_color = serializers.SerializerMethodField('_background_color')
text_color = serializers.SerializerMethodField('_text_color')
target_blank = serializers.SerializerMethodField('_target_blank')
def _start(self, instance):
return timezone.localtime(instance.start)
def _end(self, instance):
return timezone.localtime(instance.end)
def _all_day(self, instance):
return False
def _is_birthday(self, instance):
return False
def _url(self, instance):
raise NotImplementedError
def _title(self, instance):
return instance.title
def _description(self, instance):
return instance.description
def _background_color(self, instance):
pass
def _text_color(self, instance):
pass
def _target_blank(self, instance):
return False
class EventSerializer(CalenderJSSerializer):
class Meta(CalenderJSSerializer.Meta):
model = Event
def _url(self, instance):
return reverse('#')
from rest_framework import routers
from events.api import viewsets
router = routers.SimpleRouter()
router.register(r'events', viewsets.EventViewset)
urlpatterns = router.urls
from rest_framework import viewsets
from rest_framework.exceptions import ParseError
from rest_framework.response import Response
from django.utils import timezone
from datetime import datetime
from events.api.serializers import EventSerializer
from events.models import Event
class EventViewset(viewsets.ViewSet):
queryset = Event.objects.all()
def list(self, request):
try:
start = timezone.make_aware(
datetime.strptime(request.query_params['start'], '%Y-%m-%d')
)
end = timezone.make_aware(
datetime.strptime(request.query_params['end'], '%Y-%m-%d')
)
except:
raise ParseError(detail='start or end query parameters invalid')
queryset = self.queryset.filter(
end__gte=start,
start__lte=end,
published=True
)
serializer = EventSerializer(queryset, many=True)
return Response(serializer.data)
......@@ -63,6 +63,9 @@ urlpatterns = [
url(r'^career/', include('partners.urls', namespace='partners')),
url(r'^contact$', TemplateView.as_view(template_name='singlepages/contact.html'), name='contact'),
url(r'^private-thumbnails/(?P<size_fit>\d+x\d+_[01])/(?P<path>.*)', private_thumbnails, name='private-thumbnails'),
url(r'^api/', include([
url(r'^', include('events.api.urls')),
])),
# Default login helpers
url(r'^', include('django.contrib.auth.urls')),
url(r'^i18n/', include('django.conf.urls.i18n')),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment