aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/parsers.py
diff options
context:
space:
mode:
authorTom Christie2011-04-27 18:20:29 +0100
committerTom Christie2011-04-27 18:20:29 +0100
commitda60f68f50dbcc177cb8b31df428d2daa905e9c6 (patch)
tree6393597cdf4b6b0cc84f5ce792d3770d011aa189 /djangorestframework/parsers.py
parent659898ffaf24f74b62e73c487cd81bad21904790 (diff)
parentb508ca38d44f458e3eabaa4ffd3500d80a71eb9e (diff)
downloaddjango-rest-framework-da60f68f50dbcc177cb8b31df428d2daa905e9c6.tar.bz2
Merge previous checkins
Diffstat (limited to 'djangorestframework/parsers.py')
-rw-r--r--djangorestframework/parsers.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py
index caa76277..03f8bf8f 100644
--- a/djangorestframework/parsers.py
+++ b/djangorestframework/parsers.py
@@ -11,12 +11,59 @@ We need a method to be able to:
from django.http.multipartparser import MultiPartParser as DjangoMPParser
from django.utils import simplejson as json
+<<<<<<< local
from djangorestframework.response import ErrorResponse
+=======
+from djangorestframework.response import ResponseException
+>>>>>>> other
from djangorestframework import status
from djangorestframework.utils import as_tuple
from djangorestframework.mediatypes import MediaType
from djangorestframework.compat import parse_qs
+<<<<<<< local
+=======
+try:
+ from urlparse import parse_qs
+except ImportError:
+ from cgi import parse_qs
+
+class ParserMixin(object):
+ parsers = ()
+
+ def parse(self, stream, content_type):
+ """
+ Parse the request content.
+
+ May raise a 415 ResponseException (Unsupported Media Type),
+ or a 400 ResponseException (Bad Request).
+ """
+ parsers = as_tuple(self.parsers)
+
+ parser = None
+ for parser_cls in parsers:
+ if parser_cls.handles(content_type):
+ parser = parser_cls(self)
+ break
+
+ if parser is None:
+ raise ResponseException(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
+ {'error': 'Unsupported media type in request \'%s\'.' %
+ content_type.media_type})
+
+ return parser.parse(stream)
+
+ @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]
+>>>>>>> other
class BaseParser(object):