mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-29 13:04:03 +03:00
Very basic YAML support. Probably needs some tweaking, and definitely needs tests.
This commit is contained in:
parent
0b18b58c17
commit
7f536c1db3
|
@ -18,12 +18,15 @@ from djangorestframework import status
|
|||
from djangorestframework.response import ErrorResponse
|
||||
from djangorestframework.utils.mediatypes import media_type_matches
|
||||
|
||||
import yaml
|
||||
|
||||
__all__ = (
|
||||
'BaseParser',
|
||||
'JSONParser',
|
||||
'PlainTextParser',
|
||||
'FormParser',
|
||||
'MultiPartParser',
|
||||
'YAMLParser',
|
||||
)
|
||||
|
||||
|
||||
|
@ -84,6 +87,26 @@ class JSONParser(BaseParser):
|
|||
{'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):
|
||||
"""
|
||||
|
|
|
@ -11,7 +11,7 @@ from django.core.serializers.json import DateTimeAwareJSONEncoder
|
|||
from django.template import RequestContext, loader
|
||||
from django.utils import simplejson as json
|
||||
|
||||
from djangorestframework import status
|
||||
|
||||
from djangorestframework.compat import apply_markdown
|
||||
from djangorestframework.utils import dict2xml, url_resolves
|
||||
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 import VERSION
|
||||
|
||||
from decimal import Decimal
|
||||
import re
|
||||
import string
|
||||
from urllib import quote_plus
|
||||
import yaml
|
||||
|
||||
__all__ = (
|
||||
'BaseRenderer',
|
||||
|
@ -31,7 +30,8 @@ __all__ = (
|
|||
'DocumentingHTMLRenderer',
|
||||
'DocumentingXHTMLRenderer',
|
||||
'DocumentingPlainTextRenderer',
|
||||
'XMLRenderer'
|
||||
'XMLRenderer',
|
||||
'YAMLRenderer'
|
||||
)
|
||||
|
||||
|
||||
|
@ -120,6 +120,20 @@ class XMLRenderer(BaseRenderer):
|
|||
return ''
|
||||
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):
|
||||
"""
|
||||
|
@ -346,6 +360,7 @@ DEFAULT_RENDERERS = ( JSONRenderer,
|
|||
DocumentingHTMLRenderer,
|
||||
DocumentingXHTMLRenderer,
|
||||
DocumentingPlainTextRenderer,
|
||||
XMLRenderer )
|
||||
XMLRenderer,
|
||||
YAMLRenderer )
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user