Skip to content
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

Ensure generated file always contains a base name #861

Merged
Merged
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
4 changes: 2 additions & 2 deletions compressor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def filter(self, content, filters, method, **kwargs):
pass
return content

def output(self, mode='file', forced=False):
def output(self, mode='file', forced=False, basename=None):
"""
The general output method, override in subclass if you need to do
any custom modification. Calls other mode specific methods or simply
Expand All @@ -299,7 +299,7 @@ def output(self, mode='file', forced=False):

if settings.COMPRESS_ENABLED or forced:
filtered_output = self.filter_output(output)
return self.handle_output(mode, filtered_output, forced)
return self.handle_output(mode, filtered_output, forced, basename)

return output

Expand Down
2 changes: 1 addition & 1 deletion compressor/filters/css_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def post_process_url(self, url):

- original file URL: '/static/my-app/style.css'
- it has an image link: ``url(images/logo.svg)``
- compiled file URL: '/static/CACHE/css/abcdef123456.css'
- compiled file URL: '/static/CACHE/css/output.abcdef123456.css'
- replaced image link URL: ``url(../../my-app/images/logo.svg)``
"""
old_prefix = self.url
Expand Down
32 changes: 7 additions & 25 deletions compressor/templatetags/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ def render_compressed(self, context, kind, mode, forced=False):
if cache_content is not None:
return cache_content

rendered_output = compressor.output(mode, forced=forced)
file_basename = getattr(self, 'name', None) or getattr(self, 'basename', None)
if file_basename is None:
file_basename = 'output'

rendered_output = compressor.output(mode, forced=forced, basename=file_basename)
assert isinstance(rendered_output, six.string_types)
if cache_key:
cache_set(cache_key, rendered_output)
Expand Down Expand Up @@ -138,36 +142,14 @@ def compress(parser, token):

Syntax::

{% compress <js/css> %}
{% compress <js/css> [<file/inline> [block_name]] %}
<html of inline or linked JS/CSS>
{% endcompress %}

Examples::

{% compress css %}
<link rel="stylesheet" href="/static/css/one.css" type="text/css" charset="utf-8">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/static/css/two.css" type="text/css" charset="utf-8">
{% endcompress %}

Which would be rendered something like::

<link rel="stylesheet" href="/static/CACHE/css/f7c661b7a124.css" type="text/css" media="all" charset="utf-8">

or::

{% compress js %}
<script src="/static/js/one.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>
{% endcompress %}

Which would be rendered something like::

<script type="text/javascript" src="/static/CACHE/js/3f33b9146e12.js" charset="utf-8"></script>
See docs/usage.rst

