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-08-03 12:14:53 +02:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.utils.functional import cached_property
|
2017-07-18 17:48:59 +02:00
|
|
|
|
2017-07-22 00:56:30 +02:00
|
|
|
from rest_framework.viewsets import ModelViewSet
|
2017-08-03 12:14:53 +02:00
|
|
|
from rest_framework.filters import OrderingFilter
|
2017-07-18 17:48:59 +02:00
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
from event.models import Activity, ActivityTag, ActivityTemplate, Event, Place
|
|
|
|
|
|
|
|
from .serializers import (
|
|
|
|
ActivitySerializer, ActivityTagSerializer, ActivityTemplateSerializer,
|
|
|
|
EventSerializer, PlaceSerializer,
|
|
|
|
)
|
2017-07-18 17:48:59 +02:00
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
2017-07-22 00:56:30 +02:00
|
|
|
# classes utilitaires
|
2017-08-03 12:14:53 +02:00
|
|
|
|
|
|
|
class EventUrlViewSetMixin:
|
|
|
|
"""
|
|
|
|
ViewSet mixin to handle the evenk_pk from url.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def event(self):
|
|
|
|
event_pk = self.kwargs.get('event_pk')
|
|
|
|
if event_pk:
|
|
|
|
return get_object_or_404(Event, pk=event_pk)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class EventModelViewSetMixin:
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
serializer.save(event=self.event)
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
serializer.save(event=self.event)
|
|
|
|
|
|
|
|
|
|
|
|
class EventModelViewSet(
|
|
|
|
EventModelViewSetMixin,
|
|
|
|
EventUrlViewSetMixin,
|
|
|
|
ModelViewSet,
|
|
|
|
):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class EventSpecificModelViewSet(EventModelViewSet):
|
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
|
2017-07-26 13:11:58 +02:00
|
|
|
* OR objects related to the specified event
|
2017-07-22 00:56:30 +02:00
|
|
|
AND root level objects
|
|
|
|
if an event is specified it passes the event_pk
|
|
|
|
to the save method. Works fine with serializers.EventSpecificSerializer
|
2017-07-26 13:11:58 +02:00
|
|
|
Useful for models that extends EventSpecificMixin
|
2017-07-18 17:48:59 +02:00
|
|
|
"""
|
2017-08-03 12:14:53 +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
|
|
|
|
"""
|
|
|
|
queryset = super().get_queryset()
|
2017-08-03 12:14:53 +02:00
|
|
|
filters = Q(event=None)
|
|
|
|
if self.event:
|
|
|
|
filters |= Q(event=self.event)
|
|
|
|
return queryset.filter(filters)
|
2017-07-25 14:02:57 +02:00
|
|
|
|
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):
|
|
|
|
queryset = Event.objects.all()
|
|
|
|
serializer_class = EventSerializer
|
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
filter_backends = (OrderingFilter,)
|
|
|
|
ordering_fields = ('title', 'creation_date', 'beginning_date',
|
|
|
|
'ending_date', )
|
|
|
|
ordering = ('beginning_date', )
|
|
|
|
|
2017-07-22 00:56:30 +02:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
serializer.save(created_by=self.request.user)
|
2017-07-20 15:08:19 +02:00
|
|
|
|
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
class PlaceViewSet(EventSpecificModelViewSet):
|
2017-07-21 17:03:33 +02:00
|
|
|
queryset = Place.objects.all()
|
|
|
|
serializer_class = PlaceSerializer
|
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
filter_backends = (OrderingFilter,)
|
|
|
|
ordering_fields = ('name', )
|
|
|
|
ordering = ('name', )
|
|
|
|
|
2017-07-21 17:03:33 +02:00
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
class ActivityTagViewSet(EventSpecificModelViewSet):
|
2017-07-20 15:08:19 +02:00
|
|
|
queryset = ActivityTag.objects.all()
|
|
|
|
serializer_class = ActivityTagSerializer
|
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
filter_backends = (OrderingFilter,)
|
|
|
|
ordering_fields = ('is_public', 'name', )
|
|
|
|
ordering = ('is_public', 'name', )
|
2017-07-20 15:08:19 +02:00
|
|
|
|
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
class ActivityTemplateViewSet(EventModelViewSet):
|
2017-07-20 15:08:19 +02:00
|
|
|
queryset = ActivityTemplate.objects.all()
|
|
|
|
serializer_class = ActivityTemplateSerializer
|
2017-07-22 03:12:50 +02:00
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
filter_backends = (OrderingFilter,)
|
|
|
|
ordering_fields = ('title', )
|
|
|
|
ordering = ('title', )
|
2017-07-25 14:02:57 +02:00
|
|
|
|
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
class ActivityViewSet(EventModelViewSet):
|
2017-07-25 14:02:57 +02:00
|
|
|
queryset = Activity.objects.all()
|
|
|
|
serializer_class = ActivitySerializer
|
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
filter_backends = (OrderingFilter,)
|
|
|
|
ordering_fields = ('title', )
|
|
|
|
ordering = ('title', )
|