aboutsummaryrefslogtreecommitdiffstats
path: root/wkhtmltopdf/tests.py
diff options
context:
space:
mode:
authorSimon Law <simon.law@ecometrica.com>2012-07-25 12:51:26 -0400
committerSimon Law <simon.law@ecometrica.com>2012-07-25 12:51:26 -0400
commit5a1847309e7fa431c98565805d88a21a40d01406 (patch)
tree9aa1b5ac50efd0bd39f1a38130ec9d56e4a59302 /wkhtmltopdf/tests.py
parenta6f0a53702a940bf055dadb3bf558aea49c6d862 (diff)
downloaddjango-wkhtmltopdf-5a1847309e7fa431c98565805d88a21a40d01406.tar.gz
django-wkhtmltopdf-5a1847309e7fa431c98565805d88a21a40d01406.tar.bz2
django-wkhtmltopdf-5a1847309e7fa431c98565805d88a21a40d01406.zip
MEDIA_URL and STATIC_URL overrides PDFTemplateResponse.get_override_settings()
MEDIA_URL and STATIC_URL used to be set only in get_context_data(), but there are apps such as staticfiles and Django Compressor where this won't work well. Instead, they need to be overridden at the settings level, not at the context level. This allows template context processors to populate a RequestContext with the right values. In addition, MEDIA_URL and STATIC_URL are now overridden as file:// URLs, based on MEDIA_ROOT and STATIC_ROOT. This allows developers to access these views in runserver, against their current codebase. It also means faster access for wkhtmltopdf, since the files are stored locally.
Diffstat (limited to 'wkhtmltopdf/tests.py')
-rw-r--r--wkhtmltopdf/tests.py52
1 files changed, 46 insertions, 6 deletions
diff --git a/wkhtmltopdf/tests.py b/wkhtmltopdf/tests.py
index 1802440..764c7f2 100644
--- a/wkhtmltopdf/tests.py
+++ b/wkhtmltopdf/tests.py
@@ -117,7 +117,13 @@ class TestViews(TestCase):
with override_settings(
MEDIA_URL='/media/',
+ MEDIA_ROOT='/tmp/media',
STATIC_URL='/static/',
+ STATIC_ROOT='/tmp/static',
+ TEMPLATE_CONTEXT_PROCESSORS=[
+ 'django.core.context_processors.media',
+ 'django.core.context_processors.static',
+ ],
TEMPLATE_LOADERS=['django.template.loaders.filesystem.Loader'],
TEMPLATE_DIRS=[os.path.join(os.path.dirname(__file__),
'_testproject', 'templates')],
@@ -151,7 +157,7 @@ class TestViews(TestCase):
self.assertTrue(pdf_content.startswith('%PDF-'))
self.assertTrue(pdf_content.endswith('%%EOF\n'))
- # Header
+ # Footer
filename = 'output.pdf'
footer_template = 'footer.html'
cmd_options = {'title': 'Test PDF'}
@@ -170,20 +176,53 @@ class TestViews(TestCase):
tempfile = response.render_to_temporary_file(footer_template)
tempfile.seek(0)
footer_content = tempfile.read()
- self.assertTrue('MEDIA_URL = {}'.format(settings.MEDIA_URL)
- in footer_content)
- self.assertTrue('STATIC_URL = {}'.format(settings.STATIC_URL)
- in footer_content)
+
+ media_url = 'MEDIA_URL = file://{}/'.format(settings.MEDIA_ROOT)
+ self.assertTrue(
+ media_url in footer_content,
+ "{!r} not in {!r}".format(media_url, footer_content)
+ )
+
+ static_url = 'STATIC_URL = file://{}/'.format(settings.STATIC_ROOT)
+ self.assertTrue(
+ static_url in footer_content,
+ "{!r} not in {!r}".format(static_url, footer_content)
+ )
pdf_content = response.rendered_content
self.assertTrue('\0'.join('{title}'.format(**cmd_options))
in pdf_content)
+ # Override settings
+ response = PDFTemplateResponse(request=request,
+ template=template,
+ context=context,
+ filename=filename,
+ footer_template=footer_template,
+ cmd_options=cmd_options,
+ override_settings={
+ 'STATIC_URL': 'file:///tmp/s/'
+ })
+ tempfile = response.render_to_temporary_file(footer_template)
+ tempfile.seek(0)
+ footer_content = tempfile.read()
+
+ static_url = 'STATIC_URL = {}'.format('file:///tmp/s/')
+ self.assertTrue(
+ static_url in footer_content,
+ "{!r} not in {!r}".format(static_url, footer_content)
+ )
+ self.assertEqual(settings.STATIC_URL, '/static/')
+
def test_pdf_template_view(self):
"""Test PDFTemplateView."""
with override_settings(
MEDIA_URL='/media/',
STATIC_URL='/static/',
+ TEMPLATE_CONTEXT_PROCESSORS=[
+ 'django.core.context_processors.media',
+ 'django.core.context_processors.static',
+ ],
TEMPLATE_LOADERS=['django.template.loaders.filesystem.Loader'],
TEMPLATE_DIRS=[os.path.join(os.path.dirname(__file__),
'_testproject', 'templates')],
@@ -193,7 +232,8 @@ class TestViews(TestCase):
template = 'sample.html'
filename = 'output.pdf'
view = PDFTemplateView.as_view(filename=filename,
- template_name=template)
+ template_name=template,
+ footer_template='footer.html')
# As PDF
request = RequestFactory().get('/')