views.py 3.52 KB
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Video, Tags
from .serializer import VideoSerializer, TagSerializer
from rest_framework.decorators import api_view
from django.shortcuts import get_object_or_404
from rest_framework import generics
from rest_framework import mixins
from rest_framework import viewsets

# Create your views here.
class VideoViewSet(viewsets.ModelViewSet):
    queryset = Video.objects.all()
    serializer_class = VideoSerializer

class TagViewSet(viewsets.ModelViewSet):
    queryset = Tags.objects.order_by('tag')
    serializer_class = TagSerializer


# FBV 예제
@api_view(['GET','POST'])
def Video_list(request):
    if request.method == 'GET':
        qs = Video.objects.all()
        serializer = VideoSerializer(qs, many=True)
        return Response(serializer.data)
    else:
        for i in request.data['tag']:
          convertTag = {'tag':i}
          tagSerial = TagSerializer(data=convertTag)
          
          if tagSerial.is_valid():
            tagSerial.save()
          else:
            print(tagSerial.errors)
           
        request.data['tags'] = ','.join(request.data['tag'])
        serializer = VideoSerializer(data=request.data)
        
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=201)

        print(serializer.errors)
        return Response(serializer.errors, status=400)

@api_view(['GET'])
def Tag_list(request):
    if request.method == 'GET':
        qs = Tags.objects.order_by('tag')
        serializer = TagSerializer(qs, many=True)
        returnData = serializer.data
        return Response(serializer.data)
        

    # 삭제, 수정은 나중에 넣음.
    # elif request.method == 'PUT':
    #   serializer = VideoSerializer(video, data=request.data)
    #   if serializer.is_valid():
    #       serializer.save()
    #       return Response(serializer.data)
    #   return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    # else:
    #   video.delete()
    #   return Response(status=status.HTTP_204_NO_CONTENT)

# @api_view(['GET','PUT','DELETE'])
# def post_detail(request, pk):
#     post = get_object_or_404(Post, pk=pk)
#     if request.method == 'GET':
#         serializer = PostSerializer(post)
#         return Response(serializer.data)
#     elif request.method == 'PUT':
#         serializer = PostSerializer(post, data=reqeust.data)
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data)
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     else:
#         post.delete()
#         return Response(status=status.HTTP_204_NO_CONTENT)





# Mixin example

# class TagListMixins(mixins.ListModelMixin, mixins.CreateModelMixin,generics.GenericAPIView):
#     def get(self, request, *args, **kwargs):
#         return self.list(request)
    
#     def post(self, request, *args, **kwargs):
#         return self.create(request)


# class PostDetailMixins(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.GenericAPIView):
#     queryset = Video.objects.all()
#     serializer_class = VideoSerializer

#     def get(self, request, *args, **kwargs):
#         return self.retrieve(request, *args, **kwargs)

#     def put(self, request, *args, **kwargs):
#         return self.update(request, *args, **kwargs)
        
#     def delete(self, request, *args, **kwargs):
#         return self.delete(request, *args, **kwargs)