poulpe/api/event/views.py
2017-07-21 17:03:33 +02:00

68 lines
1.9 KiB
Python

from django.contrib.auth import get_user_model
from rest_framework.viewsets import ModelViewSet, ViewSet
from api.event.serializers import EventSerializer, PlaceSerializer,\
ActivityTagSerializer, ActivityTemplateSerializer
from api.event.mixins import EventNestedMixin
from event.models import Event, Place, ActivityTag, ActivityTemplate
User = get_user_model()
class EventViewSet(ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = Event.objects.all()
serializer_class = EventSerializer
def perform_create(self, serializer):
serializer.save(created_by=self.request.user)
class PlaceViewSet(ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = Place.objects.all()
serializer_class = PlaceSerializer
class PlaceEventViewSet(ModelViewSet):
queryset = Place.objects.all()
serializer_class = PlaceSerializer
def list(self, request, event_pk=None):
queryset = self.queryset.filter(event=event_pk)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
class ActivityTagViewSet(ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = ActivityTag.objects.all()
serializer_class = ActivityTagSerializer
class ActivityTemplateViewSet(ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = ActivityTemplate.objects.all()
serializer_class = ActivityTemplateSerializer