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

This commit is contained in:
markotibold 2011-06-26 01:34:52 +02:00
parent 0b18b58c17
commit 7f536c1db3
2 changed files with 43 additions and 5 deletions

View File

@ -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):
{'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):
""" """

View File

@ -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
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__ = (
'DocumentingHTMLRenderer', 'DocumentingHTMLRenderer',
'DocumentingXHTMLRenderer', 'DocumentingXHTMLRenderer',
'DocumentingPlainTextRenderer', 'DocumentingPlainTextRenderer',
'XMLRenderer' 'XMLRenderer',
'YAMLRenderer'
) )
@ -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,
DocumentingHTMLRenderer, DocumentingHTMLRenderer,
DocumentingXHTMLRenderer, DocumentingXHTMLRenderer,
DocumentingPlainTextRenderer, DocumentingPlainTextRenderer,
XMLRenderer ) XMLRenderer,
YAMLRenderer )