Linked files must be on your COMPRESS_URL (which defaults to STATIC_URL).
If DEBUG is true off-site files will throw exceptions. If DEBUG is false
they will be silently stripped.
"""

nodelist = parser.parse(('endcompress',))
Expand Down
12 changes: 6 additions & 6 deletions compressor/tests/test_jinja2ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_css_tag(self):
<link rel="stylesheet" href="{{ STATIC_URL }}css/two.css" type="text/css" charset="utf-8">
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = css_tag("/static/CACHE/css/58a8c0714e59.css")
out = css_tag("/static/CACHE/css/output.58a8c0714e59.css")
self.assertEqual(out, template.render(context))

def test_nonascii_css_tag(self):
Expand All @@ -86,7 +86,7 @@ def test_nonascii_css_tag(self):
<style type="text/css">p { border:5px solid green;}</style>
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = css_tag("/static/CACHE/css/4263023f49d6.css")
out = css_tag("/static/CACHE/css/output.4263023f49d6.css")
self.assertEqual(out, template.render(context))

def test_js_tag(self):
Expand All @@ -95,7 +95,7 @@ def test_js_tag(self):
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = '<script type="text/javascript" src="/static/CACHE/js/74e158ccb432.js"></script>'
out = '<script type="text/javascript" src="/static/CACHE/js/output.74e158ccb432.js"></script>'
self.assertEqual(out, template.render(context))

def test_nonascii_js_tag(self):
Expand All @@ -104,7 +104,7 @@ def test_nonascii_js_tag(self):
<script type="text/javascript" charset="utf-8">var test_value = "\u2014";</script>
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = '<script type="text/javascript" src="/static/CACHE/js/a18195c6ae48.js"></script>'
out = '<script type="text/javascript" src="/static/CACHE/js/output.a18195c6ae48.js"></script>'
self.assertEqual(out, template.render(context))

def test_nonascii_latin1_js_tag(self):
Expand All @@ -113,7 +113,7 @@ def test_nonascii_latin1_js_tag(self):
<script type="text/javascript">var test_value = "\u2014";</script>
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = '<script type="text/javascript" src="/static/CACHE/js/f64debbd8878.js"></script>'
out = '<script type="text/javascript" src="/static/CACHE/js/output.f64debbd8878.js"></script>'
self.assertEqual(out, template.render(context))

def test_css_inline(self):
Expand Down Expand Up @@ -143,6 +143,6 @@ def test_nonascii_inline_css(self):
'<style type="text/css">'
'/* русский текст */'
'</style>{% endcompress %}')
out = '<link rel="stylesheet" href="/static/CACHE/css/c836c9caed5c.css" type="text/css" />'
out = '<link rel="stylesheet" href="/static/CACHE/css/output.c836c9caed5c.css" type="text/css" />'
context = {'STATIC_URL': settings.COMPRESS_URL}
self.assertEqual(out, template.render(context))
17 changes: 13 additions & 4 deletions compressor/tests/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class OfflineTestCaseMixin(object):
verbosity = 0
# Change this for each test class
templates_dir = ''
expected_basename = 'output'
expected_hash = ''
# Engines to test
engines = ('django', 'jinja2')
Expand Down Expand Up @@ -134,17 +135,17 @@ def _render_template(self, engine):

def _render_script(self, hash):
return (
'<script type="text/javascript" src="{}CACHE/js/{}.js">'
'<script type="text/javascript" src="{}CACHE/js/{}.{}.js">'
'</script>'.format(
settings.COMPRESS_URL_PLACEHOLDER, hash
settings.COMPRESS_URL_PLACEHOLDER, self.expected_basename, hash
)
)

def _render_link(self, hash):
return (
'<link rel="stylesheet" href="{}CACHE/css/{}.css" '
'<link rel="stylesheet" href="{}CACHE/css/{}.{}.css" '
'type="text/css" />'.format(
settings.COMPRESS_URL_PLACEHOLDER, hash
settings.COMPRESS_URL_PLACEHOLDER, self.expected_basename, hash
)
)

Expand Down Expand Up @@ -369,6 +370,14 @@ class OfflineCompressStaticTemplateTagTestCase(OfflineTestCaseMixin, TestCase):
expected_hash = '2607a2085687'


class OfflineCompressTemplateTagNamedTestCase(OfflineTestCaseMixin, TestCase):
templates_dir = 'test_templatetag_named'
expected_basename = 'output_name'
expected_hash = 'a432b6ddb2c4'
# block naming unsupported in jinga2
engines = ('django',)


class OfflineCompressTestCaseWithContext(OfflineTestCaseMixin, TestCase):
templates_dir = 'test_with_context'
expected_hash = '045b3ad664c8'
Expand Down
2 changes: 1 addition & 1 deletion compressor/tests/test_storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_css_tag_with_storage(self):
{% endcompress %}
"""
context = {'STATIC_URL': settings.COMPRESS_URL}
out = css_tag("/static/CACHE/css/aca9bcd16bee.css")
out = css_tag("/static/CACHE/css/output.aca9bcd16bee.css")
self.assertEqual(out, render(template, context))

def test_race_condition_handling(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% load compress %}{% spaceless %}

{% compress js file output_name %}
<script type="text/javascript">
alert("Basic test");
</script>
{% endcompress %}{% endspaceless %}
29 changes: 19 additions & 10 deletions compressor/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ def test_css_tag(self):
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="{{ STATIC_URL }}css/two.css" type="text/css">
{% endcompress %}"""
out = css_tag("/static/CACHE/css/58a8c0714e59.css")
out = css_tag("/static/CACHE/css/output.58a8c0714e59.css")
self.assertEqual(out, render(template, self.context))

def test_css_tag_with_block(self):
template = """{% load compress %}{% compress css file block_name %}
<link rel="stylesheet" href="{{ STATIC_URL }}css/one.css" type="text/css">
<style type="text/css">p { border:5px solid blue;}</style>
<link rel="stylesheet" href="{{ STATIC_URL }}css/two.css" type="text/css">
{% endcompress %}"""
out = css_tag("/static/CACHE/css/block_name.393dbcddb48e.css")
self.assertEqual(out, render(template, self.context))

def test_missing_rel_leaves_empty_result(self):
Expand All @@ -62,7 +71,7 @@ def test_missing_rel_only_on_one_resource(self):
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="{{ STATIC_URL }}css/two.css" type="text/css">
{% endcompress %}"""
out = css_tag("/static/CACHE/css/58a8c0714e59.css")
out = css_tag("/static/CACHE/css/output.58a8c0714e59.css")
self.assertEqual(out, render(template, self.context))

def test_uppercase_rel(self):
Expand All @@ -71,7 +80,7 @@ def test_uppercase_rel(self):
<style type="text/css">p { border:5px solid green;}</style>
<link rel="StyleSheet" href="{{ STATIC_URL }}css/two.css" type="text/css">
{% endcompress %}"""
out = css_tag("/static/CACHE/css/58a8c0714e59.css")
out = css_tag("/static/CACHE/css/output.58a8c0714e59.css")
self.assertEqual(out, render(template, self.context))

def test_nonascii_css_tag(self):
Expand All @@ -80,7 +89,7 @@ def test_nonascii_css_tag(self):
<style type="text/css">p { border:5px solid green;}</style>
{% endcompress %}
"""
out = css_tag("/static/CACHE/css/4263023f49d6.css")
out = css_tag("/static/CACHE/css/output.4263023f49d6.css")
self.assertEqual(out, render(template, self.context))

def test_js_tag(self):
Expand All @@ -89,7 +98,7 @@ def test_js_tag(self):
<script type="text/javascript">obj.value = "value";</script>
{% endcompress %}
"""
out = '<script type="text/javascript" src="/static/CACHE/js/74e158ccb432.js"></script>'
out = '<script type="text/javascript" src="/static/CACHE/js/output.74e158ccb432.js"></script>'
self.assertEqual(out, render(template, self.context))

def test_nonascii_js_tag(self):
Expand All @@ -98,7 +107,7 @@ def test_nonascii_js_tag(self):
<script type="text/javascript">var test_value = "\u2014";</script>
{% endcompress %}
"""
out = '<script type="text/javascript" src="/static/CACHE/js/a18195c6ae48.js"></script>'
out = '<script type="text/javascript" src="/static/CACHE/js/output.a18195c6ae48.js"></script>'
self.assertEqual(out, render(template, self.context))

def test_nonascii_latin1_js_tag(self):
Expand All @@ -107,7 +116,7 @@ def test_nonascii_latin1_js_tag(self):
<script type="text/javascript">var test_value = "\u2014";</script>
{% endcompress %}
"""
out = '<script type="text/javascript" src="/static/CACHE/js/f64debbd8878.js"></script>'
out = '<script type="text/javascript" src="/static/CACHE/js/output.f64debbd8878.js"></script>'
self.assertEqual(out, render(template, self.context))

def test_compress_tag_with_illegal_arguments(self):
Expand Down Expand Up @@ -168,7 +177,7 @@ def test_sekizai_only_once(self):
<script type="text/javascript">var tmpl="{% templatetag openblock %} if x == 3 %}x IS 3{% templatetag openblock %} endif %}"</script>
{% endaddtoblock %}{% render_block "js" postprocessor "compressor.contrib.sekizai.compress" %}
"""
out = '<script type="text/javascript" src="/static/CACHE/js/4d88842b99b3.js"></script>'
out = '<script type="text/javascript" src="/static/CACHE/js/output.4d88842b99b3.js"></script>'
self.assertEqual(out, render(template, self.context, SekizaiContext))


Expand Down Expand Up @@ -197,15 +206,15 @@ def test_compress_coffeescript_tag(self):
template = """{% load compress %}{% compress js %}
<script type="text/coffeescript"># this is a comment.</script>
{% endcompress %}"""
out = script(src="/static/CACHE/js/fb128b610c3e.js")
out = script(src="/static/CACHE/js/output.fb128b610c3e.js")
self.assertEqual(out, render(template, self.context))

def test_compress_coffeescript_tag_and_javascript_tag(self):
template = """{% load compress %}{% compress js %}
<script type="text/coffeescript"># this is a comment.</script>
<script type="text/javascript"># this too is a comment.</script>
{% endcompress %}"""
out = script(src="/static/CACHE/js/cf3495aaff6e.js")
out = script(src="/static/CACHE/js/output.cf3495aaff6e.js")
self.assertEqual(out, render(template, self.context))

@override_settings(COMPRESS_ENABLED=False)
Expand Down
6 changes: 3 additions & 3 deletions docs/settings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Base settings
like this::

<script type="text/javascript" src="/static/js/site-base.js"></script>
<script type="text/javascript" src="/static/CACHE/js/8dd1a2872443.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/CACHE/js/awesome.8dd1a2872443.js" charset="utf-8"></script>

.. attribute:: COMPRESS_URL

Expand Down Expand Up @@ -301,7 +301,7 @@ Backend settings

This would give you something like this::

<script type="text/javascript" src="/static/CACHE/js/8dd1a2872443.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/CACHE/js/output.8dd1a2872443.js" charset="utf-8"></script>

The same works for less_, too:

Expand All @@ -322,7 +322,7 @@ Backend settings

Which would be rendered something like::

<link rel="stylesheet" href="/static/CACHE/css/8ccf8d877f18.css" type="text/css" charset="utf-8">
<link rel="stylesheet" href="/static/CACHE/css/output.8ccf8d877f18.css" type="text/css" charset="utf-8">

.. _less: http://lesscss.org/
.. _CoffeeScript: http://coffeescript.org/
Expand Down
Loading