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/views.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/views.py')
-rw-r--r-- | wkhtmltopdf/views.py | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 8da86f5..539a669 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import + import os from re import compile import warnings @@ -8,23 +10,27 @@ from django.template.context import RequestContext from django.template.response import HttpResponse from django.views.generic import TemplateView -from wkhtmltopdf.utils import template_to_temp_file, wkhtmltopdf +from .utils import (content_disposition_filename, + template_to_temp_file, wkhtmltopdf) class PDFResponse(HttpResponse): - def __init__(self, content, *args, **kwargs): + def __init__(self, content, mimetype=None, status=200, + content_type='application/pdf', *args, **kwargs): filename = kwargs.pop('filename', None) - super(PDFResponse, self).__init__(content, 'application/pdf', *args, **kwargs) + super(PDFResponse, self).__init__(content, mimetype, status, + content_type, *args, **kwargs) if filename: + filename = content_disposition_filename(filename) header_content = 'attachment; filename={0}'.format(filename) - self.__setitem__('Content-Disposition', header_content) + self['Content-Disposition'] = header_content class PdfResponse(PDFResponse): def __init__(self, content, filename): warnings.warn('PdfResponse is deprecated in favour of PDFResponse. It will be removed in version 1.', PendingDeprecationWarning, 2) - super(PdfResponse, self).__init__(content, filename) + super(PdfResponse, self).__init__(content, filename=filename) class PDFTemplateView(TemplateView): |