Minor refactoring in compress templatetag: part 2
there is a minor change in behaviour in here, for which i added a test.
This commit is contained in:
@@ -56,51 +56,49 @@ class CompressorMixin(object):
|
|||||||
return (settings.COMPRESS_ENABLED and
|
return (settings.COMPRESS_ENABLED and
|
||||||
settings.COMPRESS_OFFLINE) or forced
|
settings.COMPRESS_OFFLINE) or forced
|
||||||
|
|
||||||
def render_offline(self, context, forced):
|
def render_offline(self, context):
|
||||||
"""
|
"""
|
||||||
If enabled and in offline mode, and not forced check the offline cache
|
If enabled and in offline mode, and not forced check the offline cache
|
||||||
and return the result if given
|
and return the result if given
|
||||||
"""
|
"""
|
||||||
if self.is_offline_compression_enabled(forced) and not forced:
|
key = get_offline_hexdigest(self.get_original_content(context))
|
||||||
key = get_offline_hexdigest(self.get_original_content(context))
|
offline_manifest = get_offline_manifest()
|
||||||
offline_manifest = get_offline_manifest()
|
if key in offline_manifest:
|
||||||
if key in offline_manifest:
|
return offline_manifest[key]
|
||||||
return offline_manifest[key]
|
else:
|
||||||
else:
|
raise OfflineGenerationError('You have offline compression '
|
||||||
raise OfflineGenerationError('You have offline compression '
|
'enabled but key "%s" is missing from offline manifest. '
|
||||||
'enabled but key "%s" is missing from offline manifest. '
|
'You may need to run "python manage.py compress".' % key)
|
||||||
'You may need to run "python manage.py compress".' % key)
|
|
||||||
|
|
||||||
def render_cached(self, compressor, kind, mode, forced=False):
|
def render_cached(self, compressor, kind, mode):
|
||||||
"""
|
"""
|
||||||
If enabled checks the cache for the given compressor's cache key
|
If enabled checks the cache for the given compressor's cache key
|
||||||
and return a tuple of cache key and output
|
and return a tuple of cache key and output
|
||||||
"""
|
"""
|
||||||
if settings.COMPRESS_ENABLED and not forced:
|
cache_key = get_templatetag_cachekey(compressor, mode, kind)
|
||||||
cache_key = get_templatetag_cachekey(compressor, mode, kind)
|
cache_content = cache_get(cache_key)
|
||||||
cache_content = cache_get(cache_key)
|
return cache_key, cache_content
|
||||||
return cache_key, cache_content
|
|
||||||
return None, None
|
|
||||||
|
|
||||||
def render_compressed(self, context, kind, mode, forced=False):
|
def render_compressed(self, context, kind, mode, forced=False):
|
||||||
|
|
||||||
# See if it has been rendered offline
|
# See if it has been rendered offline
|
||||||
cached_offline = self.render_offline(context, forced=forced)
|
if self.is_offline_compression_enabled(forced) and not forced:
|
||||||
if cached_offline:
|
return self.render_offline(context)
|
||||||
return cached_offline
|
|
||||||
|
|
||||||
# Take a shortcut if we really don't have anything to do
|
# Take a shortcut if we really don't have anything to do
|
||||||
if ((not settings.COMPRESS_ENABLED and
|
if (not settings.COMPRESS_ENABLED and
|
||||||
not settings.COMPRESS_PRECOMPILERS) and not forced):
|
not settings.COMPRESS_PRECOMPILERS and not forced):
|
||||||
return self.get_original_content(context)
|
return self.get_original_content(context)
|
||||||
|
|
||||||
context['compressed'] = {'name': getattr(self, 'name', None)}
|
context['compressed'] = {'name': getattr(self, 'name', None)}
|
||||||
compressor = self.get_compressor(context, kind)
|
compressor = self.get_compressor(context, kind)
|
||||||
|
|
||||||
# Prepare the actual compressor and check cache
|
# Check cache
|
||||||
cache_key, cache_content = self.render_cached(compressor, kind, mode, forced=forced)
|
cache_key = None
|
||||||
if cache_content is not None:
|
if settings.COMPRESS_ENABLED and not forced:
|
||||||
return cache_content
|
cache_key, cache_content = self.render_cached(compressor, kind, mode)
|
||||||
|
if cache_content is not None:
|
||||||
|
return cache_content
|
||||||
|
|
||||||
rendered_output = compressor.output(mode, forced=forced)
|
rendered_output = compressor.output(mode, forced=forced)
|
||||||
assert isinstance(rendered_output, six.string_types)
|
assert isinstance(rendered_output, six.string_types)
|
||||||
|
@@ -358,6 +358,24 @@ class OfflineGenerationTestCase(OfflineTestCaseMixin, TestCase):
|
|||||||
settings.TEMPLATE_LOADERS = old_loaders
|
settings.TEMPLATE_LOADERS = old_loaders
|
||||||
|
|
||||||
|
|
||||||
|
class OfflineGenerationEmptyTag(OfflineTestCaseMixin, TestCase):
|
||||||
|
"""
|
||||||
|
In case of a compress template tag with no content, an entry
|
||||||
|
will be added to the manifest with an empty string as value.
|
||||||
|
This test makes sure there is no recompression happening when
|
||||||
|
compressor encounters such an emptystring in the manifest.
|
||||||
|
"""
|
||||||
|
templates_dir = "basic"
|
||||||
|
expected_hash = "f5e179b8eca4"
|
||||||
|
engines = ("django",)
|
||||||
|
|
||||||
|
def _test_offline(self, engine):
|
||||||
|
count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity, engine=engine)
|
||||||
|
manifest = get_offline_manifest()
|
||||||
|
manifest[list(manifest)[0]] = ""
|
||||||
|
self.assertEqual(self._render_template(engine), "\n")
|
||||||
|
|
||||||
|
|
||||||
class OfflineGenerationBlockSuperBaseCompressed(OfflineTestCaseMixin, TestCase):
|
class OfflineGenerationBlockSuperBaseCompressed(OfflineTestCaseMixin, TestCase):
|
||||||
template_names = ["base.html", "base2.html", "test_compressor_offline.html"]
|
template_names = ["base.html", "base2.html", "test_compressor_offline.html"]
|
||||||
templates_dir = 'test_block_super_base_compressed'
|
templates_dir = 'test_block_super_base_compressed'
|
||||||
|
Reference in New Issue
Block a user