mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-29 21:14:01 +03:00
Merge branch 'restframework2' of https://github.com/tomchristie/django-rest-framework into restframework2
This commit is contained in:
commit
2469cd2c83
|
@ -216,7 +216,7 @@ class DocumentingTemplateRenderer(BaseRenderer):
|
|||
"""
|
||||
|
||||
# Find the first valid renderer and render the content. (Don't use another documenting renderer.)
|
||||
renderers = [renderer for renderer in view.renderers
|
||||
renderers = [renderer for renderer in view.renderer_classes
|
||||
if not issubclass(renderer, DocumentingTemplateRenderer)]
|
||||
if not renderers:
|
||||
return '[No renderers were found]'
|
||||
|
|
|
@ -37,9 +37,9 @@ class Request(object):
|
|||
|
||||
Kwargs:
|
||||
- request(HttpRequest). The original request instance.
|
||||
- parsers(list/tuple). The parsers to use for parsing the
|
||||
- parsers_classes(list/tuple). The parsers to use for parsing the
|
||||
request content.
|
||||
- authentications(list/tuple). The authentications used to try
|
||||
- authentication_classes(list/tuple). The authentications used to try
|
||||
authenticating the request's user.
|
||||
"""
|
||||
|
||||
|
@ -47,10 +47,10 @@ class Request(object):
|
|||
_CONTENT_PARAM = api_settings.FORM_CONTENT_OVERRIDE
|
||||
_CONTENTTYPE_PARAM = api_settings.FORM_CONTENTTYPE_OVERRIDE
|
||||
|
||||
def __init__(self, request, parsers=None, authentication=None):
|
||||
def __init__(self, request, parser_classes=None, authentication_classes=None):
|
||||
self._request = request
|
||||
self.parsers = parsers or ()
|
||||
self.authentication = authentication or ()
|
||||
self.parser_classes = parser_classes or ()
|
||||
self.authentication_classes = authentication_classes or ()
|
||||
self._data = Empty
|
||||
self._files = Empty
|
||||
self._method = Empty
|
||||
|
@ -61,13 +61,13 @@ class Request(object):
|
|||
"""
|
||||
Instantiates and returns the list of parsers the request will use.
|
||||
"""
|
||||
return [parser() for parser in self.parsers]
|
||||
return [parser() for parser in self.parser_classes]
|
||||
|
||||
def get_authentications(self):
|
||||
"""
|
||||
Instantiates and returns the list of parsers the request will use.
|
||||
"""
|
||||
return [authentication() for authentication in self.authentication]
|
||||
return [authentication() for authentication in self.authentication_classes]
|
||||
|
||||
@property
|
||||
def method(self):
|
||||
|
|
|
@ -35,14 +35,14 @@ class Response(SimpleTemplateResponse):
|
|||
- content(object). The raw content, not yet serialized.
|
||||
This must be native Python data that renderers can handle.
|
||||
(e.g.: `dict`, `str`, ...)
|
||||
- renderers(list/tuple). The renderers to use for rendering the response content.
|
||||
- renderer_classes(list/tuple). The renderers to use for rendering the response content.
|
||||
"""
|
||||
|
||||
_ACCEPT_QUERY_PARAM = api_settings.URL_ACCEPT_OVERRIDE
|
||||
_IGNORE_IE_ACCEPT_HEADER = True
|
||||
|
||||
def __init__(self, content=None, status=None, headers=None, view=None,
|
||||
request=None, renderers=None, format=None):
|
||||
request=None, renderer_classes=None, format=None):
|
||||
# First argument taken by `SimpleTemplateResponse.__init__` is template_name,
|
||||
# which we don't need
|
||||
super(Response, self).__init__(None, status=status)
|
||||
|
@ -52,17 +52,17 @@ class Response(SimpleTemplateResponse):
|
|||
self.headers = headers and headers[:] or []
|
||||
self.view = view
|
||||
self.request = request
|
||||
self.renderers = renderers
|
||||
self.renderer_classes = renderer_classes
|
||||
self.format = format
|
||||
|
||||
def get_renderers(self):
|
||||
"""
|
||||
Instantiates and returns the list of renderers the response will use.
|
||||
"""
|
||||
if self.renderers is None:
|
||||
if self.renderer_classes is None:
|
||||
renderer_classes = api_settings.DEFAULT_RENDERERS
|
||||
else:
|
||||
renderer_classes = self.renderers
|
||||
renderer_classes = self.renderer_classes
|
||||
|
||||
if self.format:
|
||||
return [cls(self.view) for cls in renderer_classes
|
||||
|
@ -133,7 +133,7 @@ class Response(SimpleTemplateResponse):
|
|||
def _determine_renderer(self):
|
||||
"""
|
||||
Determines the appropriate renderer for the output, given the list of
|
||||
accepted media types, and the :attr:`renderers` set on this class.
|
||||
accepted media types, and the :attr:`renderer_classes` set on this class.
|
||||
|
||||
Returns a 2-tuple of `(renderer, media_type)`
|
||||
|
||||
|
@ -162,12 +162,12 @@ class Response(SimpleTemplateResponse):
|
|||
raise NotAcceptable
|
||||
|
||||
def _get_406_response(self):
|
||||
renderer = self.renderers[0]
|
||||
renderer = self.renderer_classes[0]
|
||||
return Response(
|
||||
{
|
||||
'detail': 'Could not satisfy the client\'s Accept header',
|
||||
'available_types': [renderer.media_type
|
||||
for renderer in self.renderers]
|
||||
for renderer in self.renderer_classes]
|
||||
},
|
||||
status=status.HTTP_406_NOT_ACCEPTABLE,
|
||||
view=self.view, request=self.request, renderers=[renderer])
|
||||
view=self.view, request=self.request, renderer_classes=[renderer])
|
||||
|
|
|
@ -23,7 +23,7 @@ class MockView(APIView):
|
|||
def put(self, request):
|
||||
return HttpResponse({'a': 1, 'b': 2, 'c': 3})
|
||||
|
||||
MockView.authentication += (TokenAuthentication,)
|
||||
MockView.authentication_classes += (TokenAuthentication,)
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^$', MockView.as_view()),
|
||||
|
|
|
@ -51,7 +51,7 @@ class RendererB(BaseRenderer):
|
|||
|
||||
|
||||
class MockView(APIView):
|
||||
renderers = (RendererA, RendererB)
|
||||
renderer_classes = (RendererA, RendererB)
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
response = Response(DUMMYCONTENT, status=DUMMYSTATUS)
|
||||
|
@ -65,23 +65,23 @@ class MockGETView(APIView):
|
|||
|
||||
|
||||
class HTMLView(APIView):
|
||||
renderers = (DocumentingHTMLRenderer, )
|
||||
renderer_classes = (DocumentingHTMLRenderer, )
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
return Response('text')
|
||||
|
||||
|
||||
class HTMLView1(APIView):
|
||||
renderers = (DocumentingHTMLRenderer, JSONRenderer)
|
||||
renderer_classes = (DocumentingHTMLRenderer, JSONRenderer)
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
return Response('text')
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^.*\.(?P<format>.+)$', MockView.as_view(renderers=[RendererA, RendererB])),
|
||||
url(r'^$', MockView.as_view(renderers=[RendererA, RendererB])),
|
||||
url(r'^jsonp/jsonrenderer$', MockGETView.as_view(renderers=[JSONRenderer, JSONPRenderer])),
|
||||
url(r'^jsonp/nojsonrenderer$', MockGETView.as_view(renderers=[JSONPRenderer])),
|
||||
url(r'^.*\.(?P<format>.+)$', MockView.as_view(renderer_classes=[RendererA, RendererB])),
|
||||
url(r'^$', MockView.as_view(renderer_classes=[RendererA, RendererB])),
|
||||
url(r'^jsonp/jsonrenderer$', MockGETView.as_view(renderer_classes=[JSONRenderer, JSONPRenderer])),
|
||||
url(r'^jsonp/nojsonrenderer$', MockGETView.as_view(renderer_classes=[JSONPRenderer])),
|
||||
url(r'^html$', HTMLView.as_view()),
|
||||
url(r'^html1$', HTMLView1.as_view()),
|
||||
url(r'^api', include('djangorestframework.urls', namespace='djangorestframework'))
|
||||
|
|
|
@ -217,7 +217,7 @@ class TestContentParsing(TestCase):
|
|||
|
||||
|
||||
class MockView(APIView):
|
||||
authentication = (SessionAuthentication,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def post(self, request):
|
||||
if request.POST.get('example') is not None:
|
||||
|
|
|
@ -26,12 +26,12 @@ class MockJsonRenderer(BaseRenderer):
|
|||
|
||||
class TestResponseDetermineRenderer(TestCase):
|
||||
|
||||
def get_response(self, url='', accept_list=[], renderers=[]):
|
||||
def get_response(self, url='', accept_list=[], renderer_classes=[]):
|
||||
kwargs = {}
|
||||
if accept_list is not None:
|
||||
kwargs['HTTP_ACCEPT'] = ','.join(accept_list)
|
||||
request = RequestFactory().get(url, **kwargs)
|
||||
return Response(request=request, renderers=renderers)
|
||||
return Response(request=request, renderer_classes=renderer_classes)
|
||||
|
||||
def test_determine_accept_list_accept_header(self):
|
||||
"""
|
||||
|
@ -62,14 +62,14 @@ class TestResponseDetermineRenderer(TestCase):
|
|||
Test that right renderer is chosen, in the order of Accept list.
|
||||
"""
|
||||
accept_list = ['application/pickle', 'application/json']
|
||||
renderers = (MockPickleRenderer, MockJsonRenderer)
|
||||
response = self.get_response(accept_list=accept_list, renderers=renderers)
|
||||
renderer_classes = (MockPickleRenderer, MockJsonRenderer)
|
||||
response = self.get_response(accept_list=accept_list, renderer_classes=renderer_classes)
|
||||
renderer, media_type = response._determine_renderer()
|
||||
self.assertEqual(media_type, 'application/pickle')
|
||||
self.assertTrue(isinstance(renderer, MockPickleRenderer))
|
||||
|
||||
renderers = (MockJsonRenderer, )
|
||||
response = self.get_response(accept_list=accept_list, renderers=renderers)
|
||||
renderer_classes = (MockJsonRenderer, )
|
||||
response = self.get_response(accept_list=accept_list, renderer_classes=renderer_classes)
|
||||
renderer, media_type = response._determine_renderer()
|
||||
self.assertEqual(media_type, 'application/json')
|
||||
self.assertTrue(isinstance(renderer, MockJsonRenderer))
|
||||
|
@ -78,8 +78,8 @@ class TestResponseDetermineRenderer(TestCase):
|
|||
"""
|
||||
Test determine renderer when Accept was not specified.
|
||||
"""
|
||||
renderers = (MockPickleRenderer, )
|
||||
response = self.get_response(accept_list=None, renderers=renderers)
|
||||
renderer_classes = (MockPickleRenderer, )
|
||||
response = self.get_response(accept_list=None, renderer_classes=renderer_classes)
|
||||
renderer, media_type = response._determine_renderer()
|
||||
self.assertEqual(media_type, '*/*')
|
||||
self.assertTrue(isinstance(renderer, MockPickleRenderer))
|
||||
|
@ -89,15 +89,15 @@ class TestResponseDetermineRenderer(TestCase):
|
|||
Test determine renderer when no renderer can satisfy the Accept list.
|
||||
"""
|
||||
accept_list = ['application/json']
|
||||
renderers = (MockPickleRenderer, )
|
||||
response = self.get_response(accept_list=accept_list, renderers=renderers)
|
||||
renderer_classes = (MockPickleRenderer, )
|
||||
response = self.get_response(accept_list=accept_list, renderer_classes=renderer_classes)
|
||||
self.assertRaises(NotAcceptable, response._determine_renderer)
|
||||
|
||||
|
||||
class TestResponseRenderContent(TestCase):
|
||||
def get_response(self, url='', accept_list=[], content=None, renderers=None):
|
||||
def get_response(self, url='', accept_list=[], content=None, renderer_classes=None):
|
||||
request = RequestFactory().get(url, HTTP_ACCEPT=','.join(accept_list))
|
||||
return Response(request=request, content=content, renderers=renderers or DEFAULT_RENDERERS)
|
||||
return Response(request=request, content=content, renderer_classes=renderer_classes or DEFAULT_RENDERERS)
|
||||
|
||||
def test_render(self):
|
||||
"""
|
||||
|
@ -168,29 +168,29 @@ class RendererB(BaseRenderer):
|
|||
|
||||
|
||||
class MockView(APIView):
|
||||
renderers = (RendererA, RendererB)
|
||||
renderer_classes = (RendererA, RendererB)
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
return Response(DUMMYCONTENT, status=DUMMYSTATUS)
|
||||
|
||||
|
||||
class HTMLView(APIView):
|
||||
renderers = (DocumentingHTMLRenderer, )
|
||||
renderer_classes = (DocumentingHTMLRenderer, )
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
return Response('text')
|
||||
|
||||
|
||||
class HTMLView1(APIView):
|
||||
renderers = (DocumentingHTMLRenderer, JSONRenderer)
|
||||
renderer_classes = (DocumentingHTMLRenderer, JSONRenderer)
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
return Response('text')
|
||||
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^.*\.(?P<format>.+)$', MockView.as_view(renderers=[RendererA, RendererB])),
|
||||
url(r'^$', MockView.as_view(renderers=[RendererA, RendererB])),
|
||||
url(r'^.*\.(?P<format>.+)$', MockView.as_view(renderer_classes=[RendererA, RendererB])),
|
||||
url(r'^$', MockView.as_view(renderer_classes=[RendererA, RendererB])),
|
||||
url(r'^html$', HTMLView.as_view()),
|
||||
url(r'^html1$', HTMLView1.as_view()),
|
||||
url(r'^restframework', include('djangorestframework.urls', namespace='djangorestframework'))
|
||||
|
|
|
@ -54,9 +54,9 @@ def _camelcase_to_spaces(content):
|
|||
|
||||
|
||||
class APIView(_View):
|
||||
renderers = api_settings.DEFAULT_RENDERERS
|
||||
parsers = api_settings.DEFAULT_PARSERS
|
||||
authentication = api_settings.DEFAULT_AUTHENTICATION
|
||||
renderer_classes = api_settings.DEFAULT_RENDERERS
|
||||
parser_classes = api_settings.DEFAULT_PARSERS
|
||||
authentication_classes = api_settings.DEFAULT_AUTHENTICATION
|
||||
throttle_classes = api_settings.DEFAULT_THROTTLES
|
||||
permission_classes = api_settings.DEFAULT_PERMISSIONS
|
||||
|
||||
|
@ -139,35 +139,35 @@ class APIView(_View):
|
|||
"""
|
||||
Return a list of all the media types that this view can parse.
|
||||
"""
|
||||
return [parser.media_type for parser in self.parsers]
|
||||
return [parser.media_type for parser in self.parser_classes]
|
||||
|
||||
@property
|
||||
def _default_parser(self):
|
||||
"""
|
||||
Return the view's default parser class.
|
||||
"""
|
||||
return self.parsers[0]
|
||||
return self.parser_classes[0]
|
||||
|
||||
@property
|
||||
def _rendered_media_types(self):
|
||||
"""
|
||||
Return an list of all the media types that this response can render.
|
||||
"""
|
||||
return [renderer.media_type for renderer in self.renderers]
|
||||
return [renderer.media_type for renderer in self.renderer_classes]
|
||||
|
||||
@property
|
||||
def _rendered_formats(self):
|
||||
"""
|
||||
Return a list of all the formats that this response can render.
|
||||
"""
|
||||
return [renderer.format for renderer in self.renderers]
|
||||
return [renderer.format for renderer in self.renderer_classes]
|
||||
|
||||
@property
|
||||
def _default_renderer(self):
|
||||
"""
|
||||
Return the response's default renderer class.
|
||||
"""
|
||||
return self.renderers[0]
|
||||
return self.renderer_classes[0]
|
||||
|
||||
def get_permissions(self):
|
||||
"""
|
||||
|
@ -201,7 +201,7 @@ class APIView(_View):
|
|||
"""
|
||||
Returns the initial request object.
|
||||
"""
|
||||
return Request(request, parsers=self.parsers, authentication=self.authentication)
|
||||
return Request(request, parser_classes=self.parser_classes, authentication_classes=self.authentication_classes)
|
||||
|
||||
def finalize_response(self, request, response, *args, **kwargs):
|
||||
"""
|
||||
|
@ -210,7 +210,7 @@ class APIView(_View):
|
|||
if isinstance(response, Response):
|
||||
response.view = self
|
||||
response.request = request
|
||||
response.renderers = self.renderers
|
||||
response.renderer_classes = self.renderer_classes
|
||||
if api_settings.FORMAT_SUFFIX_KWARG:
|
||||
response.format = kwargs.get(api_settings.FORMAT_SUFFIX_KWARG, None)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user