25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

views.py 1.3 KiB

1234567891011121314151617181920212223242526272829303132333435363738
  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
  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. drf_router = routers.DefaultRouter()
  26. drf_router.register(r'users', UserViewSet)
  27. drf_router.register(r'pins', PinViewSet)
  28. drf_router.register(r'images', ImageViewSet)