views.py 3.02 KB
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.response import Response
from rest_framework import viewsets
import os
from django.http.request import QueryDict
from django.http import Http404
from django.shortcuts import get_object_or_404
from api.models import Video, VideoFile
from api.serializers import VideoSerializer, VideoFileSerializer
from api import  file_upload_path

# Create your views here.


class VideoViewSet(viewsets.ModelViewSet):
    queryset = Video.objects.all()
    serializer_class = VideoSerializer


class VideoFileViewSet(viewsets.ModelViewSet):
    queryset = VideoFile.objects.all()
    serializer_class = VideoFileSerializer

class VideoFileUploadView(APIView):
    parser_classes = (MultiPartParser, FormParser)

    def get(self, request, format=None):
        videoFiles = VideoFile.objects.all()
        serializer = VideoFileSerializer(videoFiles, many=True)
        return Response(serializer.data)

    def post(self, req, *args, **kwargs):
       # 요청된 데이터를 꺼냄( QueryDict)         
        new_data = req.data.dict()
        
        # 요청된 파일 객체
        print(req.data)
        file_name = req.data['file']
        
        # 저장될 파일의 풀path를 생성        
        new_file_full_name = file_upload_path(file_name.name)
        
        # 새롭게 생성된 파일의 경로
        file_path = '\\'.join(new_file_full_name.split('\\')[0:-1])        

        new_data['file_path'] = file_path
        new_data['file_origin_name'] = req.data['file'].name
        new_data['file_save_name'] = req.data['file']

        new_query_dict = QueryDict('', mutable=True)
        new_query_dict.update(new_data)
        
        file_serializer = VideoFileSerializer(data = new_query_dict)
        if file_serializer.is_valid():
            file_serializer.save()       
            print(file_serializer.data)
                 
            return Response(True, status=status.HTTP_201_CREATED)
        else:
            return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    
class VideoFileList(APIView):

    def get_object(self, pk):
        try:
            return VideoFile.objects.get(pk=pk)
        except VideoFile.DoesNotExist:
            raise Http404

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

    def put(self, request, pk, format=None):
        video = self.get_object(pk)
        serializer = VideoFileSerializer(video, 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):
        video = self.get_object(pk)
        video.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)