Permalink
Browse files

step-3 uso de @api_view

  • Loading branch information...
rg3915 committed Feb 14, 2018
1 parent 613ddae commit 69205da9262415eaf83ff04f22a635e912880a60
Showing with 16 additions and 30 deletions.
  1. +16 −30 core/views.py
View
@@ -1,63 +1,49 @@
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from core.models import Person
from core.serializers import PersonSerializer
class JSONResponse(HttpResponse):
"""
An HttpResponse that renders its content into JSON.
"""
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)
@csrf_exempt
@api_view(['GET', 'POST'])
def person_list(request):
"""
List all persons, or create a new person.
"""
if request.method == 'GET':
persons = Person.objects.all()
serializer = PersonSerializer(persons, many=True)
return JSONResponse(serializer.data)
return Response(serializer.data)
elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = PersonSerializer(data=data)
serializer = PersonSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return JSONResponse(serializer.data, status=201)
return JSONResponse(serializer.errors, status=400)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@csrf_exempt
@api_view(['GET', 'PUT', 'DELETE'])
def person_detail(request, pk):
"""
Retrieve, update or delete a person.
Retrieve, update or delete a person instance.
"""
try:
person = Person.objects.get(pk=pk)
except Person.DoesNotExist:
return HttpResponse(status=404)
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = PersonSerializer(person)
return JSONResponse(serializer.data)
return Response(serializer.data)
elif request.method == 'PUT':
data = JSONParser().parse(request)
serializer = PersonSerializer(person, data=data)
serializer = PersonSerializer(person, data=request.data)
if serializer.is_valid():
serializer.save()
return JSONResponse(serializer.data)
return JSONResponse(serializer.errors, status=400)
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
person.delete()
return HttpResponse(status=204)
return Response(status=status.HTTP_204_NO_CONTENT)

0 comments on commit 69205da

Please sign in to comment.