diff options
| author | Tom Christie | 2011-04-27 18:20:29 +0100 |
|---|---|---|
| committer | Tom Christie | 2011-04-27 18:20:29 +0100 |
| commit | da60f68f50dbcc177cb8b31df428d2daa905e9c6 (patch) | |
| tree | 6393597cdf4b6b0cc84f5ce792d3770d011aa189 /djangorestframework/parsers.py | |
| parent | 659898ffaf24f74b62e73c487cd81bad21904790 (diff) | |
| parent | b508ca38d44f458e3eabaa4ffd3500d80a71eb9e (diff) | |
| download | django-rest-framework-da60f68f50dbcc177cb8b31df428d2daa905e9c6.tar.bz2 | |
Merge previous checkins
Diffstat (limited to 'djangorestframework/parsers.py')
| -rw-r--r-- | djangorestframework/parsers.py | 47 |
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): |
