<gh_stars>0
from django.contrib.auth import login, logout
from django.contrib.auth.models import User
from knox.models import AuthToken
from rest_framework import generics, permissions
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.response import Response
from knox.views import LoginView as KnoxLoginView

from vozila_backend.common.serialisers import UserSerializer, RegisterSerializer


class UserList(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class UserDetail(generics.RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class RegisterAPI(generics.GenericAPIView):
    serializer_class = RegisterSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)[1]
        })


class LoginAPI(KnoxLoginView):
    permission_classes = (permissions.AllowAny,)

    def post(self, request, format=None):
        serializer = AuthTokenSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        login(request, user)
        return super(LoginAPI, self).post(request, format=None)

    # def get_post_response_data(self, request, token, instance):
    #     super(LoginAPI, self).get_post_response_data(request, token, instance)
    #     data = {
    #
    #     }
    #     if UserSerializer is not None:
    #         data["username"] = UserSerializer(
    #             request.user,
    #             context=self.get_context()
    #         ).data.get('username')
    #         data["id"] = UserSerializer(
    #             request.user,
    #             context=self.get_context()
    #         ).data.get('id')
    #     return data


# def logout_view(request):
#     logout(request)


# TODO - to be implemented
# class CreateFollowView(generics.CreateAPIView):
#     permission_classes = [permissions.IsAuthenticatedOrReadOnly]
#     queryset = Follow.objects.all()
#     serializer_class = FollowSerializer
#
#     def perform_create(self, serializer):
#         pass
#
#     def create(self, request, *args, **kwargs):
#         super().create(request, *args, **kwargs)
#
#         return like_dislike_follow_toggle(request, *args, **kwargs)

from django.contrib.auth import login, logout
from django.contrib.auth.models import User
from knox.models import AuthToken
from rest_framework import generics, permissions
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.response import Response
from knox.views import LoginView as KnoxLoginView

from vozila_backend.common.serialisers import UserSerializer, RegisterSerializer


class UserList(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class UserDetail(generics.RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class RegisterAPI(generics.GenericAPIView):
    serializer_class = RegisterSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)[1]
        })


class LoginAPI(KnoxLoginView):
    permission_classes = (permissions.AllowAny,)

    def post(self, request, format=None):
        serializer = AuthTokenSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        login(request, user)
        return super(LoginAPI, self).post(request, format=None)

    # def get_post_response_data(self, request, token, instance):
    #     super(LoginAPI, self).get_post_response_data(request, token, instance)
    #     data = {
    #
    #     }
    #     if UserSerializer is not None:
    #         data["username"] = UserSerializer(
    #             request.user,
    #             context=self.get_context()
    #         ).data.get('username')
    #         data["id"] = UserSerializer(
    #             request.user,
    #             context=self.get_context()
    #         ).data.get('id')
    #     return data


# def logout_view(request):
#     logout(request)


# TODO - to be implemented
# class CreateFollowView(generics.CreateAPIView):
#     permission_classes = [permissions.IsAuthenticatedOrReadOnly]
#     queryset = Follow.objects.all()
#     serializer_class = FollowSerializer
#
#     def perform_create(self, serializer):
#         pass
#
#     def create(self, request, *args, **kwargs):
#         super().create(request, *args, **kwargs)
#
#         return like_dislike_follow_toggle(request, *args, **kwargs)
# def get_post_response_data(self, request, token, instance): #     super(LoginAPI, self).get_post_response_data(request, token, instance) #     data = { # #     } #     if UserSerializer is not None: #         data["username"] = UserSerializer( #             request.user, #             context=self.get_context() #         ).data.get('username') #         data["id"] = UserSerializer( #             request.user, #             context=self.get_context() #         ).data.get('id') #     return data # def logout_view(request): #     logout(request) # TODO - to be implemented # class CreateFollowView(generics.CreateAPIView): #     permission_classes = [permissions.IsAuthenticatedOrReadOnly] #     queryset = Follow.objects.all() #     serializer_class = FollowSerializer # #     def perform_create(self, serializer): #         pass # #     def create(self, request, *args, **kwargs): #         super().create(request, *args, **kwargs) # #         return like_dislike_follow_toggle(request, *args, **kwargs)