diff options
author | Jonathan Liuti <liuti.john@gmail.com> | 2016-02-17 21:22:13 +0100 |
---|---|---|
committer | Jonathan Liuti <liuti.john@gmail.com> | 2016-02-17 21:22:13 +0100 |
commit | 3e7c17331bade835e14d3b9939fc7e8bd375f113 (patch) | |
tree | 089a93d00a8480f3e40291e5d4df5fdac2ea8455 /wkhtmltopdf | |
parent | 87be0aaafb59fca5540a300c8e6262fbe023fce5 (diff) | |
parent | 60f29e9b1a055c2f927fae56c15e48918d5e76fd (diff) | |
download | django-wkhtmltopdf-3e7c17331bade835e14d3b9939fc7e8bd375f113.tar.gz django-wkhtmltopdf-3e7c17331bade835e14d3b9939fc7e8bd375f113.tar.bz2 django-wkhtmltopdf-3e7c17331bade835e14d3b9939fc7e8bd375f113.zip |
Merge pull request #102 from ryankask/issue-101-decouple-template-engine
Remove dependency on Django template engine
Diffstat (limited to 'wkhtmltopdf')
-rw-r--r-- | wkhtmltopdf/tests/tests.py | 3 | ||||
-rw-r--r-- | wkhtmltopdf/utils.py | 30 | ||||
-rw-r--r-- | wkhtmltopdf/views.py | 1 |
3 files changed, 24 insertions, 10 deletions
diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py index c15d88f..8f40c5e 100644 --- a/wkhtmltopdf/tests/tests.py +++ b/wkhtmltopdf/tests/tests.py @@ -203,7 +203,8 @@ class TestViews(TestCase): self.assertTrue(response.has_header('Content-Disposition')) footer_template = loader.get_template(self.footer_template) - tempfile = render_to_temporary_file(footer_template, context=RequestContext(request, context)) + tempfile = render_to_temporary_file(footer_template, context=context, + request=request) tempfile.seek(0) footer_content = smart_str(tempfile.read()) footer_content = make_absolute_paths(footer_content) diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py index 3d125bb..6be961c 100644 --- a/wkhtmltopdf/utils.py +++ b/wkhtmltopdf/utils.py @@ -8,7 +8,6 @@ import sys import shlex from tempfile import NamedTemporaryFile -from django.template.context import Context, RequestContext from django.utils.encoding import smart_text try: @@ -18,7 +17,9 @@ except ImportError: # Python2 from urllib import pathname2url from urlparse import urljoin +import django from django.conf import settings +from django.template.context import Context, RequestContext from django.utils import six from .subprocess import check_output @@ -120,7 +121,7 @@ def convert_to_pdf(filename, header_filename=None, footer_filename=None, cmd_opt cmd_options['footer_html'] = footer_filename return wkhtmltopdf(pages=[filename], **cmd_options) -def render_pdf_from_template(input_template, header_template, footer_template, context, cmd_options=None): +def render_pdf_from_template(input_template, header_template, footer_template, context, request=None, cmd_options=None): debug = getattr(settings, 'WKHTMLTOPDF_DEBUG', settings.DEBUG) cmd_options = cmd_options if cmd_options else {} @@ -131,6 +132,7 @@ def render_pdf_from_template(input_template, header_template, footer_template, c input_file = render_to_temporary_file( template=input_template, context=context, + request=request, prefix='wkhtmltopdf', suffix='.html', delete=(not debug) ) @@ -139,6 +141,7 @@ def render_pdf_from_template(input_template, header_template, footer_template, c header_file = render_to_temporary_file( template=header_template, context=context, + request=request, prefix='wkhtmltopdf', suffix='.html', delete=(not debug) ) @@ -148,6 +151,7 @@ def render_pdf_from_template(input_template, header_template, footer_template, c footer_file = render_to_temporary_file( template=footer_template, context=context, + request=request, prefix='wkhtmltopdf', suffix='.html', delete=(not debug) ) @@ -231,14 +235,22 @@ def make_absolute_paths(content): return content -def render_to_temporary_file(template, context, mode='w+b', bufsize=-1, - suffix='.html', prefix='tmp', dir=None, - delete=True): - # make sure the context is a context object - if not isinstance(context, (Context, RequestContext)): - context = Context(context) +def render_to_temporary_file(template, context, request=None, mode='w+b', + bufsize=-1, suffix='.html', prefix='tmp', + dir=None, delete=True): + if django.VERSION < (1, 8): + # If using a version of Django prior to 1.8, ensure ``context`` is an + # instance of ``Context`` + if not isinstance(context, Context): + if request: + context = RequestContext(request, context) + else: + context = Context(context) + content = template.render(context) + else: + content = template.render(context, request) - content = smart_text(template.render(context)) + content = smart_text(content) content = make_absolute_paths(content) try: diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index f26c9ae..dff87b4 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -75,6 +75,7 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): self.resolve_template(self.header_template), self.resolve_template(self.footer_template), context=self.resolve_context(self.context_data), + request=self._request, cmd_options=cmd_options ) |