From ea778c6f7b3df11b4fbb106f64cd8f64da99270e Mon Sep 17 00:00:00 2001 From: Charlie Denton Date: Thu, 23 Feb 2012 16:47:01 +0000 Subject: Deprecate baDly cApitalised Views. --- wkhtmltopdf/views.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'wkhtmltopdf') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 0c1ec4c..1d6e38e 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -8,13 +8,21 @@ from django.views.generic import TemplateView from wkhtmltopdf.utils import template_to_temp_file, wkhtmltopdf -class PdfResponse(HttpResponse): + +class PDFResponse(HttpResponse): def __init__(self, content, filename): super(PdfResponse, self).__init__(content, 'application/pdf') self.__setitem__('Content-Disposition', 'attachment; filename=%s' % filename) -class PdfTemplateView(TemplateView): +class PdfResponse(PDFResponse): + def __init__(self, content, filename): + warning = '''PdfTemplateView is deprecated in favour of PDFTemplateView. It will be removed in version 1.''' + raise PendingDeprecationWarning(warning) + super(PdfResponse, self).__init__(content, filename) + + +class PDFTemplateView(TemplateView): filename = 'rendered_pdf.pdf' footer_template = None header_template = None @@ -59,3 +67,9 @@ class PdfTemplateView(TemplateView): return context + +class PdfTemplateView(PDFTemplateView): #TODO: Remove this in v1.0 + def as_view(cls, **initkwargs): + warning = '''PdfTemplateView is deprecated in favour of PDFTemplateView. It will be removed in version 1.''' + raise PendingDeprecationWarning(warning) + return super(PdfTemplateView, cls).as_view(**initkwargs) \ No newline at end of file -- cgit v1.2.3 From 8a531833baa9ca69d5b15ba82b3f4d0b330b2aa5 Mon Sep 17 00:00:00 2001 From: Charlie Denton Date: Thu, 23 Feb 2012 17:35:21 +0000 Subject: Make PDFTemplateView use PDFResponse. --- wkhtmltopdf/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wkhtmltopdf') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 1d6e38e..c01a579 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -30,7 +30,7 @@ class PDFTemplateView(TemplateView): margin_left = 0 margin_right = 0 margin_top = 0 - response = PdfResponse + response = PDFResponse def get(self, request, context_instance=None, *args, **kwargs): if request.GET.get('as', '') == 'html': -- cgit v1.2.3 From 3581893df1f945d77c74cfb997a6be3d2d0f0831 Mon Sep 17 00:00:00 2001 From: Charlie Denton Date: Thu, 23 Feb 2012 17:37:56 +0000 Subject: Return super when called. --- wkhtmltopdf/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wkhtmltopdf') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index c01a579..7d9e844 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -34,7 +34,7 @@ class PDFTemplateView(TemplateView): def get(self, request, context_instance=None, *args, **kwargs): if request.GET.get('as', '') == 'html': - super(PdfTemplateView, self).get(request, *args, **kwargs) + return super(PdfTemplateView, self).get(request, *args, **kwargs) page_path = template_to_temp_file(self.template_name, self.get_context_data(), context_instance) -- cgit v1.2.3 From e625b44977915a8bf193f5fc974fba86758b7926 Mon Sep 17 00:00:00 2001 From: Charlie Denton Date: Thu, 23 Feb 2012 17:48:04 +0000 Subject: Made PDFTemplateView a little more extensible, and added orientation. --- wkhtmltopdf/views.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'wkhtmltopdf') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 7d9e844..53f11bd 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -26,6 +26,7 @@ class PDFTemplateView(TemplateView): filename = 'rendered_pdf.pdf' footer_template = None header_template = None + orientation = 'portrait' margin_bottom = 0 margin_left = 0 margin_right = 0 @@ -36,25 +37,30 @@ class PDFTemplateView(TemplateView): if request.GET.get('as', '') == 'html': return super(PdfTemplateView, self).get(request, *args, **kwargs) - page_path = template_to_temp_file(self.template_name, self.get_context_data(), context_instance) + self.context_instance = context_instance + page_path = template_to_temp_file(self.template_name, self.get_context_data(), self.context_instance) + + pdf_kwargs = self.get_pdf_kwargs() + return self.response(wkhtmltopdf(page_path, **pdf_kwargs), self.filename) + + def get_pdf_kwargs(self): + kwargs = { + 'margin_bottom': self.margin_bottom, + 'margin_left': self.margin_left, + 'margin_right': self.margin_right, + 'margin_top': self.margin_top, + 'orientation': self.self.orientation, + } tmp_files = [] if self.header_template: - kwargs['header_html'] = template_to_temp_file(self.header_template, self.get_context_data(), context_instance) + kwargs['header_html'] = template_to_temp_file(self.header_template, self.get_context_data(), self.context_instance) tmp_files.append(kwargs['header_html']) if self.footer_template: - kwargs['footer_html'] = template_to_temp_file(self.footer_template, self.get_context_data(), context_instance) + kwargs['footer_html'] = template_to_temp_file(self.footer_template, self.get_context_data(), self.context_instance) tmp_files.append(kwargs['footer_html']) - map(remove, tmp_files) - - kwargs.update({ - 'margin_bottom': self.margin_bottom, - 'margin_left': self.margin_left, - 'margin_right': self.margin_right, - 'margin_top': self.margin_top - }) - return self.response(wkhtmltopdf(page_path, **kwargs), self.filename) + return kwargs def get_context_data(self, **kwargs): context = super(PdfTemplateView, self).get_context_data(**kwargs) -- cgit v1.2.3 From 923c044e5d77e841a694ded1b9940ea6a9620596 Mon Sep 17 00:00:00 2001 From: Charlie Denton Date: Thu, 23 Feb 2012 17:53:36 +0000 Subject: Add get_filename method. --- wkhtmltopdf/views.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'wkhtmltopdf') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 53f11bd..561faf0 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -42,7 +42,10 @@ class PDFTemplateView(TemplateView): page_path = template_to_temp_file(self.template_name, self.get_context_data(), self.context_instance) pdf_kwargs = self.get_pdf_kwargs() - return self.response(wkhtmltopdf(page_path, **pdf_kwargs), self.filename) + return self.response(wkhtmltopdf(page_path, **pdf_kwargs), self.get_filename()) + + def get_filename(self): + return self.filename def get_pdf_kwargs(self): kwargs = { -- cgit v1.2.3 From bb3e0dbbf1cc60d5413acbf4b1eacc691cffc919 Mon Sep 17 00:00:00 2001 From: Charlie Denton Date: Fri, 24 Feb 2012 09:15:52 +0000 Subject: Fix a couple of daft mistakes. --- wkhtmltopdf/views.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'wkhtmltopdf') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 561faf0..c92b117 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -11,13 +11,13 @@ from wkhtmltopdf.utils import template_to_temp_file, wkhtmltopdf class PDFResponse(HttpResponse): def __init__(self, content, filename): - super(PdfResponse, self).__init__(content, 'application/pdf') + super(PDFResponse, self).__init__(content, 'application/pdf') self.__setitem__('Content-Disposition', 'attachment; filename=%s' % filename) class PdfResponse(PDFResponse): def __init__(self, content, filename): - warning = '''PdfTemplateView is deprecated in favour of PDFTemplateView. It will be removed in version 1.''' + warning = '''PdfResponse is deprecated in favour of PDFResponse. It will be removed in version 1.''' raise PendingDeprecationWarning(warning) super(PdfResponse, self).__init__(content, filename) @@ -33,10 +33,11 @@ class PDFTemplateView(TemplateView): margin_top = 0 response = PDFResponse - def get(self, request, context_instance=None, *args, **kwargs): + def get(self, request, context_instance=None, **kwargs): if request.GET.get('as', '') == 'html': - return super(PdfTemplateView, self).get(request, *args, **kwargs) + return super(PDFTemplateView, self).get(request, **kwargs) + # Save arguments onto class (would have been done in super-class) self.context_instance = context_instance page_path = template_to_temp_file(self.template_name, self.get_context_data(), self.context_instance) @@ -53,7 +54,7 @@ class PDFTemplateView(TemplateView): 'margin_left': self.margin_left, 'margin_right': self.margin_right, 'margin_top': self.margin_top, - 'orientation': self.self.orientation, + 'orientation': self.orientation, } tmp_files = [] if self.header_template: @@ -66,7 +67,7 @@ class PDFTemplateView(TemplateView): return kwargs def get_context_data(self, **kwargs): - context = super(PdfTemplateView, self).get_context_data(**kwargs) + context = super(PDFTemplateView, self).get_context_data(**kwargs) match_full_url = compile(r'^https?://') if not match_full_url.match(settings.STATIC_URL): -- cgit v1.2.3 From 1bf53ebfee2c911ed3aeca602c4e69ed6fb3bdce Mon Sep 17 00:00:00 2001 From: Charlie Denton Date: Fri, 24 Feb 2012 09:49:19 +0000 Subject: Restore *args to PDFTemplateView. --- wkhtmltopdf/views.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'wkhtmltopdf') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index c92b117..c26d1fc 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -33,15 +33,13 @@ class PDFTemplateView(TemplateView): margin_top = 0 response = PDFResponse - def get(self, request, context_instance=None, **kwargs): + def get(self, request, context_instance=None, *args, **kwargs): if request.GET.get('as', '') == 'html': - return super(PDFTemplateView, self).get(request, **kwargs) + return super(PDFTemplateView, self).get(request, *args, **kwargs) - # Save arguments onto class (would have been done in super-class) self.context_instance = context_instance page_path = template_to_temp_file(self.template_name, self.get_context_data(), self.context_instance) - pdf_kwargs = self.get_pdf_kwargs() return self.response(wkhtmltopdf(page_path, **pdf_kwargs), self.get_filename()) -- cgit v1.2.3