Skip to content

fix templates without context_processor #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions wkhtmltopdf/tests/templates/context.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
<h1>{{ debug }}</h1>
</body>
</html>
59 changes: 59 additions & 0 deletions wkhtmltopdf/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.conf import settings
from django.template import loader, RequestContext
from django.test import TestCase
from django.test.utils import override_settings
from django.test.client import RequestFactory
from django.utils import six
from django.utils.encoding import smart_str
Expand Down Expand Up @@ -159,6 +160,7 @@ def test_render_with_null_request(self):

class TestViews(TestCase):
template = 'sample.html'
context_template = 'context.html'
footer_template = 'footer.html'
pdf_filename = 'output.pdf'
attached_fileheader = 'attachment; filename="{0}"'
Expand Down Expand Up @@ -379,3 +381,60 @@ def test_get_cmd_options(self):
view.cmd_options.update(cmd_options)
self.assertEqual(view.cmd_options, cmd_options)
self.assertEqual(PDFTemplateView.cmd_options, {})

def _render_file(self, template, context, request=None):
"""Helper method for testing rendered file deleted/persists tests."""
render = RenderedFile(template=template, context=context, request=request)
render.temporary_file.seek(0)
saved_content = smart_str(render.temporary_file.read())

return (saved_content, render.filename)

@override_settings(
DEBUG=True,
INTERNAL_IPS=['127.0.0.1'],
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
],
},
},
]
)
def test_get_context_processor_variables_debug(self, show_content=False):
request = RequestFactory().get('/')
template = loader.get_template(self.context_template)

saved_content, filename = self._render_file(template=template, context={}, request=request)
self.assertTrue('<h1>True</h1>' in saved_content)

with override_settings(DEBUG=False):
request = RequestFactory().get('/')
template = loader.get_template(self.context_template)

saved_content, filename = self._render_file(template=template, context={}, request=request)
self.assertTrue('<h1></h1>' in saved_content)

view = PDFTemplateView.as_view(filename=self.pdf_filename,
show_content_in_browser=show_content,
template_name=self.context_template,
footer_template=self.footer_template)
# As HTML
request = RequestFactory().get('/?as=html')
response = view(request)
self.assertEqual(response.status_code, 200)
response.render()
self.assertIn(b'<h1>True</h1>', response.content)
with override_settings(DEBUG=False):
request = RequestFactory().get('/?as=html')
response = view(request)
self.assertEqual(response.status_code, 200)
response.render()
self.assertIn(b'<h1></h1>', response.content)

def test_get_context_processor_variables_debug_show_content(self):
self.test_get_context_processor_variables_debug(show_content=True)
23 changes: 12 additions & 11 deletions wkhtmltopdf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,21 @@ def make_absolute_paths(content):
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)
# Handle error when ``request`` is None
try:
content = template.render(context)
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)
# Handle error when ``request`` is None
content = template.render(context)
else:
content = template.render(context, request)
except AttributeError:
content = loader.render_to_string(template, context)

content = smart_text(content)
content = make_absolute_paths(content)

Expand Down