diff options
author | Simon Law <simon.law@ecometrica.com> | 2012-07-23 15:29:39 -0400 |
---|---|---|
committer | Simon Law <simon.law@ecometrica.com> | 2012-07-23 15:29:39 -0400 |
commit | 4a27fe9f16ecd4bc6f94ad89046767450316490c (patch) | |
tree | 659a75357afd37449685adbd70657b3a21530755 /wkhtmltopdf/utils.py | |
parent | 8c5b30925654309da9e79bf8bb65e010068bd903 (diff) | |
download | django-wkhtmltopdf-4a27fe9f16ecd4bc6f94ad89046767450316490c.tar.gz django-wkhtmltopdf-4a27fe9f16ecd4bc6f94ad89046767450316490c.tar.bz2 django-wkhtmltopdf-4a27fe9f16ecd4bc6f94ad89046767450316490c.zip |
PDFResponse is more robust:
* Now matches HttpResponse in function signature.
* Modern Django content_type/mimetype handling.
* Sanitizes and quotes filenames in Content-Disposition header.
* Tests.
Diffstat (limited to 'wkhtmltopdf/utils.py')
-rw-r--r-- | wkhtmltopdf/utils.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py index 7fe432a..fb8f0aa 100644 --- a/wkhtmltopdf/utils.py +++ b/wkhtmltopdf/utils.py @@ -81,3 +81,33 @@ def template_to_temp_file(template_name, dictionary=None, context_instance=None) f.write(smart_str(loader.render_to_string(template_name, dictionary=dictionary, context_instance=context_instance))) return tempfile_path + +def content_disposition_filename(filename): + """ + Sanitize a file name to be used in the Content-Disposition HTTP + header. + + Even if the standard is quite permissive in terms of + characters, there are a lot of edge cases that are not supported by + different browsers. + + See http://greenbytes.de/tech/tc2231/#attmultinstances for more details. + """ + filename = filename.replace(';', '').replace('"', '') + return http_quote(filename) + + +def http_quote(string): + """ + Given a unicode string, will do its dandiest to give you back a + valid ascii charset string you can use in, say, http headers and the + like. + """ + if isinstance(string, unicode): + try: + import unidecode + string = unidecode.unidecode(string) + except ImportError: + string = string.encode('ascii', 'replace') + # Wrap in double-quotes for ; , and the like + return '"{!s}"'.format(string.replace('\\', '\\\\').replace('"', '\\"')) |