Skip to content

Instantly share code, notes, and snippets.

@smcoll
Created February 17, 2012 00:45
Show Gist options
  • Save smcoll/1849267 to your computer and use it in GitHub Desktop.
Save smcoll/1849267 to your computer and use it in GitHub Desktop.
more flexible DocumentingTemplateRenderer
class DocumentingTemplateRenderer:
...
escape_binary = False
def _escape_binary(self, content):
if self.escape_binary and not all(char in string.printable for char in content):
return '[%d bytes of binary content]' % len(content)
return content
def _get_content(self, view, request, obj, media_type):
renderers = [renderer for renderer in view.renderers if not issubclass(renderer, DocumentingTemplateRenderer)]
if not renderers:
return '[No renderers were found]'
media_type = add_media_type_param(media_type, 'indent', '4')
content = renderers[0](view).render(obj, media_type)
return self._escape_binary(content)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment