Commit 7f536c1d by markotibold

Very basic YAML support. Probably needs some tweaking, and definitely needs tests.

parent 0b18b58c
...@@ -18,12 +18,15 @@ from djangorestframework import status ...@@ -18,12 +18,15 @@ from djangorestframework import status
from djangorestframework.response import ErrorResponse from djangorestframework.response import ErrorResponse
from djangorestframework.utils.mediatypes import media_type_matches from djangorestframework.utils.mediatypes import media_type_matches
import yaml
__all__ = ( __all__ = (
'BaseParser', 'BaseParser',
'JSONParser', 'JSONParser',
'PlainTextParser', 'PlainTextParser',
'FormParser', 'FormParser',
'MultiPartParser', 'MultiPartParser',
'YAMLParser',
) )
...@@ -84,6 +87,26 @@ class JSONParser(BaseParser): ...@@ -84,6 +87,26 @@ class JSONParser(BaseParser):
{'detail': 'JSON parse error - %s' % unicode(exc)}) {'detail': 'JSON parse error - %s' % unicode(exc)})
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): class PlainTextParser(BaseParser):
""" """
......
...@@ -11,7 +11,7 @@ from django.core.serializers.json import DateTimeAwareJSONEncoder ...@@ -11,7 +11,7 @@ from django.core.serializers.json import DateTimeAwareJSONEncoder
from django.template import RequestContext, loader from django.template import RequestContext, loader
from django.utils import simplejson as json from django.utils import simplejson as json
from djangorestframework import status
from djangorestframework.compat import apply_markdown from djangorestframework.compat import apply_markdown
from djangorestframework.utils import dict2xml, url_resolves from djangorestframework.utils import dict2xml, url_resolves
from djangorestframework.utils.breadcrumbs import get_breadcrumbs from djangorestframework.utils.breadcrumbs import get_breadcrumbs
...@@ -19,10 +19,9 @@ from djangorestframework.utils.description import get_name, get_description ...@@ -19,10 +19,9 @@ from djangorestframework.utils.description import get_name, get_description
from djangorestframework.utils.mediatypes import get_media_type_params, add_media_type_param, media_type_matches from djangorestframework.utils.mediatypes import get_media_type_params, add_media_type_param, media_type_matches
from djangorestframework import VERSION from djangorestframework import VERSION
from decimal import Decimal
import re
import string import string
from urllib import quote_plus from urllib import quote_plus
import yaml
__all__ = ( __all__ = (
'BaseRenderer', 'BaseRenderer',
...@@ -31,7 +30,8 @@ __all__ = ( ...@@ -31,7 +30,8 @@ __all__ = (
'DocumentingHTMLRenderer', 'DocumentingHTMLRenderer',
'DocumentingXHTMLRenderer', 'DocumentingXHTMLRenderer',
'DocumentingPlainTextRenderer', 'DocumentingPlainTextRenderer',
'XMLRenderer' 'XMLRenderer',
'YAMLRenderer'
) )
...@@ -120,6 +120,20 @@ class XMLRenderer(BaseRenderer): ...@@ -120,6 +120,20 @@ class XMLRenderer(BaseRenderer):
return '' return ''
return dict2xml(obj) return dict2xml(obj)
class YAMLRenderer(BaseRenderer):
"""
Renderer which serializes to YAML.
"""
media_type = 'application/yaml'
def render(self, obj=None, media_type=None):
"""
Renders *obj* into serialized YAML.
"""
if obj is None:
return ''
return yaml.dump(obj)
class TemplateRenderer(BaseRenderer): class TemplateRenderer(BaseRenderer):
""" """
...@@ -346,6 +360,7 @@ DEFAULT_RENDERERS = ( JSONRenderer, ...@@ -346,6 +360,7 @@ DEFAULT_RENDERERS = ( JSONRenderer,
DocumentingHTMLRenderer, DocumentingHTMLRenderer,
DocumentingXHTMLRenderer, DocumentingXHTMLRenderer,
DocumentingPlainTextRenderer, DocumentingPlainTextRenderer,
XMLRenderer ) XMLRenderer,
YAMLRenderer )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment