Skip to content

make_absolute_paths don't depend anymore on STATIC_URL #126

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion wkhtmltopdf/tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
MEDIA_URL='/media/',
STATIC_ROOT=os.path.join(DIRNAME, 'static'),
STATIC_URL='/static/',
TEMPLATES = [ # For Django >= 1.10. Ignored in lower versions
TEMPLATES=[ # For Django >= 1.10. Ignored in lower versions
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
Expand Down
5 changes: 3 additions & 2 deletions wkhtmltopdf/tests/templates/footer.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load static %}
<script src="{% static 'sample_js_not_existing.js' %}"></script>

<img src="{% get_media_prefix %}sample_image_not_existing.png" />
<script src="{% static 'js/sample_js_file.js' %}"></script>

<img src="{% get_media_prefix %}sample_image_file.png" />
33 changes: 17 additions & 16 deletions wkhtmltopdf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import django
from django.conf import settings
from django.contrib.staticfiles import finders
from django.template import loader
from django.template.context import Context, RequestContext
from django.utils import six
Expand Down Expand Up @@ -141,6 +142,7 @@ def wkhtmltopdf(pages, output=None, **kwargs):

return check_output(ck_args, **ck_kwargs)


def convert_to_pdf(filename, header_filename=None, footer_filename=None, cmd_options=None):
# Clobber header_html and footer_html only if filenames are
# provided. These keys may be in self.cmd_options as hardcoded
Expand All @@ -155,6 +157,7 @@ def convert_to_pdf(filename, header_filename=None, footer_filename=None, cmd_opt
cmd_options['footer_html'] = footer_filename
return wkhtmltopdf(pages=filename, **cmd_options)


class RenderedFile(object):
"""
Create a temporary file resource of the rendered template with context.
Expand All @@ -180,6 +183,7 @@ def __del__(self):
if self.temporary_file is not None:
self.temporary_file.close()


def render_pdf_from_template(input_template, header_template, footer_template, context, request=None, cmd_options=None):
# For basic usage. Performs all the actions necessary to create a single
# page PDF from a single template and context.
Expand Down Expand Up @@ -217,6 +221,7 @@ def render_pdf_from_template(input_template, header_template, footer_template, c
footer_filename=footer_filename,
cmd_options=cmd_options)


def content_disposition_filename(filename):
"""
Sanitize a file name to be used in the Content-Disposition HTTP
Expand Down Expand Up @@ -260,34 +265,30 @@ 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,
}
settings.MEDIA_URL,
settings.STATIC_URL,
]

has_scheme = re.compile(r'^[^:/]+://')

for x in overrides:
if not x['url'] or has_scheme.match(x['url']):
if has_scheme.match(x):
continue

if not x['root'].endswith('/'):
x['root'] += '/'

occur_pattern = '''["|']({0}.*?)["|']'''
occurences = re.findall(occur_pattern.format(x['url']), content)
occurences = re.findall(occur_pattern.format(x), content)
occurences = list(set(occurences)) # Remove dups

for occur in occurences:
content = content.replace(occur,
pathname2fileurl(x['root']) +
occur[len(x['url']):])
filename = occur[len(x):]
pathname = finders.find(filename, all=False)

if pathname:
content = content.replace(occur, pathname2fileurl(pathname))

return content


def render_to_temporary_file(template, context, request=None, mode='w+b',
bufsize=-1, suffix='.html', prefix='tmp',
dir=None, delete=True):
Expand Down