2017-07-18 17:48:59 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
2017-07-22 00:56:30 +02:00
|
|
|
from django.db.models import Q
|
2017-07-18 17:48:59 +02:00
|
|
|
|
2017-07-22 00:56:30 +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,\
|
|
|
|
ActivityTagSerializer, ActivityTemplateSerializer
|
|
|
|
from event.models import Event, Place, ActivityTag, ActivityTemplate
|
2017-07-18 17:48:59 +02:00
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
2017-07-22 00:56:30 +02:00
|
|
|
# classes utilitaires
|
|
|
|
class EventSpecificViewSet(ModelViewSet):
|
2017-07-18 17:48:59 +02:00
|
|
|
"""
|
2017-07-22 00:56:30 +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
|
|
|
"""
|
2017-07-22 00:56:30 +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):
|
2017-07-22 00:56:30 +02:00
|
|
|
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
|
2017-07-22 00:56:30 +02:00
|
|
|
class EventViewSet(ModelViewSet):
|
2017-07-20 15:08:19 +02:00
|
|
|
"""
|
|
|
|
This viewset automatically provides `list`, `create`, `retrieve`,
|
|
|
|
`update` and `destroy` actions.
|
|
|
|
|
|
|
|
"""
|
2017-07-22 00:56:30 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
2017-07-22 00:56:30 +02:00
|
|
|
class PlaceViewSet(EventSpecificViewSet):
|
2017-07-21 17:03:33 +02:00
|
|
|
queryset = Place.objects.all()
|
|
|
|
serializer_class = PlaceSerializer
|
|
|
|
|
|
|
|
|
2017-07-20 15:08:19 +02:00
|
|
|
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
|