diff options
| author | Tom Christie | 2011-07-01 17:44:08 +0100 |
|---|---|---|
| committer | Tom Christie | 2011-07-01 17:44:08 +0100 |
| commit | f7b7778a79bad4124a6f50bf461f206a6d71fa90 (patch) | |
| tree | 2bd1b84a90dec5724cda3d89b3a9faf5ca298961 /djangorestframework/parsers.py | |
| parent | c3babe751a6bab7c7888f173b7853a65ba2ea35e (diff) | |
| parent | f67c0651baf1cea65221b4fb7f4df11a54abed90 (diff) | |
| download | django-rest-framework-f7b7778a79bad4124a6f50bf461f206a6d71fa90.tar.bz2 | |
pull in markos changes, minor tweaks to yaml stuff
Diffstat (limited to 'djangorestframework/parsers.py')
| -rw-r--r-- | djangorestframework/parsers.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py index 37882984..5f19c521 100644 --- a/djangorestframework/parsers.py +++ b/djangorestframework/parsers.py @@ -16,15 +16,18 @@ from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser from django.http.multipartparser import MultiPartParserError from django.utils import simplejson as json from djangorestframework import status +from djangorestframework.compat import yaml from djangorestframework.response import ErrorResponse from djangorestframework.utils.mediatypes import media_type_matches + __all__ = ( 'BaseParser', 'JSONParser', 'PlainTextParser', 'FormParser', 'MultiPartParser', + 'YAMLParser', ) @@ -85,6 +88,27 @@ class JSONParser(BaseParser): {'detail': 'JSON parse error - %s' % unicode(exc)}) +if yaml: + class YAMLParser(BaseParser): + """ + Parses YAML-serialized data. + """ + + media_type = 'application/yaml' + + def parse(self, stream): + """ + Returns a 2-tuple of `(data, files)`. + + `data` will be an object which is the parsed content of the response. + `files` will always be `None`. + """ + try: + return (yaml.safe_load(stream), None) + except ValueError, exc: + raise ErrorResponse(status.HTTP_400_BAD_REQUEST, + {'detail': 'YAML parse error - %s' % unicode(exc)}) + class PlainTextParser(BaseParser): """ |
