diff options
author | mattack108 <matt.lenc@gmail.com> | 2013-01-15 15:48:13 +0000 |
---|---|---|
committer | mattack108 <matt.lenc@gmail.com> | 2013-01-15 15:48:13 +0000 |
commit | 813537e4adb811f10011349ce06bee0de0775dc6 (patch) | |
tree | ff4d890ac7b43cbb9c7ac544efae702f6c39dfd9 /wkhtmltopdf/views.py | |
parent | 8410054212002609dd1521152bcb80fda9fade2e (diff) | |
download | django-wkhtmltopdf-813537e4adb811f10011349ce06bee0de0775dc6.tar.gz django-wkhtmltopdf-813537e4adb811f10011349ce06bee0de0775dc6.tar.bz2 django-wkhtmltopdf-813537e4adb811f10011349ce06bee0de0775dc6.zip |
Move make_absolute_paths to utils so it can be used in tests
Diffstat (limited to 'wkhtmltopdf/views.py')
-rw-r--r-- | wkhtmltopdf/views.py | 41 |
1 files changed, 4 insertions, 37 deletions
diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index fd68c0a..5a46490 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -1,7 +1,6 @@ from __future__ import absolute_import from tempfile import NamedTemporaryFile -import re from django.conf import settings from django.http import HttpResponse @@ -9,7 +8,8 @@ from django.template.response import TemplateResponse from django.utils.encoding import smart_str from django.views.generic import TemplateView -from .utils import content_disposition_filename, pathname2fileurl, wkhtmltopdf +from .utils import (content_disposition_filename, make_absolute_paths, + wkhtmltopdf) class PDFResponse(HttpResponse): @@ -73,9 +73,9 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): template = self.resolve_template(template_name) context = self.resolve_context(self.context_data) - content = smart_str(template.render(context)) - content = self.make_absolute_paths(content) + content = smart_str(template.render(context)) + content = make_absolute_paths(content) tempfile = NamedTemporaryFile(mode=mode, bufsize=bufsize, suffix=suffix, prefix=prefix, @@ -147,39 +147,6 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): for f in filter(None, (input_file, header_file, footer_file)): f.close() - def make_absolute_paths(self, content): - """Convert all MEDIA files into a file://URL paths in order to - correctly get it displayed in PDFs.""" - - overrides = [ - { - 'root': settings.MEDIA_ROOT, - 'url': settings.MEDIA_URL, - }, - { - 'root': settings.STATIC_ROOT, - 'url': settings.STATIC_URL, - } - ] - has_scheme = re.compile(r'^[^:/]+://') - - for x in overrides: - if has_scheme.match(x['url']): - continue - - if not x['root'].endswith('/'): - x['root'] += '/' - - occur_pattern = '''["|']({0}.*?)["|']''' - occurences = re.findall(occur_pattern.format(x['url']), content) - occurences = list(set(occurences)) # Remove dups - for occur in occurences: - content = content.replace(occur, - pathname2fileurl(x['root']) + - occur[len(x['url']):]) - - return content - class PDFTemplateView(TemplateView): """Class-based view for HTML templates rendered to PDF.""" |