aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/parsers.py
diff options
context:
space:
mode:
authorTom Christie2011-05-19 08:49:57 +0100
committerTom Christie2011-05-19 08:49:57 +0100
commite7f8c06dbbbc9e4ae91327ee02cd8147777b17e2 (patch)
treed2a0788753c03eb438ac44d97520b132be0d9097 /djangorestframework/parsers.py
parent8c3280f9c0d73c4e2536f1d757ad457b4a8f1de7 (diff)
parent92c015e0495b7cf39b0d0387fe6d376812a9ebef (diff)
downloaddjango-rest-framework-e7f8c06dbbbc9e4ae91327ee02cd8147777b17e2.tar.bz2
Merge in marko's doc markup
Diffstat (limited to 'djangorestframework/parsers.py')
-rw-r--r--djangorestframework/parsers.py37
1 files changed, 21 insertions, 16 deletions
diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py
index 2fd1e15d..7c76bcc6 100644
--- a/djangorestframework/parsers.py
+++ b/djangorestframework/parsers.py
@@ -5,8 +5,9 @@ to general HTTP requests.
We need a method to be able to:
-1) Determine the parsed content on a request for methods other than POST (eg typically also PUT)
-2) Determine the parsed content on a request for media types other than application/x-www-form-urlencoded
+1.) Determine the parsed content on a request for methods other than POST (eg typically also PUT)
+
+2.) Determine the parsed content on a request for media types other than application/x-www-form-urlencoded
and multipart/form-data. (eg also handle multipart/json)
"""
@@ -23,40 +24,41 @@ __all__ = (
'JSONParser',
'PlainTextParser',
'FormParser',
- 'MultiPartParser'
+ 'MultiPartParser',
)
class BaseParser(object):
"""
- All parsers should extend BaseParser, specifying a media_type attribute,
- and overriding the parse() method.
+ All parsers should extend :class:`BaseParser`, specifying a :attr:`media_type` attribute,
+ and overriding the :meth:`parse` method.
"""
+
media_type = None
def __init__(self, view):
"""
Initialize the parser with the ``View`` instance as state,
- in case the parser needs to access any metadata on the ``View`` object.
+ in case the parser needs to access any metadata on the :obj:`View` object.
"""
self.view = view
def can_handle_request(self, content_type):
"""
- Returns `True` if this parser is able to deal with the given media type.
+ Returns :const:`True` if this parser is able to deal with the given *content_type*.
- The default implementation for this function is to check the ``media_type``
- argument against the ``media_type`` attribute set on the class to see if
+ The default implementation for this function is to check the *content_type*
+ argument against the :attr:`media_type` attribute set on the class to see if
they match.
This may be overridden to provide for other behavior, but typically you'll
- instead want to just set the ``media_type`` attribute on the class.
+ instead want to just set the :attr:`media_type` attribute on the class.
"""
return media_type_matches(content_type, self.media_type)
def parse(self, stream):
"""
- Given a stream to read from, return the deserialized output.
+ Given a *stream* to read from, return the deserialized output.
Should return a 2-tuple of (data, files).
"""
raise NotImplementedError("BaseParser.parse() Must be overridden to be implemented.")
@@ -64,8 +66,9 @@ class BaseParser(object):
class JSONParser(BaseParser):
"""
- JSON parser.
+ Parses JSON-serialized data.
"""
+
media_type = 'application/json'
def parse(self, stream):
@@ -82,10 +85,12 @@ class JSONParser(BaseParser):
{'detail': 'JSON parse error - %s' % unicode(exc)})
+
class PlainTextParser(BaseParser):
"""
Plain text parser.
"""
+
media_type = 'text/plain'
def parse(self, stream):
@@ -109,8 +114,8 @@ class FormParser(BaseParser):
"""
Returns a 2-tuple of `(data, files)`.
- `data` will be a `QueryDict` containing all the form parameters.
- `files` will always be `None`.
+ `data` will be a :class:`QueryDict` containing all the form parameters.
+ `files` will always be :const:`None`.
"""
data = parse_qs(stream.read(), keep_blank_values=True)
return (data, None)
@@ -127,8 +132,8 @@ class MultiPartParser(BaseParser):
"""
Returns a 2-tuple of `(data, files)`.
- `data` will be a `QueryDict` containing all the form parameters.
- `files` will be a `QueryDict` containing all the form files.
+ `data` will be a :class:`QueryDict` containing all the form parameters.
+ `files` will be a :class:`QueryDict` containing all the form files.
"""
upload_handlers = self.view.request._get_upload_handlers()
django_parser = DjangoMultiPartParser(self.view.request.META, stream, upload_handlers)