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 core import serializers as api
from core.models import Image, Pin
from core.permissions import IsOwnerOrReadOnly
from users.models import User


class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = api.UserSerializer


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):
    queryset = Pin.objects.all()
    serializer_class = api.PinSerializer
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_fields = ("submitter__username", 'tags__name', )
    ordering_fields = ('-id', )
    ordering = ('-id', )
    permission_classes = [IsOwnerOrReadOnly("submitter"), ]


drf_router = routers.DefaultRouter()
drf_router.register(r'users', UserViewSet)
drf_router.register(r'pins', PinViewSet)
drf_router.register(r'images', ImageViewSet)

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 core import serializers as api
from core.models import Image, Pin
from core.permissions import IsOwnerOrReadOnly
from users.models import User


class UserViewSet(mixins.RetrieveModelMixin, GenericViewSet):
    queryset = User.objects.all()
    serializer_class = api.UserSerializer


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):
    queryset = Pin.objects.all()
    serializer_class = api.PinSerializer
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_fields = ("submitter__username", 'tags__name', )
    ordering_fields = ('-id', )
    ordering = ('-id', )
    permission_classes = [IsOwnerOrReadOnly("submitter"), ]


drf_router = routers.DefaultRouter()
drf_router.register(r'users', UserViewSet)
drf_router.register(r'pins', PinViewSet)
drf_router.register(r'images', ImageViewSet)

Allow only the user-data fetching
Refactor: Allow only the user-data fetching

  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 core import serializers as api
  from core.models import Image, Pin
  from core.permissions import IsOwnerOrReadOnly
  from users.models import User
  
  
- class UserViewSet(viewsets.ModelViewSet):
+ class UserViewSet(mixins.RetrieveModelMixin, GenericViewSet):
      queryset = User.objects.all()
      serializer_class = api.UserSerializer
  
  
  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):
      queryset = Pin.objects.all()
      serializer_class = api.PinSerializer
      filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
      filter_fields = ("submitter__username", 'tags__name', )
      ordering_fields = ('-id', )
      ordering = ('-id', )
      permission_classes = [IsOwnerOrReadOnly("submitter"), ]
  
  
  drf_router = routers.DefaultRouter()
  drf_router.register(r'users', UserViewSet)
  drf_router.register(r'pins', PinViewSet)
  drf_router.register(r'images', ImageViewSet)
  
Allow only the user-data fetching
## Code Before:
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 core import serializers as api
from core.models import Image, Pin
from core.permissions import IsOwnerOrReadOnly
from users.models import User


class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = api.UserSerializer


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):
    queryset = Pin.objects.all()
    serializer_class = api.PinSerializer
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_fields = ("submitter__username", 'tags__name', )
    ordering_fields = ('-id', )
    ordering = ('-id', )
    permission_classes = [IsOwnerOrReadOnly("submitter"), ]


drf_router = routers.DefaultRouter()
drf_router.register(r'users', UserViewSet)
drf_router.register(r'pins', PinViewSet)
drf_router.register(r'images', ImageViewSet)

## Instruction:
Allow only the user-data fetching
## Code After:
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 core import serializers as api
from core.models import Image, Pin
from core.permissions import IsOwnerOrReadOnly
from users.models import User


class UserViewSet(mixins.RetrieveModelMixin, GenericViewSet):
    queryset = User.objects.all()
    serializer_class = api.UserSerializer


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):
    queryset = Pin.objects.all()
    serializer_class = api.PinSerializer
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_fields = ("submitter__username", 'tags__name', )
    ordering_fields = ('-id', )
    ordering = ('-id', )
    permission_classes = [IsOwnerOrReadOnly("submitter"), ]


drf_router = routers.DefaultRouter()
drf_router.register(r'users', UserViewSet)
drf_router.register(r'pins', PinViewSet)
drf_router.register(r'images', ImageViewSet)
