>
If you think these issues warrant a fork and pull request, let me know.
Yes. Absolutely they do.
I've looked into it, and it's slightly complicated, tho, there's several different things that ought to happen.
I think there's up to four seperate pull reqs needed here:
Firstly:
The escaping in DocumentingTemplateRenderer should definitely be refactored into an `escape_binary` method.
That'll let it be overridden as needed. I don't think it needs a separate boolean attribute, just refactoring out the method would be fine.
Secondly:
We need to differentiate between binary and string content in a more intelligent way.
Renderers that return string content should *always* return unicode.
Renderers that return binary content should return bytes. (Or str, which is the same thing in Python 2.x)
I think the template render will already do that. The json, jsonp, and yaml don't. I'm not sure about the xml, I don't think it does.
So...
Fix json: Add `ensure_ascii=False` in `dumps()`, wrap the entire result of dumps() up in 'unicode(dumps(...))'
Fix jsonp: Add 'u' prefix on the returned string, and fix json as above.
Fix yaml: Add encoding=None in `safe_dump()`.
Fix xml: Not totally sure.
Thirdly:
After that we can fix up the escape_binary test, so that it does not escape if `isinstance(obj, unicode)`, and it does otherwise.
Finally:
When we render the response, we ought to be setting the charset if it's a unicode object (and not otherwise).
Something like:
def get_content_type(content, mimetype):
if isinstance(content, unicode):
return "%s; %s" % (response.media_type, settings.DEFAULT_CHARSET)
else:
return response.media_type
And generating the final response with:
content_type = self.get_content_type(content, response.media_type)
HttpResponse(content, status=response.status, content_type=content_type)
I would love a set of pull requests addressing those! (Or a single pull req I guess)