Another pass over the offline code to make sure it works when compression is actually disabled (e.g. in deployment situations).

This commit is contained in:
Jannis Leidel
2011-02-17 00:59:20 +01:00
parent e96ab45ed2
commit 7d1f159523
6 changed files with 33 additions and 27 deletions

View File

@@ -117,8 +117,8 @@ class Compressor(object):
self.storage.save(self.new_filepath, ContentFile(self.combined))
return True
def output(self):
if not settings.COMPRESS_ENABLED:
def output(self, forced=False):
if not settings.COMPRESS_ENABLED and not forced:
return self.content
context = {
"saved": self.save_file(),

View File

@@ -41,14 +41,14 @@ class CssCompressor(Compressor):
self.media_nodes.append((media, node))
return self.split_content
def output(self):
def output(self, forced=False):
self.split_contents()
if not hasattr(self, 'media_nodes'):
return super(CssCompressor, self).output()
if not settings.COMPRESS_ENABLED:
return super(CssCompressor, self).output(forced=forced)
if not settings.COMPRESS_ENABLED and not forced:
return self.content
ret = []
for media, subnode in self.media_nodes:
subnode.extra_context.update({'media': media})
ret.append(subnode.output())
ret.append(subnode.output(forced=forced))
return "".join(ret)

View File

@@ -139,7 +139,7 @@ class Command(NoArgsCommand):
for nodes in compressor_nodes.values():
for node in nodes:
key = get_offline_cachekey(node.nodelist)
result = node.render(context, compress=True, offline=False)
result = node.render(context, forced=True)
cache.set(key, result, settings.COMPRESS_OFFLINE_TIMEOUT)
results.append(result)
count += 1

View File

@@ -16,6 +16,9 @@ class CompressorSettings(AppSettings):
OUTPUT_DIR = 'cache'
STORAGE = 'compressor.storage.CompressorFileStorage'
CSS_COMPRESSOR = "compressor.css.CssCompressor"
JS_COMPRESSOR = "compressor.js.JsCompressor"
URL = None
ROOT = None

View File

@@ -1,15 +1,21 @@
import time
from django import template
from django.core.exceptions import ImproperlyConfigured
from compressor.cache import cache, get_offline_cachekey
from compressor.conf import settings
from compressor.css import CssCompressor
from compressor.js import JsCompressor
from compressor.utils import get_class
OUTPUT_FILE = 'file'
OUTPUT_INLINE = 'inline'
OUTPUT_MODES = (OUTPUT_FILE, OUTPUT_INLINE)
COMPRESSORS = {
"css": settings.COMPRESS_CSS_COMPRESSOR,
"js": settings.COMPRESS_JS_COMPRESSOR,
}
register = template.Library()
@@ -18,6 +24,8 @@ class CompressorNode(template.Node):
self.nodelist = nodelist
self.kind = kind
self.mode = mode
self.compressor_cls = get_class(
COMPRESSORS.get(self.kind), exception=ImproperlyConfigured)
def cache_get(self, key):
packed_val = cache.get(key)
@@ -37,30 +45,23 @@ class CompressorNode(template.Node):
packed_val = (val, refresh_time, refreshed)
return cache.set(key, packed_val, real_timeout)
def render(self, context, compress=settings.COMPRESS_ENABLED,
offline=settings.COMPRESS_OFFLINE):
if compress and offline:
def render(self, context, forced=False):
if (settings.COMPRESS_ENABLED and settings.COMPRESS_OFFLINE) and not forced:
key = get_offline_cachekey(self.nodelist)
content = cache.get(key)
if content:
return content
content = self.nodelist.render(context)
if offline or not compress or not len(content.strip()):
if (not settings.COMPRESS_ENABLED or not len(content.strip())) and not forced:
return content
if self.kind == 'css':
compressor = CssCompressor(content)
elif self.kind == 'js':
compressor = JsCompressor(content)
cachekey = "%s.%s" % (compressor.cachekey, self.mode)
compressor = self.compressor_cls(content)
output = self.cache_get(cachekey)
if output is None or not offline:
if output is None or forced:
try:
if self.mode == OUTPUT_FILE:
output = compressor.output()
elif self.mode == OUTPUT_INLINE:
output = compressor.output_inline()
else:
output = content
if self.mode == OUTPUT_INLINE:
return compressor.output_inline()
output = compressor.output(forced=forced)
self.cache_set(cachekey, output)
except:
from traceback import format_exc
@@ -116,13 +117,13 @@ def compress(parser, token):
"%r tag requires either one or two arguments." % args[0])
kind = args[1]
if not kind in ('css', 'js'):
if not kind in COMPRESSORS.keys():
raise template.TemplateSyntaxError(
"%r's argument must be 'js' or 'css'." % args[0])
if len(args) == 3:
mode = args[2]
if not mode in (OUTPUT_FILE, OUTPUT_INLINE):
if not mode in OUTPUT_MODES:
raise template.TemplateSyntaxError(
"%r's second argument must be '%s' or '%s'." %
(args[0], OUTPUT_FILE, OUTPUT_INLINE))

View File

@@ -27,8 +27,10 @@ def get_class(class_string, exception=FilterError):
if class_name != '':
cls = getattr(__import__(mod_name, {}, {}, ['']), class_name)
except (ImportError, AttributeError):
raise exception('Failed to import filter %s' % class_string)
return cls
pass
else:
return cls
raise exception('Failed to import %s' % class_string)
def get_mod_func(callback):
"""