Permalink
Browse files

mixins

  • Loading branch information...
junkboy0315 committed Feb 8, 2019
1 parent 4585bb6 commit 32e625bbdc70e0935bd2cc60e62466ab76a6cebb
Showing with 24 additions and 41 deletions.
  1. +24 −41 tutorial/snippets/views.py
@@ -1,57 +1,40 @@
from django.http import Http404
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import generics, mixins, status
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer


class SnippetList(APIView):
class SnippetList(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
"""
- 全ての snippets を表示する。
- 新しい snippet を作成する
"""
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer

def get(self, request, format=None):
snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True)
return Response(serializer.data)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)

def post(self, request, format=None):
serializer = SnippetSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)


class SnippetDetail(APIView):
class SnippetDetail(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView):
"""
単一のスニペットの、取得・更新・削除を行う。
"""
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer

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

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

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

def delete(self, request, pk, format=None):
snippet = self.get_object(pk)
snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, *args, **kwargs): # なお、pkは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.destroy(request, *args, **kwargs)

0 comments on commit 32e625b

Please sign in to comment.