parser_context includes view, request, args, kwargs. (Not meta and upload_handlers)

Consistency with renderer API.
This commit is contained in:
Tom Christie 2012-10-17 22:19:59 +01:00
parent 99d48f9003
commit 4231995fbd
5 changed files with 22 additions and 17 deletions

View File

@ -103,7 +103,9 @@ A stream-like object representing the body of the request.
### parser_context ### parser_context
If supplied, this argument will be a dictionary containing any additional context that may be required to parse the request content. By default it includes the keys `'upload_handlers'` and `'meta'`, which contain the values of the `request.upload_handlers` and `request.meta` properties. Optional. If supplied, this argument will be a dictionary containing any additional context that may be required to parse the request content.
By default this will include the following keys: `view`, `request`, `args`, `kwargs`.
## Example ## Example

View File

@ -162,11 +162,14 @@ The request data, as set by the `Response()` instantiation.
### `media_type=None` ### `media_type=None`
Optional. If provided, this is the accepted media type, as determined by the content negotiation stage. Depending on the client's `Accept:` header, this may be more specific than the renderer's `media_type` attribute, and may include media type parameters. For example `"application/json; nested=true"`. Optional. If provided, this is the accepted media type, as determined by the content negotiation stage.
Depending on the client's `Accept:` header, this may be more specific than the renderer's `media_type` attribute, and may include media type parameters. For example `"application/json; nested=true"`.
### `renderer_context=None` ### `renderer_context=None`
Optional. If provided, this is a dictionary of contextual information provided by the view. Optional. If provided, this is a dictionary of contextual information provided by the view.
By default this will include the following keys: `view`, `request`, `response`, `args`, `kwargs`. By default this will include the following keys: `view`, `request`, `response`, `args`, `kwargs`.
## Example ## Example

View File

@ -119,8 +119,10 @@ class MultiPartParser(BaseParser):
`.files` will be a `QueryDict` containing all the form files. `.files` will be a `QueryDict` containing all the form files.
""" """
parser_context = parser_context or {} parser_context = parser_context or {}
meta = parser_context['meta'] request = parser_context['request']
upload_handlers = parser_context['upload_handlers'] meta = request.META
upload_handlers = request.upload_handlers
try: try:
parser = DjangoMultiPartParser(meta, stream, upload_handlers) parser = DjangoMultiPartParser(meta, stream, upload_handlers)
data, files = parser.parse() data, files = parser.parse()

View File

@ -88,17 +88,12 @@ class Request(object):
self._stream = Empty self._stream = Empty
if self.parser_context is None: if self.parser_context is None:
self.parser_context = self._default_parser_context(request) self.parser_context = {}
self.parser_context['request'] = self
def _default_negotiator(self): def _default_negotiator(self):
return api_settings.DEFAULT_CONTENT_NEGOTIATION() return api_settings.DEFAULT_CONTENT_NEGOTIATION()
def _default_parser_context(self, request):
return {
'upload_handlers': request.upload_handlers,
'meta': request.META,
}
@property @property
def method(self): def method(self):
""" """

View File

@ -161,9 +161,12 @@ class APIView(View):
Returns a dict that is passed through to Parser.parse(), Returns a dict that is passed through to Parser.parse(),
as the `parser_context` keyword argument. as the `parser_context` keyword argument.
""" """
# Note: Additionally `request` will also be added to the context
# by the Request object.
return { return {
'upload_handlers': http_request.upload_handlers, 'view': self,
'meta': http_request.META, 'args': getattr(self, 'args', ()),
'kwargs': getattr(self, 'kwargs', {})
} }
def get_renderer_context(self): def get_renderer_context(self):
@ -171,13 +174,13 @@ class APIView(View):
Returns a dict that is passed through to Renderer.render(), Returns a dict that is passed through to Renderer.render(),
as the `renderer_context` keyword argument. as the `renderer_context` keyword argument.
""" """
# Note: Additionally 'response' will also be set on the context, # Note: Additionally 'response' will also be added to the context,
# by the Response object. # by the Response object.
return { return {
'view': self, 'view': self,
'request': self.request, 'args': getattr(self, 'args', ()),
'args': self.args, 'kwargs': getattr(self, 'kwargs', {}),
'kwargs': self.kwargs 'request': getattr(self, 'request', None)
} }
# API policy instantiation methods # API policy instantiation methods