Added COMPRESS_DEBUG_TOGGLE setting for easier debugging. Fixes #19.

This commit is contained in:
Jannis Leidel
2011-04-12 13:41:18 +02:00
parent 08b0e3fab1
commit 05bcb2f25d
4 changed files with 50 additions and 3 deletions

View File

@@ -9,6 +9,8 @@ class CompressorSettings(AppSettings):
ENABLED = not settings.DEBUG
# Allows changing verbosity from the settings.
VERBOSE = False
# GET variable that disables compressor e.g. "nocompress"
DEBUG_TOGGLE = "None"
# the backend to use when parsing the JavaScript or Stylesheet files
PARSER = 'compressor.parser.BeautifulSoupParser'
OUTPUT_DIR = 'CACHE'

View File

@@ -48,13 +48,21 @@ class CompressorNode(template.Node):
def cache_key(self, compressor):
return "%s.%s.%s" % (compressor.cachekey, self.mode, self.kind)
def render(self, context, forced=False):
def render(self, context, forced=False, debug=False):
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:
debug = settings.COMPRESS_DEBUG_TOGGLE in request.GET
if (settings.COMPRESS_ENABLED and
settings.COMPRESS_OFFLINE) and not forced:
settings.COMPRESS_OFFLINE) and not forced and not debug:
content = cache.get(get_offline_cachekey(self.nodelist))
if content:
return content
content = self.nodelist.render(context)
if debug:
return content
compressor = self.compressor_cls(content)
cachekey = self.cache_key(compressor)
output = self.cache_get(cachekey)

View File

@@ -25,7 +25,8 @@ class CompressorTestCase(TestCase):
def setUp(self):
settings.COMPRESS_ENABLED = True
settings.PRECOMPILERS = {}
settings.COMPRESS_PRECOMPILERS = {}
settings.COMPRESS_DEBUG_TOGGLE = 'nocompress'
self.css = """
<link rel="stylesheet" href="/media/css/one.css" type="text/css" charset="utf-8">
<style type="text/css">p { border:5px solid green;}</style>
@@ -339,6 +340,18 @@ class TemplatetagTestCase(TestCase):
{% endcompress %}"""
self.assertRaises(TemplateSyntaxError, render, template, {})
def test_debug_toggle(self):
template = u"""{% load compress %}{% compress js %}
<script src="{{ MEDIA_URL }}js/one.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>
{% endcompress %}
"""
class MockDebugRequest(object):
GET = {settings.COMPRESS_DEBUG_TOGGLE: 'true'}
context = { 'MEDIA_URL': settings.COMPRESS_URL, 'request': MockDebugRequest()}
out = u"""<script src="/media/js/one.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>"""
self.assertEqual(out, render(template, context))
class StorageTestCase(TestCase):
def setUp(self):

View File

@@ -217,6 +217,30 @@ The amount of time (in seconds) to cache the modification timestamp of a
file. Disabled by default. Should be smaller than COMPRESS_REBUILD_TIMEOUT_
and COMPRESS_MINT_DELAY_.
COMPRESS_DEBUG_TOGGLE
^^^^^^^^^^^^^^^^^^^^^
:Default: None
The name of the GET variable that toggles the debug mode and prevents Django
Compressor from performing the actual compression. Only useful for debugging.
.. warning::
Don't use this option in production!
An easy convention is to only set it depending on the ``DEBUG`` setting::
if DEBUG:
COMPRESS_DEBUG_TOGGLE = 'whatever'
.. note::
This only works for pages that are rendered using the RequestContext_
and the ``django.core.context_processors.request`` context processor.
.. _RequestContext: http://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.RequestContext
COMPRESS_OFFLINE
^^^^^^^^^^^^^^^^