Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

57 строки
1.9 KiB

  1. from django_filters.rest_framework import DjangoFilterBackend
  2. from rest_framework import viewsets, mixins, routers
  3. from rest_framework.filters import SearchFilter, OrderingFilter
  4. from rest_framework.viewsets import GenericViewSet
  5. from core import serializers as api
  6. from core.models import Image, Pin, Board
  7. from core.permissions import IsOwnerOrReadOnly
  8. class ImageViewSet(mixins.CreateModelMixin, GenericViewSet):
  9. queryset = Image.objects.all()
  10. serializer_class = api.ImageSerializer
  11. def create(self, request, *args, **kwargs):
  12. return super(ImageViewSet, self).create(request, *args, **kwargs)
  13. class PinViewSet(viewsets.ModelViewSet):
  14. queryset = Pin.objects.all().select_related('image', 'submitter')
  15. serializer_class = api.PinSerializer
  16. filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
  17. filter_fields = ("submitter__username", 'tags__name', )
  18. ordering_fields = ('-id', )
  19. ordering = ('-id', )
  20. permission_classes = [IsOwnerOrReadOnly("submitter"), ]
  21. class BoardViewSet(viewsets.ModelViewSet):
  22. queryset = Board.objects.all()
  23. serializer_class = api.BoardSerializer
  24. filter_backends = (DjangoFilterBackend, OrderingFilter)
  25. filter_fields = ("submitter__username", )
  26. ordering_fields = ('-id', )
  27. ordering = ('-id', )
  28. permission_classes = [IsOwnerOrReadOnly("submitter"), ]
  29. class BoardAutoCompleteViewSet(
  30. mixins.ListModelMixin,
  31. viewsets.GenericViewSet,
  32. ):
  33. queryset = Board.objects.all()
  34. serializer_class = api.BoardAutoCompleteSerializer
  35. filter_backends = (DjangoFilterBackend, OrderingFilter)
  36. filter_fields = ("submitter__username", )
  37. ordering_fields = ('-id', )
  38. ordering = ('-id', )
  39. pagination_class = None
  40. drf_router = routers.DefaultRouter()
  41. drf_router.register(r'pins', PinViewSet)
  42. drf_router.register(r'images', ImageViewSet)
  43. drf_router.register(r'boards', BoardViewSet)
  44. drf_router.register(r'boards-auto-complete', BoardAutoCompleteViewSet)