Skip to content
Browse files

Merge branch 'develop' of https://github.com/diox/django_compressor i…

…nto diox-develop

Conflicts:
	compressor/templatetags/compress.py
  • Loading branch information...
2 parents 78d1db2 + cdf8d8b commit b185a0f9ef2e09cf1fbfc98ca9a92802b22f8cc1 @jezdez jezdez committed Jan 30, 2012
Showing with 42 additions and 26 deletions.
  1. +1 −1 compressor/management/commands/compress.py
  2. +41 −25 compressor/templatetags/compress.py
View
2 compressor/management/commands/compress.py
@@ -243,7 +243,7 @@ def walk_nodes(self, node, block_name=None):
for node in self.get_nodelist(node):
if isinstance(node, BlockNode):
block_name = node.name
- if isinstance(node, CompressorNode):
+ if isinstance(node, CompressorNode) and node.is_offline_compression_enabled(forced=True):
node._block_name = block_name
yield node
else:
View
66 compressor/templatetags/compress.py
@@ -15,10 +15,10 @@
class CompressorMixin(object):
-
+
def get_original_content(self, context):
raise NotImplementedError
-
+
@property
def compressors(self):
return {
@@ -30,23 +30,39 @@ def compressor_cls(self, kind, *args, **kwargs):
if kind not in self.compressors.keys():
raise template.TemplateSyntaxError(
"The compress tag's argument must be 'js' or 'css'.")
-
return get_class(self.compressors.get(kind),
- exception=ImproperlyConfigured)(*args, **kwargs)
-
+ exception=ImproperlyConfigured)(*args, **kwargs)
+
def get_compressor(self, context, kind):
- return self.compressor_cls(kind,
- content=self.get_original_content(context),
- context=context)
+ return self.compressor_cls(kind,
+ content=self.get_original_content(context), context=context)
+
+ def debug_mode(self, context):
+ if settings.COMPRESS_DEBUG_TOGGLE:
+ # Only check for the debug parameter
+ # if a RequestContext was used
+ request = context.get('request', None)
+ if request is not None:
+ return settings.COMPRESS_DEBUG_TOGGLE in request.GET
+
+ def is_offline_compression_enabled(self, forced):
+ """
+ Check if offline compression is enabled or forced
- def render_offline(self, context, forced=False):
+ Defaults to just checking the settings and forced argument,
+ but can be overriden to completely disable compression for
+ a subclass, for instance.
"""
- If enabled and in offline mode, and not forced or in debug mode
- check the offline cache and return the result if given
+ return (settings.COMPRESS_ENABLED and
+ settings.COMPRESS_OFFLINE) or forced
+
+ def render_offline(self, context, forced):
+ """
+ If enabled and in offline mode, and not forced check the offline cache
+ and return the result if given
"""
- if (settings.COMPRESS_ENABLED and
- settings.COMPRESS_OFFLINE) and not forced:
- key = get_offline_hexdigest(self.get_original_content(context))
+ if self.is_offline_compression_enabled(forced) and not forced:
+ key = get_offline_hexdigest(self.nodelist.render(context))
offline_manifest = get_offline_manifest()
if key in offline_manifest:
return offline_manifest[key]
@@ -65,17 +81,17 @@ def render_cached(self, compressor, kind, mode, forced=False):
cache_content = cache_get(cache_key)
return cache_key, cache_content
return None, None
-
+
def render_compressed(self, context, kind, mode, forced=False):
-
+
# See if it has been rendered offline
cached_offline = self.render_offline(context, forced=forced)
if cached_offline:
return cached_offline
-
+
context['compressed'] = {'name': getattr(self, 'name', None)}
compressor = self.get_compressor(context, kind)
-
+
# Prepare the actual compressor and check cache
cache_key, cache_content = self.render_cached(compressor, kind, mode, forced=forced)
if cache_content is not None:
@@ -93,22 +109,22 @@ def render_compressed(self, context, kind, mode, forced=False):
# Or don't do anything in production
return self.get_original_content(context)
-
+
def render_output(self, compressor, mode, forced=False):
return compressor.output(mode, forced=forced)
-
-
+
+
class CompressorNode(CompressorMixin, template.Node):
def __init__(self, nodelist, kind=None, mode=OUTPUT_FILE, name=None):
self.nodelist = nodelist
self.kind = kind
self.mode = mode
self.name = name
-
+
def get_original_content(self, context):
return self.nodelist.render(context)
-
+
def debug_mode(self, context):
if settings.COMPRESS_DEBUG_TOGGLE:
# Only check for the debug parameter
@@ -122,9 +138,9 @@ def render(self, context, forced=False):
# Check if in debug mode
if self.debug_mode(context):
return self.get_original_content(context)
-
+
return self.render_compressed(context, self.kind, self.mode, forced=forced)
-
+
@register.tag
def compress(parser, token):

0 comments on commit b185a0f

Please sign in to comment.
Something went wrong with that request. Please try again.