aboutsummaryrefslogtreecommitdiffstats
path: root/wkhtmltopdf
diff options
context:
space:
mode:
authormattack108 <matt.lenc@gmail.com>2013-01-15 15:48:13 +0000
committermattack108 <matt.lenc@gmail.com>2013-01-15 15:48:13 +0000
commit813537e4adb811f10011349ce06bee0de0775dc6 (patch)
treeff4d890ac7b43cbb9c7ac544efae702f6c39dfd9 /wkhtmltopdf
parent8410054212002609dd1521152bcb80fda9fade2e (diff)
downloaddjango-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')
-rw-r--r--wkhtmltopdf/utils.py35
-rw-r--r--wkhtmltopdf/views.py41
2 files changed, 39 insertions, 37 deletions
diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py
index 63310c3..e5bd102 100644
--- a/wkhtmltopdf/utils.py
+++ b/wkhtmltopdf/utils.py
@@ -4,6 +4,7 @@ from copy import copy
from functools import wraps
from itertools import chain
import os
+import re
import sys
import urllib
from urlparse import urljoin
@@ -121,3 +122,37 @@ def http_quote(string):
def pathname2fileurl(pathname):
"""Returns a file:// URL for pathname. Handles OS-specific conversions."""
return urljoin('file:', urllib.pathname2url(pathname))
+
+
+def make_absolute_paths(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
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."""