from django.contrib.auth import get_user_model
from django.db.models import query
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated

from .serializers import FollowSerializer, UserInfoSerializer, UserProfileSerializer, UserSerializer, AuthTokenSerializer

from rest_framework import generics, serializers
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.settings import api_settings


class UserCreateView(generics.CreateAPIView):

    serializer_class = UserSerializer


class ManageUserView(generics.RetrieveUpdateDestroyAPIView):
    serializer_class = UserInfoSerializer
    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get_object(self):
        return self.request.user


class FollowersView(generics.ListAPIView):
    serializer_class = FollowSerializer

    def get_queryset(self):
        username = self.kwargs['username']
        queryset = get_user_model().objects.get(username=username).followers.all()
        return queryset


class UserProfileView(generics.RetrieveAPIView):

    lookup_field = 'username'
    queryset = get_user_model().objects.all()
    serializer_class = UserProfileSerializer
    permission_classes = (IsAuthenticated,)


class AuthTokenView(ObtainAuthToken):

    serializer_class = AuthTokenSerializer
    renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES

from django.contrib.auth import get_user_model
from django.db.models import query
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated

from .serializers import FollowSerializer, UserInfoSerializer, UserProfileSerializer, UserSerializer, AuthTokenSerializer

from rest_framework import generics, serializers
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.settings import api_settings


class UserCreateView(generics.CreateAPIView):

    serializer_class = UserSerializer


class ManageUserView(generics.RetrieveUpdateDestroyAPIView):
    serializer_class = UserInfoSerializer
    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get_object(self):
        return self.request.user


class FollowersView(generics.ListAPIView):
    serializer_class = FollowSerializer

    def get_queryset(self):
        username = self.kwargs['username']
        queryset = get_user_model().objects.get(username=username).followers.all()
        return queryset


class UserProfileView(generics.RetrieveAPIView):

    lookup_field = 'username'
    queryset = get_user_model().objects.all()
    serializer_class = UserProfileSerializer
    permission_classes = (IsAuthenticated,)


class AuthTokenView(ObtainAuthToken):

    serializer_class = AuthTokenSerializer
    renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
