|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- from django.utils.decorators import method_decorator
- from django.views.decorators.cache import cache_page
- 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 taggit.models import Tag
-
- from core import serializers as api
- from core.models import Image, Pin, Board
- from core.permissions import IsOwnerOrReadOnly, OwnerOnlyIfPrivate
- from core.serializers import filter_private_pin, filter_private_board
-
-
- 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):
- serializer_class = api.PinSerializer
- filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
- filter_fields = ("submitter__username", 'tags__name', )
- ordering_fields = ('-id', )
- ordering = ('-id', )
- permission_classes = [IsOwnerOrReadOnly("submitter"), OwnerOnlyIfPrivate("submitter")]
-
- def get_queryset(self):
- query = Pin.objects.all()
- request = self.request
- return filter_private_pin(request, query)
-
-
- class BoardViewSet(viewsets.ModelViewSet):
- serializer_class = api.BoardSerializer
- filter_backends = (DjangoFilterBackend, OrderingFilter, SearchFilter)
- search_fields = ("name", )
- filter_fields = ("submitter__username", )
- ordering_fields = ('-id', )
- ordering = ('-id', )
- permission_classes = [IsOwnerOrReadOnly("submitter"), OwnerOnlyIfPrivate("submitter")]
-
- def get_queryset(self):
- return filter_private_board(self.request, Board.objects.all())
-
-
- class BoardAutoCompleteViewSet(
- mixins.ListModelMixin,
- viewsets.GenericViewSet,
- ):
- serializer_class = api.BoardAutoCompleteSerializer
- filter_backends = (DjangoFilterBackend, OrderingFilter)
- filter_fields = ("submitter__username", )
- ordering_fields = ('-id', )
- ordering = ('-id', )
- pagination_class = None
- permission_classes = [OwnerOnlyIfPrivate("submitter"), ]
-
- def get_queryset(self):
- return filter_private_board(self.request, Board.objects.all())
-
-
- class TagAutoCompleteViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
- queryset = Tag.objects.all()
- serializer_class = api.TagAutoCompleteSerializer
- pagination_class = None
-
- @method_decorator(cache_page(60 * 5))
- def list(self, request, *args, **kwargs):
- return super(TagAutoCompleteViewSet, self).list(
- request,
- *args,
- **kwargs
- )
-
-
- drf_router = routers.DefaultRouter()
- drf_router.register(r'pins', PinViewSet, basename="pin")
- drf_router.register(r'images', ImageViewSet)
- drf_router.register(r'boards', BoardViewSet, basename="board")
- drf_router.register(r'tags-auto-complete', TagAutoCompleteViewSet)
- drf_router.register(r'boards-auto-complete', BoardAutoCompleteViewSet, base_name="board")
|