Some cleanup

This commit is contained in:
Tom Christie 2012-02-20 09:36:03 +00:00
parent fbf76c87af
commit 21fcd3a906
5 changed files with 25 additions and 29 deletions

View File

@ -13,7 +13,6 @@ from djangorestframework.renderers import BaseRenderer
from djangorestframework.resources import Resource, FormResource, ModelResource from djangorestframework.resources import Resource, FormResource, ModelResource
from djangorestframework.response import Response, ImmediateResponse from djangorestframework.response import Response, ImmediateResponse
from djangorestframework.request import Request from djangorestframework.request import Request
from djangorestframework.utils import as_tuple, allowed_methods
__all__ = ( __all__ = (

View File

@ -9,11 +9,9 @@ The wrapped request then offers a richer API, in particular :
- form overloading of HTTP method, content type and content - form overloading of HTTP method, content type and content
""" """
from django.http import HttpRequest
from djangorestframework.response import ImmediateResponse from djangorestframework.response import ImmediateResponse
from djangorestframework import status from djangorestframework import status
from djangorestframework.utils.mediatypes import is_form_media_type, order_by_precedence from djangorestframework.utils.mediatypes import is_form_media_type
from djangorestframework.utils import as_tuple from djangorestframework.utils import as_tuple
from StringIO import StringIO from StringIO import StringIO

View File

@ -32,7 +32,7 @@ class Response(SimpleTemplateResponse):
An HttpResponse that may include content that hasn't yet been serialized. An HttpResponse that may include content that hasn't yet been serialized.
Kwargs: Kwargs:
- content(object). The raw content, not yet serialized. This must be simple Python \ - content(object). The raw content, not yet serialized. This must be simple Python
data that renderers can handle (e.g.: `dict`, `str`, ...) data that renderers can handle (e.g.: `dict`, `str`, ...)
- renderers(list/tuple). The renderers to use for rendering the response content. - renderers(list/tuple). The renderers to use for rendering the response content.
""" """

View File

@ -281,6 +281,6 @@ class TestPagination(TestCase):
paginated URLs. So page 1 should contain ?page=2, not ?page=1&page=2 """ paginated URLs. So page 1 should contain ?page=2, not ?page=1&page=2 """
request = self.req.get('/paginator/?page=1') request = self.req.get('/paginator/?page=1')
response = MockPaginatorView.as_view()(request) response = MockPaginatorView.as_view()(request)
content = json.loads(response.content) content = json.loads(response.rendered_content)
self.assertTrue('page=2' in content['next']) self.assertTrue('page=2' in content['next'])
self.assertFalse('page=1' in content['next']) self.assertFalse('page=1' in content['next'])

View File

@ -7,13 +7,12 @@ By setting or modifying class attributes on your view, you change it's predefine
import re import re
from django.core.urlresolvers import set_script_prefix, get_script_prefix from django.core.urlresolvers import set_script_prefix, get_script_prefix
from django.http import HttpResponse
from django.utils.html import escape 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 as DjangoView, apply_markdown
from djangorestframework.response import Response, ImmediateResponse from djangorestframework.response import ImmediateResponse
from djangorestframework.mixins import * from djangorestframework.mixins import *
from djangorestframework.utils import allowed_methods from djangorestframework.utils import allowed_methods
from djangorestframework import resources, renderers, parsers, authentication, permissions, status from djangorestframework import resources, renderers, parsers, authentication, permissions, status
@ -163,6 +162,9 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
return description return description
def markup_description(self, description): def markup_description(self, description):
"""
Apply HTML markup to the description of this view.
"""
if apply_markdown: if apply_markdown:
description = apply_markdown(description) description = apply_markdown(description)
else: else:
@ -171,11 +173,13 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
def http_method_not_allowed(self, request, *args, **kwargs): def http_method_not_allowed(self, request, *args, **kwargs):
""" """
Return an HTTP 405 error if an operation is called which does not have a handler method. Return an HTTP 405 error if an operation is called which does not have
a handler method.
""" """
raise ImmediateResponse( content = {
{'detail': 'Method \'%s\' not allowed on this resource.' % request.method}, 'detail': "Method '%s' not allowed on this resource." % request.method
status=status.HTTP_405_METHOD_NOT_ALLOWED) }
raise ImmediateResponse(content, status.HTTP_405_METHOD_NOT_ALLOWED)
def initial(self, request, *args, **kargs): def initial(self, request, *args, **kargs):
""" """
@ -211,17 +215,12 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
# all other authentication is CSRF exempt. # all other authentication is CSRF exempt.
@csrf_exempt @csrf_exempt
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.request = request self.request = self.create_request(request)
self.args = args self.args = args
self.kwargs = kwargs self.kwargs = kwargs
try: try:
# Get a custom request, built form the original request instance self.initial(request, *args, **kwargs)
self.request = request = self.create_request(request)
# `initial` is the opportunity to temper with the request,
# even completely replace it.
self.request = request = self.initial(request, *args, **kwargs)
# Authenticate and check request has the relevant permissions # Authenticate and check request has the relevant permissions
self._check_permissions() self._check_permissions()