poulpe/api/event/views.py

102 lines
3 KiB
Python
Raw Normal View History

2017-07-18 17:48:59 +02:00
from django.contrib.auth import get_user_model
from django.db.models import Q
2017-07-18 17:48:59 +02:00
from rest_framework.viewsets import ModelViewSet
2017-07-18 17:48:59 +02:00
2017-07-20 15:08:19 +02:00
from api.event.serializers import EventSerializer, PlaceSerializer,\
2017-07-25 14:02:57 +02:00
ActivityTagSerializer, ActivityTemplateSerializer, ActivitySerializer
from event.models import Event, Place, ActivityTag, ActivityTemplate, Activity
2017-07-18 17:48:59 +02:00
User = get_user_model()
# classes utilitaires
class EventSpecificViewSet(ModelViewSet):
2017-07-18 17:48:59 +02:00
"""
ViewSet that returns :
* rootlevel objects if no Event is specified
* OR objects related to the specidied event
AND root level objects
if an event is specified it passes the event_pk
to the save method. Works fine with serializers.EventSpecificSerializer
Useful for models that exetends EventSpecificMixin
2017-07-18 17:48:59 +02:00
"""
def get_queryset(self):
"""
Warning : You may want to override this method
and not call with super
"""
event_pk = self.kwargs.get('event_pk')
queryset = super().get_queryset()
if event_pk:
return queryset.filter(Q(event=event_pk) | Q(event=None))
return queryset.filter(event=None)
2017-07-18 17:48:59 +02:00
def perform_create(self, serializer):
event_pk = self.kwargs.get('event_pk')
serializer.save(event_pk=event_pk)
2017-07-20 15:08:19 +02:00
2017-07-25 14:02:57 +02:00
def perform_update(self, serializer):
event_pk = self.kwargs.get('event_pk')
serializer.save(event_pk=event_pk)
2017-07-20 15:08:19 +02:00
2017-07-22 02:09:11 +02:00
# ViewSets
class EventViewSet(ModelViewSet):
2017-07-20 15:08:19 +02:00
"""
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)
2017-07-20 15:08:19 +02:00
class PlaceViewSet(EventSpecificViewSet):
2017-07-21 17:03:33 +02:00
queryset = Place.objects.all()
serializer_class = PlaceSerializer
2017-07-22 02:09:31 +02:00
class ActivityTagViewSet(EventSpecificViewSet):
2017-07-20 15:08:19 +02:00
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
2017-07-22 03:12:50 +02:00
def perform_create(self, serializer):
event_pk = self.kwargs.get('event_pk')
serializer.save(event_pk=event_pk)
2017-07-25 14:02:57 +02:00
def perform_update(self, serializer):
event_pk = self.kwargs.get('event_pk')
serializer.save(event_pk=event_pk)
class ActivityViewSet(ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = Activity.objects.all()
serializer_class = ActivitySerializer
def perform_create(self, serializer):
event_pk = self.kwargs.get('event_pk')
serializer.save(event_pk=event_pk)
def perform_update(self, serializer):
event_pk = self.kwargs.get('event_pk')
serializer.save(event_pk=event_pk)