from rest_framework.decorators import api_view
from rest_framework.response import Response
from django.contrib.auth.models import User
from application.serializers import UserSerializer, ApplicationSerializer, ApplicationListSerializer
from rest_framework import viewsets, status
from application.models import Application, validate_tc
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
from django.http import Http404
from rest_framework import permissions
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework.parsers import FileUploadParser

class ApplicationList(APIView):
    def get(self, request, format=None):
        applications = Application.objects.all()
        serializer = ApplicationListSerializer(applications, many=True)
        return Response(serializer.data)

class ApplicationDetail(APIView):
    def get_object(self, pk):
        try:
            return Application.objects.get(pk=pk)
        except Application.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        applications = self.get_object(pk)
        serializer = ApplicationSerializer(applications)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        application = self.get_object(pk)
        serializer = ApplicationSerializer(application, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        application = self.get_object(pk)
        application.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

class ApplicationCreate(APIView):
    def post(self, request):
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            Application.objects.create(user=User.objects.get(pk=serializer.data['id']))
            return Response(serializer.data)

class FileUpload(APIView):
    parser_classes = (FileUploadParser,)

    def post(self, request, format=None):
        file_obj = request.FILES['file']
        # do some stuff with uploaded file
        return Response(status=204)

@api_view(['GET'])
def 
(request, tc):
    if len(User.objects.filter(username=tc)) != 0:
        return Response({'tc': tc}, status=status.HTTP_302_FOUND)
    try:
        validate_tc(tc)
        return Response({'tc': tc}, status=status.HTTP_200_OK)
    except:
        return Response({'tc': tc}, status=status.HTTP_400_BAD_REQUEST)

tc_validator