Fixes to APIView

This commit is contained in:
Tom Christie 2012-09-03 16:42:57 +01:00
parent 93189ec27d
commit 1a1ccf94c2
2 changed files with 17 additions and 27 deletions

View File

@ -12,10 +12,11 @@ from django.utils.html import escape
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from djangorestframework.compat import View as DjangoView, apply_markdown from djangorestframework.compat import View, apply_markdown
from djangorestframework.response import Response from djangorestframework.response import Response
from djangorestframework.request import Request from djangorestframework.request import Request
from djangorestframework import renderers, parsers, authentication, permissions, status, exceptions from djangorestframework.settings import api_settings
from djangorestframework import parsers, authentication, permissions, status, exceptions
__all__ = ( __all__ = (
@ -61,13 +62,13 @@ def _camelcase_to_spaces(content):
return re.sub(camelcase_boundry, ' \\1', content).strip() return re.sub(camelcase_boundry, ' \\1', content).strip()
class View(DjangoView): class APIView(View):
""" """
Handles incoming requests and maps them to REST operations. Handles incoming requests and maps them to REST operations.
Performs request deserialization, response serialization, authentication and input validation. Performs request deserialization, response serialization, authentication and input validation.
""" """
renderers = renderers.DEFAULT_RENDERERS renderers = api_settings.DEFAULT_RENDERERS
""" """
List of renderer classes the view can serialize the response with, ordered by preference. List of renderer classes the view can serialize the response with, ordered by preference.
""" """

View File

@ -11,9 +11,9 @@ We'll start by rewriting the root view as a class based view. All this involves
from django.http import Http404 from django.http import Http404
from djangorestframework.views import APIView from djangorestframework.views import APIView
from djangorestframework.response import Response from djangorestframework.response import Response
from djangorestframework.status import * from djangorestframework.status import status
class CommentRoot(views.APIView): class CommentRoot(APIView):
""" """
List all comments, or create a new comment. List all comments, or create a new comment.
""" """
@ -28,20 +28,21 @@ We'll start by rewriting the root view as a class based view. All this involves
comment = serializer.object comment = serializer.object
comment.save() comment.save()
return Response(serializer.serialized, status=HTTP_201_CREATED) return Response(serializer.serialized, status=HTTP_201_CREATED)
else:
return Response(serializer.serialized_errors, status=HTTP_400_BAD_REQUEST) return Response(serializer.serialized_errors, status=HTTP_400_BAD_REQUEST)
comment_root = CommentRoot.as_view()
So far, so good. It looks pretty similar to the previous case, but we've got better seperation between the different HTTP methods. We'll also need to update the instance view. So far, so good. It looks pretty similar to the previous case, but we've got better seperation between the different HTTP methods. We'll also need to update the instance view.
class CommentInstance(views.APIView): class CommentInstance(APIView):
""" """
Retrieve, update or delete a comment instance. Retrieve, update or delete a comment instance.
""" """
def get_object(self, pk): def get_object(self, pk):
try: try:
return Poll.objects.get(pk=pk) return Comment.objects.get(pk=pk)
except Poll.DoesNotExist: except Comment.DoesNotExist:
raise Http404 raise Http404
def get(self, request, pk, format=None): def get(self, request, pk, format=None):
@ -56,28 +57,16 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
comment = serializer.deserialized comment = serializer.deserialized
comment.save() comment.save()
return Response(serializer.data) return Response(serializer.data)
else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None): def delete(self, request, pk, format=None):
comment = self.get_object(pk) comment = self.get_object(pk)
comment.delete() comment.delete()
return Response(status=HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
comment_instance = CommentInstance.as_view()
That's looking good. Again, it's still pretty similar to the function based view right now. That's looking good. Again, it's still pretty similar to the function based view right now.
Since we're now working with class based views, rather than function based views, we'll also need to update our urlconf slightly.
from blogpost import views
from djangorestframework.urlpatterns import format_suffix_patterns
urlpatterns = patterns('',
url(r'^$', views.CommentRoot.as_view()),
url(r'^(?P<id>[0-9]+)$', views.CommentInstance.as_view())
)
urlpatterns = format_suffix_patterns(urlpatterns)
Okay, we're done. If you run the development server everything should be working just as before. Okay, we're done. If you run the development server everything should be working just as before.
## Using mixins ## Using mixins