Commit 40340cf3 by andreas.pelme

* Fixed bug in settings - COMPRESS_AUTO was mistyped.

 * Added setting COMPRESS_AUTO_TEMPLATES - for updating the output file whenever needed when the template tags are invoked
   This setting is enabled by default.
* Minor cleanups in the templatetag code



git-svn-id: https://django-compress.googlecode.com/svn/trunk@50 98d35234-f74b-0410-9e22-51d878bdf110
parent f62e3005
from django.conf import settings from django.conf import settings
COMPRESS = getattr(settings, 'COMPRESS', not settings.DEBUG) COMPRESS = getattr(settings, 'COMPRESS', not settings.DEBUG)
COMPRESS_AUTO = getattr(settings, 'COMPRESS_AUTH', True) COMPRESS_AUTO = getattr(settings, 'COMPRESS_AUTO', True)
COMPRESS_AUTO_TEMPLATES = getattr(settings, 'COMPRESS_AUTO_TEMPLATES', True)
COMPRESS_CSS_FILTERS = getattr(settings, 'COMPRESS_CSS_FILTERS', ('compress.filters.csstidy.CSSTidyFilter', )) COMPRESS_CSS_FILTERS = getattr(settings, 'COMPRESS_CSS_FILTERS', ('compress.filters.csstidy.CSSTidyFilter', ))
COMPRESS_JS_FILTERS = getattr(settings, 'COMPRESS_JS_FILTERS', ('compress.filters.jsmin.JSMinFilter',)) COMPRESS_JS_FILTERS = getattr(settings, 'COMPRESS_JS_FILTERS', ('compress.filters.jsmin.JSMinFilter',))
COMPRESS_CSS = getattr(settings, 'COMPRESS_CSS', {}) COMPRESS_CSS = getattr(settings, 'COMPRESS_CSS', {})
......
...@@ -7,19 +7,15 @@ from django import template ...@@ -7,19 +7,15 @@ from django import template
from django.conf import settings as django_settings from django.conf import settings as django_settings
from compress.conf import settings from compress.conf import settings
from compress.utils import media_root from compress.utils import media_root, needs_update, filter_css, filter_js
register = template.Library() register = template.Library()
def render_common(template_name, obj, filename): def render_common(template_name, obj, filename):
if 'extra_context' in obj:
context = obj['extra_context']
else:
context = {}
url = django_settings.MEDIA_URL + urlquote(filename) url = django_settings.MEDIA_URL + urlquote(filename)
if settings.COMPRESS and 'bump_filename' in obj and obj['bump_filename']: if settings.COMPRESS and obj.get('bump_filename', False):
try: try:
url += '?%d' % os.stat(media_root(filename)).st_mtime url += '?%d' % os.stat(media_root(filename)).st_mtime
except: except:
...@@ -28,24 +24,16 @@ def render_common(template_name, obj, filename): ...@@ -28,24 +24,16 @@ def render_common(template_name, obj, filename):
# this will (probably) make the problem visible, while not aborting the entire rendering # this will (probably) make the problem visible, while not aborting the entire rendering
return '' return ''
context.update(url=url) context = obj.get('extra_context', {})
context['url'] = url
return template.loader.render_to_string(template_name, context) return template.loader.render_to_string(template_name, context)
def render_css(css, filename): def render_css(css, filename):
try: return render_common(css.get('template_name', 'compress/css.html'), css, filename)
template_name = css['template_name']
except KeyError:
template_name = 'compress/css.html'
return render_common(template_name, css, filename)
def render_js(js, filename): def render_js(js, filename):
try: return render_common(js.get('template_name', 'compress/js.html'), js, filename)
template_name = js['template_name']
except KeyError:
template_name = 'compress/js.html'
return render_common(template_name, js, filename)
class CompressedCSSNode(template.Node): class CompressedCSSNode(template.Node):
def __init__(self, name): def __init__(self, name):
...@@ -60,6 +48,10 @@ class CompressedCSSNode(template.Node): ...@@ -60,6 +48,10 @@ class CompressedCSSNode(template.Node):
return '' # fail silently, do not return anything if an invalid group is specified return '' # fail silently, do not return anything if an invalid group is specified
if settings.COMPRESS: if settings.COMPRESS:
if settings.COMPRESS_AUTO_TEMPLATES and needs_update(css['output_filename'], css['source_filenames']):
filter_css(css)
return render_css(css, css['output_filename']) return render_css(css, css['output_filename'])
else: else:
# output source files # output source files
...@@ -82,6 +74,10 @@ class CompressedJSNode(template.Node): ...@@ -82,6 +74,10 @@ class CompressedJSNode(template.Node):
return '' # fail silently, do not return anything if an invalid group is specified return '' # fail silently, do not return anything if an invalid group is specified
if settings.COMPRESS: if settings.COMPRESS:
if settings.COMPRESS_AUTO_TEMPLATES and needs_update(js['output_filename'], js['source_filenames']):
filter_js(js)
return render_js(js, js['output_filename']) return render_js(js, js['output_filename'])
else: else:
# output source files # output source files
...@@ -95,7 +91,7 @@ def compressed_css(parser, token): ...@@ -95,7 +91,7 @@ def compressed_css(parser, token):
try: try:
tag_name, name = token.split_contents() tag_name, name = token.split_contents()
except ValueError: except ValueError:
raise template.TemplateSyntaxError, '%r requires exactly one argument, being the name of the COMPRESS_CSS setting' raise template.TemplateSyntaxError, '%r requires exactly one argument: the name of a group in the COMPRESS_CSS setting' % token.split_contents()[0]
return CompressedCSSNode(name) return CompressedCSSNode(name)
compressed_css = register.tag(compressed_css) compressed_css = register.tag(compressed_css)
...@@ -105,7 +101,7 @@ def compressed_js(parser, token): ...@@ -105,7 +101,7 @@ def compressed_js(parser, token):
try: try:
tag_name, name = token.split_contents() tag_name, name = token.split_contents()
except ValueError: except ValueError:
raise template.TemplateSyntaxError, '%r requires exactly one argument, being the name of the COMPRESS_JS setting' raise template.TemplateSyntaxError, '%r requires exactly one argument: the name of a group in the COMPRESS_JS setting' % token.split_contents()[0]
return CompressedJSNode(name) return CompressedJSNode(name)
compressed_js = register.tag(compressed_js) compressed_js = register.tag(compressed_js)
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