mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-27 03:54:01 +03:00
parent
e712ab0ba1
commit
aaa14a3e17
|
@ -215,7 +215,7 @@ class DocumentingTemplateRenderer(BaseRenderer):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Find the first valid renderer and render the content. (Don't use another documenting renderer.)
|
# Find the first valid renderer and render the content. (Don't use another documenting renderer.)
|
||||||
renderers = [renderer for renderer in view.renderers if not isinstance(renderer, DocumentingTemplateRenderer)]
|
renderers = [renderer for renderer in view.renderers if not issubclass(renderer, DocumentingTemplateRenderer)]
|
||||||
if not renderers:
|
if not renderers:
|
||||||
return '[No renderers were found]'
|
return '[No renderers were found]'
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ from djangorestframework import status
|
||||||
from djangorestframework.views import View
|
from djangorestframework.views import View
|
||||||
from djangorestframework.compat import View as DjangoView
|
from djangorestframework.compat import View as DjangoView
|
||||||
from djangorestframework.renderers import BaseRenderer, JSONRenderer, YAMLRenderer, \
|
from djangorestframework.renderers import BaseRenderer, JSONRenderer, YAMLRenderer, \
|
||||||
XMLRenderer, JSONPRenderer
|
XMLRenderer, JSONPRenderer, DocumentingHTMLRenderer
|
||||||
from djangorestframework.parsers import JSONParser, YAMLParser
|
from djangorestframework.parsers import JSONParser, YAMLParser
|
||||||
from djangorestframework.mixins import ResponseMixin
|
from djangorestframework.mixins import ResponseMixin
|
||||||
from djangorestframework.response import Response
|
from djangorestframework.response import Response
|
||||||
|
@ -46,15 +46,30 @@ class MockView(ResponseMixin, DjangoView):
|
||||||
|
|
||||||
|
|
||||||
class MockGETView(View):
|
class MockGETView(View):
|
||||||
|
|
||||||
def get(self, request, **kwargs):
|
def get(self, request, **kwargs):
|
||||||
return {'foo': ['bar', 'baz']}
|
return {'foo': ['bar', 'baz']}
|
||||||
|
|
||||||
|
|
||||||
|
class HTMLView(View):
|
||||||
|
renderers = (DocumentingHTMLRenderer, )
|
||||||
|
|
||||||
|
def get(self, request, **kwargs):
|
||||||
|
return 'text'
|
||||||
|
|
||||||
|
class HTMLView1(View):
|
||||||
|
renderers = (DocumentingHTMLRenderer, JSONRenderer)
|
||||||
|
|
||||||
|
def get(self, request, **kwargs):
|
||||||
|
return 'text'
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^.*\.(?P<format>.+)$', MockView.as_view(renderers=[RendererA, RendererB])),
|
url(r'^.*\.(?P<format>.+)$', MockView.as_view(renderers=[RendererA, RendererB])),
|
||||||
url(r'^$', MockView.as_view(renderers=[RendererA, RendererB])),
|
url(r'^$', MockView.as_view(renderers=[RendererA, RendererB])),
|
||||||
url(r'^jsonp/jsonrenderer$', MockGETView.as_view(renderers=[JSONRenderer, JSONPRenderer])),
|
url(r'^jsonp/jsonrenderer$', MockGETView.as_view(renderers=[JSONRenderer, JSONPRenderer])),
|
||||||
url(r'^jsonp/nojsonrenderer$', MockGETView.as_view(renderers=[JSONPRenderer])),
|
url(r'^jsonp/nojsonrenderer$', MockGETView.as_view(renderers=[JSONPRenderer])),
|
||||||
|
url(r'^html$', HTMLView.as_view()),
|
||||||
|
url(r'^html1$', HTMLView1.as_view()),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -327,3 +342,166 @@ class XMLRendererTestCase(TestCase):
|
||||||
self.assertTrue(xml.startswith('<?xml version="1.0" encoding="utf-8"?>\n<root>'))
|
self.assertTrue(xml.startswith('<?xml version="1.0" encoding="utf-8"?>\n<root>'))
|
||||||
self.assertTrue(xml.endswith('</root>'))
|
self.assertTrue(xml.endswith('</root>'))
|
||||||
self.assertTrue(string in xml, '%r not in %r' % (string, xml))
|
self.assertTrue(string in xml, '%r not in %r' % (string, xml))
|
||||||
|
|
||||||
|
class HTMLView(View):
|
||||||
|
renderers = (DocumentingHTMLRenderer)
|
||||||
|
|
||||||
|
def get(self, request, **kwargs):
|
||||||
|
return 'text'
|
||||||
|
|
||||||
|
urlpatterns += patterns('',
|
||||||
|
url(r'^/html$', HTMLView.as_view()),
|
||||||
|
)
|
||||||
|
|
||||||
|
class Issue122Tests(TestCase):
|
||||||
|
"""
|
||||||
|
Tests that cover issues.
|
||||||
|
"""
|
||||||
|
|
||||||
|
urls = 'djangorestframework.tests.renderers'
|
||||||
|
|
||||||
|
def test_without_callback_with_json_renderer(self):
|
||||||
|
"""
|
||||||
|
Test JSONP rendering with View JSON Renderer.
|
||||||
|
"""
|
||||||
|
resp = self.client.get('/jsonp/jsonrenderer',
|
||||||
|
HTTP_ACCEPT='application/json-p')
|
||||||
|
self.assertEquals(resp.status_code, 200)
|
||||||
|
self.assertEquals(resp['Content-Type'], 'application/json-p')
|
||||||
|
self.assertEquals(resp.content, 'callback(%s);' % _flat_repr)
|
||||||
|
|
||||||
|
def test_without_callback_without_json_renderer(self):
|
||||||
|
"""
|
||||||
|
Test JSONP rendering without View JSON Renderer.
|
||||||
|
"""
|
||||||
|
resp = self.client.get('/jsonp/nojsonrenderer',
|
||||||
|
HTTP_ACCEPT='application/json-p')
|
||||||
|
self.assertEquals(resp.status_code, 200)
|
||||||
|
self.assertEquals(resp['Content-Type'], 'application/json-p')
|
||||||
|
self.assertEquals(resp.content, 'callback(%s);' % _flat_repr)
|
||||||
|
|
||||||
|
def test_with_callback(self):
|
||||||
|
"""
|
||||||
|
Test JSONP rendering with callback function name.
|
||||||
|
"""
|
||||||
|
callback_func = 'myjsonpcallback'
|
||||||
|
resp = self.client.get('/jsonp/nojsonrenderer?callback=' + callback_func,
|
||||||
|
HTTP_ACCEPT='application/json-p')
|
||||||
|
self.assertEquals(resp.status_code, 200)
|
||||||
|
self.assertEquals(resp['Content-Type'], 'application/json-p')
|
||||||
|
self.assertEquals(resp.content, '%s(%s);' % (callback_func, _flat_repr))
|
||||||
|
|
||||||
|
|
||||||
|
if YAMLRenderer:
|
||||||
|
_yaml_repr = 'foo: [bar, baz]\n'
|
||||||
|
|
||||||
|
class YAMLRendererTests(TestCase):
|
||||||
|
"""
|
||||||
|
Tests specific to the JSON Renderer
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_render(self):
|
||||||
|
"""
|
||||||
|
Test basic YAML rendering.
|
||||||
|
"""
|
||||||
|
obj = {'foo': ['bar', 'baz']}
|
||||||
|
renderer = YAMLRenderer(None)
|
||||||
|
content = renderer.render(obj, 'application/yaml')
|
||||||
|
self.assertEquals(content, _yaml_repr)
|
||||||
|
|
||||||
|
def test_render_and_parse(self):
|
||||||
|
"""
|
||||||
|
Test rendering and then parsing returns the original object.
|
||||||
|
IE obj -> render -> parse -> obj.
|
||||||
|
"""
|
||||||
|
obj = {'foo': ['bar', 'baz']}
|
||||||
|
|
||||||
|
renderer = YAMLRenderer(None)
|
||||||
|
parser = YAMLParser(None)
|
||||||
|
|
||||||
|
content = renderer.render(obj, 'application/yaml')
|
||||||
|
(data, files) = parser.parse(StringIO(content))
|
||||||
|
self.assertEquals(obj, data)
|
||||||
|
|
||||||
|
|
||||||
|
class XMLRendererTestCase(TestCase):
|
||||||
|
"""
|
||||||
|
Tests specific to the XML Renderer
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_render_string(self):
|
||||||
|
"""
|
||||||
|
Test XML rendering.
|
||||||
|
"""
|
||||||
|
renderer = XMLRenderer(None)
|
||||||
|
content = renderer.render({'field': 'astring'}, 'application/xml')
|
||||||
|
self.assertXMLContains(content, '<field>astring</field>')
|
||||||
|
|
||||||
|
def test_render_integer(self):
|
||||||
|
"""
|
||||||
|
Test XML rendering.
|
||||||
|
"""
|
||||||
|
renderer = XMLRenderer(None)
|
||||||
|
content = renderer.render({'field': 111}, 'application/xml')
|
||||||
|
self.assertXMLContains(content, '<field>111</field>')
|
||||||
|
|
||||||
|
def test_render_datetime(self):
|
||||||
|
"""
|
||||||
|
Test XML rendering.
|
||||||
|
"""
|
||||||
|
renderer = XMLRenderer(None)
|
||||||
|
content = renderer.render({
|
||||||
|
'field': datetime.datetime(2011, 12, 25, 12, 45, 00)
|
||||||
|
}, 'application/xml')
|
||||||
|
self.assertXMLContains(content, '<field>2011-12-25 12:45:00</field>')
|
||||||
|
|
||||||
|
def test_render_float(self):
|
||||||
|
"""
|
||||||
|
Test XML rendering.
|
||||||
|
"""
|
||||||
|
renderer = XMLRenderer(None)
|
||||||
|
content = renderer.render({'field': 123.4}, 'application/xml')
|
||||||
|
self.assertXMLContains(content, '<field>123.4</field>')
|
||||||
|
|
||||||
|
def test_render_decimal(self):
|
||||||
|
"""
|
||||||
|
Test XML rendering.
|
||||||
|
"""
|
||||||
|
renderer = XMLRenderer(None)
|
||||||
|
content = renderer.render({'field': Decimal('111.2')}, 'application/xml')
|
||||||
|
self.assertXMLContains(content, '<field>111.2</field>')
|
||||||
|
|
||||||
|
def test_render_none(self):
|
||||||
|
"""
|
||||||
|
Test XML rendering.
|
||||||
|
"""
|
||||||
|
renderer = XMLRenderer(None)
|
||||||
|
content = renderer.render({'field': None}, 'application/xml')
|
||||||
|
self.assertXMLContains(content, '<field></field>')
|
||||||
|
|
||||||
|
def assertXMLContains(self, xml, string):
|
||||||
|
self.assertTrue(xml.startswith('<?xml version="1.0" encoding="utf-8"?>\n<root>'))
|
||||||
|
self.assertTrue(xml.endswith('</root>'))
|
||||||
|
self.assertTrue(string in xml, '%r not in %r' % (string, xml))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Issue122Tests(TestCase):
|
||||||
|
"""
|
||||||
|
Tests that covers #122.
|
||||||
|
"""
|
||||||
|
|
||||||
|
urls = 'djangorestframework.tests.renderers'
|
||||||
|
|
||||||
|
def test_only_html_renderer(self):
|
||||||
|
"""
|
||||||
|
Test if no recursion occurs.
|
||||||
|
"""
|
||||||
|
resp = self.client.get('/html')
|
||||||
|
|
||||||
|
def test_html_renderer_is_first(self):
|
||||||
|
"""
|
||||||
|
Test if no recursion occurs.
|
||||||
|
"""
|
||||||
|
resp = self.client.get('/html1')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user