aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/parsers.py
diff options
context:
space:
mode:
authortom christie tom@tomchristie.com2011-02-07 08:23:54 +0000
committertom christie tom@tomchristie.com2011-02-07 08:23:54 +0000
commit027ffed21064b1ec304a1ea559104382313d76f4 (patch)
tree8621c8741d76cc672da207cf314574a4fbd828d1 /djangorestframework/parsers.py
parenta8bcb2edc63564522b89b9950ea0882d6b25f24a (diff)
downloaddjango-rest-framework-027ffed21064b1ec304a1ea559104382313d76f4.tar.bz2
Refactor a bunch of stuff into mixins, more tests
Diffstat (limited to 'djangorestframework/parsers.py')
-rw-r--r--djangorestframework/parsers.py33
1 files changed, 32 insertions, 1 deletions
diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py
index a656e2eb..0d5121e9 100644
--- a/djangorestframework/parsers.py
+++ b/djangorestframework/parsers.py
@@ -5,7 +5,38 @@ try:
except ImportError:
import simplejson as json
-# TODO: Make all parsers only list a single media_type, rather than a list
+
+class ParserMixin(object):
+ parsers = ()
+
+ def parse(self, content_type, content):
+ # See RFC 2616 sec 3 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
+ split = content_type.split(';', 1)
+ if len(split) > 1:
+ content_type = split[0]
+ content_type = content_type.strip()
+
+ media_type_to_parser = dict([(parser.media_type, parser) for parser in self.parsers])
+
+ try:
+ parser = media_type_to_parser[content_type]
+ except KeyError:
+ raise ResponseException(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
+ {'error': 'Unsupported media type in request \'%s\'.' % content_type})
+
+ return parser(self).parse(content)
+
+ @property
+ def parsed_media_types(self):
+ """Return an list of all the media types that this ParserMixin can parse."""
+ return [parser.media_type for parser in self.parsers]
+
+ @property
+ def default_parser(self):
+ """Return the ParerMixin's most prefered emitter.
+ (This has no behavioural effect, but is may be used by documenting emitters)"""
+ return self.parsers[0]
+
class BaseParser(object):
"""All parsers should extend BaseParser, specifing a media_type attribute,