views.py
3.52 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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)