Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

views.py 1.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. from users.models import User
  9. class UserViewSet(mixins.RetrieveModelMixin, GenericViewSet):
  10. queryset = User.objects.all()
  11. serializer_class = api.UserSerializer
  12. class ImageViewSet(mixins.CreateModelMixin, GenericViewSet):
  13. queryset = Image.objects.all()
  14. serializer_class = api.ImageSerializer
  15. def create(self, request, *args, **kwargs):
  16. return super(ImageViewSet, self).create(request, *args, **kwargs)
  17. class PinViewSet(viewsets.ModelViewSet):
  18. queryset = Pin.objects.all().select_related('image', 'submitter')
  19. serializer_class = api.PinSerializer
  20. filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
  21. filter_fields = ("submitter__username", 'tags__name', )
  22. ordering_fields = ('-id', )
  23. ordering = ('-id', )
  24. permission_classes = [IsOwnerOrReadOnly("submitter"), ]
  25. class BoardViewSet(viewsets.ModelViewSet):
  26. queryset = Board.objects.all()
  27. serializer_class = api.BoardSerializer
  28. filter_backends = (DjangoFilterBackend, OrderingFilter)
  29. filter_fields = ("submitter__username", )
  30. ordering_fields = ('-id', )
  31. ordering = ('-id', )
  32. permission_classes = [IsOwnerOrReadOnly("submitter"), ]
  33. drf_router = routers.DefaultRouter()
  34. drf_router.register(r'users', UserViewSet)
  35. drf_router.register(r'pins', PinViewSet)
  36. drf_router.register(r'images', ImageViewSet)
  37. drf_router.register(r'boards', BoardViewSet)