views.py
3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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)