from django_filters.rest_framework import DjangoFilterBackend from rest_framework import viewsets, mixins, routers from rest_framework.filters import SearchFilter, OrderingFilter from rest_framework.viewsets import GenericViewSet from core import serializers as api from core.models import Image, Pin, Board from core.permissions import IsOwnerOrReadOnly class ImageViewSet(mixins.CreateModelMixin, GenericViewSet): queryset = Image.objects.all() serializer_class = api.ImageSerializer def create(self, request, *args, **kwargs): return super(ImageViewSet, self).create(request, *args, **kwargs) class PinViewSet(viewsets.ModelViewSet): queryset = Pin.objects.all().select_related('image', 'submitter') serializer_class = api.PinSerializer filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ("submitter__username", 'tags__name', ) ordering_fields = ('-id', ) ordering = ('-id', ) permission_classes = [IsOwnerOrReadOnly("submitter"), ] class BoardViewSet(viewsets.ModelViewSet): queryset = Board.objects.all() serializer_class = api.BoardSerializer filter_backends = (DjangoFilterBackend, OrderingFilter) filter_fields = ("submitter__username", ) ordering_fields = ('-id', ) ordering = ('-id', ) permission_classes = [IsOwnerOrReadOnly("submitter"), ] class BoardAutoCompleteViewSet( mixins.ListModelMixin, viewsets.GenericViewSet, ): queryset = Board.objects.all() serializer_class = api.BoardAutoCompleteSerializer filter_backends = (DjangoFilterBackend, OrderingFilter) filter_fields = ("submitter__username", ) ordering_fields = ('-id', ) ordering = ('-id', ) pagination_class = None drf_router = routers.DefaultRouter() drf_router.register(r'pins', PinViewSet) drf_router.register(r'images', ImageViewSet) drf_router.register(r'boards', BoardViewSet) drf_router.register(r'boards-auto-complete', BoardAutoCompleteViewSet)