Verified Commit 376de684 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Add API endpoint for partner events

parent a80391f4
......@@ -37,7 +37,7 @@
var sources = {
events: "/api/v1/events/calendarjs",
birthdays: "/api/v1/members/birthdays",
partners: "/api/v1/partners/events",
partners: "/api/v1/partners/calendarjs",
unpublishedEvents: "/api/v1/events/unpublished"
};
......
from html import unescape
from django.utils.html import strip_tags
from rest_framework import serializers
from events.api.serializers import CalenderJSSerializer
from partners.models import PartnerEvent
from partners.models import PartnerEvent, Partner
class PartnerEventSerializer(CalenderJSSerializer):
class PartnerEventCalendarJSSerializer(CalenderJSSerializer):
class Meta(CalenderJSSerializer.Meta):
model = PartnerEvent
......@@ -20,3 +25,22 @@ class PartnerEventSerializer(CalenderJSSerializer):
def _target_blank(self, instance):
return True
class PartnerSerializer(serializers.ModelSerializer):
class Meta:
model = Partner
fields = ('pk', 'name', 'link', 'company_profile', 'address',
'zip_code', 'city', 'logo')
class PartnerEventSerializer(serializers.ModelSerializer):
class Meta:
model = PartnerEvent
fields = ('pk', 'title', 'description', 'start', 'end', 'location',
'url')
description = serializers.SerializerMethodField('_description')
def _description(self, instance):
return unescape(strip_tags(instance.description))
......@@ -3,5 +3,6 @@ from rest_framework import routers
from partners.api import viewsets
router = routers.SimpleRouter()
router.register(r'partners/events', viewsets.PartnerEventViewset)
router.register(r'partners', viewsets.PartnerViewset)
urlpatterns = router.urls
from datetime import datetime
from django.utils import timezone
from rest_framework import viewsets
from rest_framework import viewsets, filters
from rest_framework.decorators import list_route
from rest_framework.exceptions import ParseError
from rest_framework.permissions import IsAuthenticatedOrReadOnly, \
IsAuthenticated
from rest_framework.response import Response
from partners.api.serializers import PartnerEventSerializer
from partners.api.serializers import PartnerEventCalendarJSSerializer, \
PartnerEventSerializer, PartnerSerializer
from partners.models import Partner, PartnerEvent
class PartnerViewset(viewsets.ViewSet):
def _extract_date_range(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')
return end, start
class PartnerViewset(viewsets.ReadOnlyModelViewSet):
serializer_class = PartnerSerializer
queryset = Partner.objects.all()
@list_route()
def events(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')
@list_route(permission_classes=(IsAuthenticatedOrReadOnly,))
def calendarjs(self, request):
end, start = _extract_date_range(request)
queryset = PartnerEvent.objects.filter(
end__gte=start,
......@@ -31,5 +40,14 @@ class PartnerViewset(viewsets.ViewSet):
published=True
)
serializer = PartnerEventSerializer(queryset, many=True)
serializer = PartnerEventCalendarJSSerializer(queryset, many=True)
return Response(serializer.data)
class PartnerEventViewset(viewsets.ReadOnlyModelViewSet):
queryset = PartnerEvent.objects.filter(end__gte=timezone.now(),
published=True)
permission_classes = [IsAuthenticated]
filter_backends = (filters.OrderingFilter,)
ordering_fields = ('start', 'end')
serializer_class = PartnerEventSerializer
Supports Markdown
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