From 3e5f6553c8e51e8304004419e48fd169f84ca91e Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Mon, 1 Sep 2014 10:25:11 -0300 Subject: Add failing test of unicode content. --- wkhtmltopdf/tests/tests.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py index f296c8e..0c332f6 100644 --- a/wkhtmltopdf/tests/tests.py +++ b/wkhtmltopdf/tests/tests.py @@ -59,6 +59,17 @@ class TestUtils(TestCase): finally: temp_file.close() + def test_wkhtmltopdf_with_unicode_content(self): + """A wkhtmltopdf call should render unicode content properly""" + title = u'♥' + response = PDFTemplateResponse(self.factory.get('/'), None, context={'title': title}) + temp_file = response.render_to_temporary_file('unicode.html') + try: + pdf_output = wkhtmltopdf(pages=[temp_file.name]) + self.assertTrue(pdf_output.startswith(b'%PDF'), pdf_output) + finally: + temp_file.close() + def test_PDFTemplateResponse_render_to_temporary_file(self): """Should render a template to a temporary file.""" title = 'A test template.' -- cgit v1.2.3 From b6d9a18528e79673412714fb7c6d3e9826e89421 Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Mon, 1 Sep 2014 10:49:42 -0300 Subject: Makes unicode content views testing fails too. The view didn't define any title in context and the templates itself doen't have unicode content. I forced it making a view with a unicode title in context. --- wkhtmltopdf/tests/tests.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py index 0c332f6..e3aa869 100644 --- a/wkhtmltopdf/tests/tests.py +++ b/wkhtmltopdf/tests/tests.py @@ -17,6 +17,18 @@ from wkhtmltopdf.utils import (_options_to_args, make_absolute_paths, from wkhtmltopdf.views import PDFResponse, PDFTemplateView, PDFTemplateResponse +class UnicodeContentPDFTemplateView(PDFTemplateView): + """ + PDFTemplateView with the addition of unicode content in his context. + + Used in unicode content view testing. + """ + def get_context_data(self, **kwargs): + Base = super(UnicodeContentPDFTemplateView, self) + context = Base.get_context_data(**kwargs) + context['title'] = u'♥' + return context + class TestUtils(TestCase): def setUp(self): # Clear standard error @@ -244,11 +256,12 @@ class TestViews(TestCase): self.test_pdf_template_view(show_content=True) def test_pdf_template_view_unicode(self, show_content=False): - """Test PDFTemplateView.""" - - view = PDFTemplateView.as_view(filename=self.pdf_filename, - show_content_in_browser=show_content, - template_name=self.template) + """Test PDFTemplateView with unicode content.""" + view = UnicodeContentPDFTemplateView.as_view( + filename=self.pdf_filename, + show_content_in_browser=show_content, + template_name=self.template + ) # As PDF request = RequestFactory().get('/') -- cgit v1.2.3 From 8e1715bdbcc504a9ea815e401c064a6759077a2d Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Mon, 1 Sep 2014 10:51:14 -0300 Subject: Replace smart_str with smart_text/unicode for py2.x compat. --- wkhtmltopdf/views.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 2d91224..882f4fa 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -5,8 +5,11 @@ from tempfile import NamedTemporaryFile from django.conf import settings from django.http import HttpResponse from django.template.response import TemplateResponse -from django.utils.encoding import smart_str from django.views.generic import TemplateView +try: + from django.utils.encoding import smart_text +except ImportError: + from django.utils.encoding import smart_unicode as smart_text from .utils import (content_disposition_filename, make_absolute_paths, wkhtmltopdf) @@ -72,7 +75,7 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): context = self.resolve_context(self.context_data) - content = smart_str(template.render(context)) + content = smart_text(template.render(context)) content = make_absolute_paths(content) try: -- cgit v1.2.3 From 04530dcce4489c720cf02cdfb906df0cfe6f0fed Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Mon, 1 Sep 2014 11:19:20 -0300 Subject: Makes tests.py lines 80 columns max. --- wkhtmltopdf/tests/tests.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py index e3aa869..48a8b19 100644 --- a/wkhtmltopdf/tests/tests.py +++ b/wkhtmltopdf/tests/tests.py @@ -50,7 +50,9 @@ class TestUtils(TestCase): def test_wkhtmltopdf(self): """Should run wkhtmltopdf to generate a PDF""" title = 'A test template.' - response = PDFTemplateResponse(self.factory.get('/'), None, context={'title': title}) + response = PDFTemplateResponse(self.factory.get('/'), + None, + context={'title': title}) temp_file = response.render_to_temporary_file('sample.html') try: # Standard call @@ -74,7 +76,9 @@ class TestUtils(TestCase): def test_wkhtmltopdf_with_unicode_content(self): """A wkhtmltopdf call should render unicode content properly""" title = u'♥' - response = PDFTemplateResponse(self.factory.get('/'), None, context={'title': title}) + response = PDFTemplateResponse(self.factory.get('/'), + None, + context={'title': title}) temp_file = response.render_to_temporary_file('unicode.html') try: pdf_output = wkhtmltopdf(pages=[temp_file.name]) @@ -85,7 +89,9 @@ class TestUtils(TestCase): def test_PDFTemplateResponse_render_to_temporary_file(self): """Should render a template to a temporary file.""" title = 'A test template.' - response = PDFTemplateResponse(self.factory.get('/'), None, context={'title': title}) + response = PDFTemplateResponse(self.factory.get('/'), + None, + context={'title': title}) temp_file = response.render_to_temporary_file('sample.html') temp_file.seek(0) saved_content = smart_str(temp_file.read()) @@ -101,7 +107,7 @@ class TestViews(TestCase): inline_fileheader = 'inline; filename="{0}"' def test_pdf_response(self): - """Should generate the correct HttpResponse object and content type.""" + """Should generate correct HttpResponse object and content type.""" # 404 response = PDFResponse(content='', status=404) self.assertEqual(response.status_code, 404) -- cgit v1.2.3 From ec709959f0a2827589bc8ff532136a8e09f40acd Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Mon, 1 Sep 2014 12:02:37 -0300 Subject: Being more explicit about the reason for smart_unicode. --- wkhtmltopdf/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 882f4fa..949ae38 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -9,6 +9,7 @@ from django.views.generic import TemplateView try: from django.utils.encoding import smart_text except ImportError: + # Django 1.4 doesn't have smart_text, we must smart_unicode in place from django.utils.encoding import smart_unicode as smart_text from .utils import (content_disposition_filename, make_absolute_paths, -- cgit v1.2.3 From 08b50680c71b903634f7137a596ac5c0fe21562f Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Mon, 1 Sep 2014 12:10:55 -0300 Subject: Rely only smart_text once django 1.4 also supports it. As well pointed by @mattack108, django 1.4 also haves smart_text. We don't have to do that import. --- wkhtmltopdf/views.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 949ae38..d5f189a 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -6,11 +6,7 @@ from django.conf import settings from django.http import HttpResponse from django.template.response import TemplateResponse from django.views.generic import TemplateView -try: - from django.utils.encoding import smart_text -except ImportError: - # Django 1.4 doesn't have smart_text, we must smart_unicode in place - from django.utils.encoding import smart_unicode as smart_text +from django.utils.encoding import smart_text from .utils import (content_disposition_filename, make_absolute_paths, wkhtmltopdf) -- cgit v1.2.3