Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

views.py 1.9 KiB

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