Make sure to cleanup after monkeypatching template nodes in compress command. Fixes #262
This commit is contained in:
@@ -38,6 +38,9 @@ def patched_render(self, context):
|
||||
# not, to be able to handle complex inheritance chain.
|
||||
self._render_firstnode = MethodType(patched_render_firstnode, self)
|
||||
self._render_firstnode(context)
|
||||
|
||||
# Cleanup, uninstall our _render monkeypatch now that it has been called
|
||||
self._render = self._old_render
|
||||
return context
|
||||
|
||||
|
||||
@@ -52,7 +55,10 @@ def patched_render_firstnode(self, context):
|
||||
# its get_parent method so that rendering the ExtendsNode only
|
||||
# gives us the blocks content without doing any actual rendering.
|
||||
extra_context = {}
|
||||
firstnode = self.nodelist[0]
|
||||
try:
|
||||
firstnode = self.nodelist[0]
|
||||
except IndexError:
|
||||
firstnode = None
|
||||
if isinstance(firstnode, ExtendsNode):
|
||||
firstnode._log = self._log
|
||||
firstnode._log_verbosity = self._log_verbosity
|
||||
@@ -75,8 +81,11 @@ def patched_render_firstnode(self, context):
|
||||
# as well.
|
||||
if self._log_verbosity > 0:
|
||||
self._log.write("Caught error when rendering extend node from "
|
||||
"template %s\n" % self)
|
||||
"template %s\n" % getattr(self, 'name', self))
|
||||
return None
|
||||
finally:
|
||||
# Cleanup, uninstall our get_parent monkeypatch now that it has been called
|
||||
firstnode.get_parent = firstnode._old_get_parent
|
||||
return extra_context
|
||||
|
||||
|
||||
@@ -89,6 +98,7 @@ def patched_get_parent(self, context):
|
||||
compiled_template = self._old_get_parent(context)
|
||||
compiled_template._log = self._log
|
||||
compiled_template._log_verbosity = self._log_verbosity
|
||||
compiled_template._old_render = compiled_template._render
|
||||
compiled_template._render = MethodType(patched_render, compiled_template)
|
||||
return compiled_template
|
||||
|
||||
|
@@ -27,14 +27,13 @@ class OfflineTestCaseMixin(object):
|
||||
self._old_compress_offline = settings.COMPRESS_OFFLINE
|
||||
self._old_template_dirs = settings.TEMPLATE_DIRS
|
||||
self._old_offline_context = settings.COMPRESS_OFFLINE_CONTEXT
|
||||
self._old_template_loaders = settings.TEMPLATE_LOADERS
|
||||
self.log = StringIO()
|
||||
|
||||
# Force template dirs, because it enables us to force compress to
|
||||
# Reset template dirs, because it enables us to force compress to
|
||||
# consider only a specific directory (helps us make true,
|
||||
# independant unit tests).
|
||||
settings.TEMPLATE_DIRS = (
|
||||
os.path.join(settings.TEMPLATE_DIRS[0], self.templates_dir),
|
||||
os.path.join(settings.TEST_DIR, 'test_templates', self.templates_dir),
|
||||
)
|
||||
# Enable offline compress
|
||||
settings.COMPRESS_ENABLED = True
|
||||
@@ -47,7 +46,6 @@ class OfflineTestCaseMixin(object):
|
||||
settings.COMPRESS_ENABLED = self._old_compress
|
||||
settings.COMPRESS_OFFLINE = self._old_compress_offline
|
||||
settings.TEMPLATE_DIRS = self._old_template_dirs
|
||||
settings.TEMPLATE_LOADERS = self._old_template_loaders
|
||||
self.template_file.close()
|
||||
manifest_path = os.path.join('CACHE', 'manifest.json')
|
||||
if default_storage.exists(manifest_path):
|
||||
@@ -72,6 +70,24 @@ class OfflineGenerationBlockSuperMultipleTestCase(OfflineTestCaseMixin, TestCase
|
||||
templates_dir = "test_block_super_multiple"
|
||||
expected_hash = "2f6ef61c488e"
|
||||
|
||||
class OfflineGenerationBlockSuperMultipleWithCachedLoaderTestCase(OfflineTestCaseMixin, TestCase):
|
||||
templates_dir = "test_block_super_multiple_cached"
|
||||
expected_hash = "2f6ef61c488e"
|
||||
|
||||
def setUp(self):
|
||||
self._old_template_loaders = settings.TEMPLATE_LOADERS
|
||||
settings.TEMPLATE_LOADERS = (
|
||||
('django.template.loaders.cached.Loader', (
|
||||
'django.template.loaders.filesystem.Loader',
|
||||
'django.template.loaders.app_directories.Loader',
|
||||
)),
|
||||
)
|
||||
super(OfflineGenerationBlockSuperMultipleWithCachedLoaderTestCase, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
super(OfflineGenerationBlockSuperMultipleWithCachedLoaderTestCase, self).tearDown()
|
||||
settings.TEMPLATE_LOADERS = self._old_template_loaders
|
||||
|
||||
|
||||
class OfflineGenerationBlockSuperTestCaseWithExtraContent(OfflineTestCaseMixin, TestCase):
|
||||
templates_dir = "test_block_super_extra"
|
||||
|
@@ -0,0 +1,15 @@
|
||||
{% spaceless %}
|
||||
{% block js %}
|
||||
<script type="text/javascript">
|
||||
alert("test using multiple inheritance and block.super");
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block css %}
|
||||
<style type="text/css">
|
||||
body {
|
||||
background: red;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
{% endspaceless %}
|
@@ -0,0 +1,3 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block css %}{% endblock %}
|
@@ -0,0 +1,11 @@
|
||||
{% extends "base2.html" %}
|
||||
{% load compress %}
|
||||
|
||||
{% block js %}{% spaceless %}
|
||||
{% compress js %}
|
||||
{{ block.super }}
|
||||
<script type="text/javascript">
|
||||
alert("this alert shouldn't be alone!");
|
||||
</script>
|
||||
{% endcompress %}
|
||||
{% endspaceless %}{% endblock %}
|
Reference in New Issue
Block a user