Commit f57e4959 authored by Jannis Leidel's avatar Jannis Leidel

Use the current context when rendering the output templates.

parent bf8a1e0c
......@@ -4,6 +4,7 @@ import codecs
from django.conf import settings
from django.core.files.base import ContentFile
from django.template import Context
from django.template.loader import render_to_string
from django.utils.encoding import smart_unicode
......@@ -27,13 +28,14 @@ class Compressor(object):
"""
type = None
def __init__(self, content=None, output_prefix="compressed"):
def __init__(self, content=None, output_prefix=None, context=None, *args, **kwargs):
self.content = content or ""
self.output_prefix = output_prefix
self.output_prefix = output_prefix or "compressed"
self.output_dir = settings.COMPRESS_OUTPUT_DIR.strip('/')
self.charset = settings.DEFAULT_CHARSET
self.storage = default_storage
self.split_content = []
self.context = context or {}
self.extra_context = {}
self.all_mimetypes = dict(settings.COMPRESS_PRECOMPILERS)
self.finders = staticfiles.finders
......@@ -254,6 +256,9 @@ class Compressor(object):
"""
if context is None:
context = {}
context.update(self.extra_context)
return render_to_string(
"compressor/%s_%s.html" % (self.type, mode), context)
final_context = Context()
final_context.update(context)
final_context.update(self.context)
final_context.update(self.extra_context)
return render_to_string("compressor/%s_%s.html" %
(self.type, mode), final_context)
......@@ -8,8 +8,9 @@ class CssCompressor(Compressor):
template_name = "compressor/css.html"
template_name_inline = "compressor/css_inline.html"
def __init__(self, content=None, output_prefix="css"):
super(CssCompressor, self).__init__(content, output_prefix)
def __init__(self, content=None, output_prefix="css", context=None):
super(CssCompressor, self).__init__(content=content,
output_prefix=output_prefix, context=context)
self.filters = list(settings.COMPRESS_CSS_FILTERS)
self.type = output_prefix
......@@ -35,7 +36,8 @@ class CssCompressor(Compressor):
if self.media_nodes and self.media_nodes[-1][0] == media:
self.media_nodes[-1][1].split_content.append(data)
else:
node = CssCompressor(self.parser.elem_str(elem))
node = CssCompressor(content=self.parser.elem_str(elem),
context=self.context)
node.split_content.append(data)
self.media_nodes.append((media, node))
return self.split_content
......
......@@ -8,8 +8,8 @@ class JsCompressor(Compressor):
template_name = "compressor/js.html"
template_name_inline = "compressor/js_inline.html"
def __init__(self, content=None, output_prefix="js"):
super(JsCompressor, self).__init__(content, output_prefix)
def __init__(self, content=None, output_prefix="js", context=None):
super(JsCompressor, self).__init__(content, output_prefix, context)
self.filters = list(settings.COMPRESS_JS_FILTERS)
self.type = output_prefix
......
<link rel="stylesheet" href="{{ url }}" type="text/css"{% if media %} media="{{ media }}"{% endif %} />
<link rel="stylesheet" href="{{ url }}" type="text/css"{% if media %} media="{{ media }}"{% endif %} />
\ No newline at end of file
......@@ -70,7 +70,8 @@ class CompressorNode(template.Node):
return cached_offline
# 3. Prepare the actual compressor and check cache
compressor = self.compressor_cls(self.nodelist.render(context))
compressor = self.compressor_cls(content=self.nodelist.render(context),
context=context)
cache_key, cache_content = self.render_cached(compressor, forced)
if cache_content is not None:
return cache_content
......
......@@ -43,6 +43,8 @@ HEAD
- Make use of `django-appconf`_ for settings handling and `versiontools`_
for versions.
- Uses the current context when rendering the render templates.
.. _`Slim It`: http://slimit.org/
.. _`django-appconf`: http://django-appconf.rtfd.org/
.. _`versiontools`: http://pypi.python.org/pypi/versiontools
......
......@@ -30,11 +30,11 @@ class OfflineGenerationTestCase(TestCase):
count, result = CompressCommand().compress()
self.assertEqual(5, count)
self.assertEqual([
css_tag('/media/CACHE/css/cd579b7deb7d.css')+'\n',
css_tag('/media/CACHE/css/cd579b7deb7d.css'),
u'<script type="text/javascript" src="/media/CACHE/js/0a2bb9a287c0.js"></script>',
u'<script type="text/javascript" src="/media/CACHE/js/fb1736ad48b7.js"></script>',
u'<script type="text/javascript" src="/media/CACHE/js/770a7311729e.js"></script>',
u'<link rel="stylesheet" href="/media/CACHE/css/67ed6aff7f7b.css" type="text/css" />\n',
u'<link rel="stylesheet" href="/media/CACHE/css/67ed6aff7f7b.css" type="text/css" />',
], result)
# Template rendering should use the cache. FIXME: how to make sure of it ? Should we test the cache
# key<->values ourselves?
......@@ -49,11 +49,11 @@ class OfflineGenerationTestCase(TestCase):
count, result = CompressCommand().compress()
self.assertEqual(5, count)
self.assertEqual([
css_tag('/media/CACHE/css/ee62fbfd116a.css')+'\n',
css_tag('/media/CACHE/css/ee62fbfd116a.css'),
u'<script type="text/javascript" src="/media/CACHE/js/0a2bb9a287c0.js"></script>',
u'<script type="text/javascript" src="/media/CACHE/js/fb1736ad48b7.js"></script>',
u'<script type="text/javascript" src="/media/CACHE/js/770a7311729e.js"></script>',
u'<link rel="stylesheet" href="/media/CACHE/css/73e015f740c6.css" type="text/css" />\n',
u'<link rel="stylesheet" href="/media/CACHE/css/73e015f740c6.css" type="text/css" />',
], result)
# Template rendering should use the cache. FIXME: how to make sure of it ? Should we test the cache
# key<->values ourselves?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment