diff --git a/docs/index.rst b/docs/index.rst index 9c873611..5608e979 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -741,7 +741,7 @@ Theme debugging ~~~~~~~~~~~~~~~ When Zope is in development mode (e.g. running in the foreground in a console with ``bin/instance fg``), -the theme will be re-compiled on each request (unless the environment variable ``DIAZO_ALWAYS_CACHE_RULES`` is set). +the theme will be re-compiled on each request (unless the environment variable ``DIAZO_ALWAYS_CACHE_RULES`` is set to a truthy value). To set the environment variable ``DIAZO_ALWAYS_CACHE_RULES``, you can use buildout:: @@ -755,6 +755,8 @@ you can use buildout:: In non-development mode or when the environment variable ``DIAZO_ALWAYS_CACHE_RULES`` is set, the theme is compiled once when first accessed, and then only re-compiled the control panel values are changed. +You can also disable caching without removing the environment variable by setting it to a value which evaluates to ``False``, like ``DIAZO_ALWAYS_CACHE_RULES false`` + Also, in development mode (even when the environment variable ``DIAZO_ALWAYS_CACHE_RULES`` is set), it is possible to temporarily disable the theme by appending a query string parameter ``diazo.off=1``. diff --git a/news/245.bugfix b/news/245.bugfix new file mode 100644 index 00000000..3b45f8fd --- /dev/null +++ b/news/245.bugfix @@ -0,0 +1,2 @@ +Allow to disable rule caching by setting "DIAZO_ALWAYS_CACHE_RULES" to a value which evaluates to False. +[thet] diff --git a/src/plone/app/theming/policy.py b/src/plone/app/theming/policy.py index d837ff22..04da4b8a 100644 --- a/src/plone/app/theming/policy.py +++ b/src/plone/app/theming/policy.py @@ -76,8 +76,7 @@ def isThemeEnabled(self, settings=None): return False # Check for diazo.off request parameter - true_vals = ("1", "y", "yes", "t", "true") - if debug_mode and self.request.get("diazo.off", "").lower() in true_vals: + if debug_mode and utils.is_truthy(self.request.get("diazo.off", False)): return False if not settings: diff --git a/src/plone/app/theming/tests/test_utils.py b/src/plone/app/theming/tests/test_utils.py index e3c4330c..5542a32d 100644 --- a/src/plone/app/theming/tests/test_utils.py +++ b/src/plone/app/theming/tests/test_utils.py @@ -482,6 +482,32 @@ def _open_zipfile(self, filename): path = os.path.join(os.path.dirname(__file__), "zipfiles", filename) return open(path, "rb") + def test_is_truthy(self): + """Test the `is_truthy` utility function with different inputs.""" + from plone.app.theming.utils import is_truthy + + self.assertTrue(is_truthy(True)) + self.assertTrue(is_truthy(1)) + self.assertTrue(is_truthy("1")) + self.assertTrue(is_truthy("TRUE")) + self.assertTrue(is_truthy("tRUE")) + self.assertTrue(is_truthy("true")) + self.assertTrue(is_truthy("y")) + self.assertTrue(is_truthy("Y")) + self.assertTrue(is_truthy("yEs")) + self.assertTrue(is_truthy("yes")) + + self.assertFalse(is_truthy(None)) + self.assertFalse(is_truthy(False)) + self.assertFalse(is_truthy(0)) + self.assertFalse(is_truthy("0")) + self.assertFalse(is_truthy("FALSE")) + self.assertFalse(is_truthy("fALSE")) + self.assertFalse(is_truthy("false")) + self.assertFalse(is_truthy("n")) + self.assertFalse(is_truthy("NO")) + self.assertFalse(is_truthy("no")) + def test_extractThemeInfo_default_rules(self): with self._open_zipfile("default_rules.zip") as fp: zf = zipfile.ZipFile(fp) diff --git a/src/plone/app/theming/transform.py b/src/plone/app/theming/transform.py index 3f74c015..2c872b1f 100644 --- a/src/plone/app/theming/transform.py +++ b/src/plone/app/theming/transform.py @@ -1,12 +1,8 @@ from App.config import getConfiguration from lxml import etree from os import environ +from plone.app.theming import utils from plone.app.theming.interfaces import IThemingLayer -from plone.app.theming.utils import compileThemeTransform -from plone.app.theming.utils import findContext -from plone.app.theming.utils import getParser -from plone.app.theming.utils import prepareThemeParameters -from plone.app.theming.utils import theming_policy from plone.app.theming.zmi import patch_zmi from plone.transformchain.interfaces import ITransform from repoze.xmliter.utils import getHTMLSerializer @@ -55,13 +51,13 @@ def develop_theme(self): return False if self.debug_theme(): return True - if environ.get("DIAZO_ALWAYS_CACHE_RULES"): + if utils.is_truthy(environ.get("DIAZO_ALWAYS_CACHE_RULES", False)): return False return True def setupTransform(self, runtrace=False): debug_mode = self.develop_theme() - policy = theming_policy(self.request) + policy = utils.theming_policy(self.request) # Obtain settings. Do nothing if not found settings = policy.getSettings() @@ -89,7 +85,7 @@ def setupTransform(self, runtrace=False): readNetwork = settings.readNetwork parameterExpressions = settings.parameterExpressions - transform = compileThemeTransform( + transform = utils.compileThemeTransform( rules, absolutePrefix, readNetwork, @@ -105,7 +101,7 @@ def setupTransform(self, runtrace=False): return transform def getSettings(self): - return theming_policy(self.request).getSettings() + return utils.theming_policy(self.request).getSettings() def parseTree(self, result): contentType = self.request.response.getHeader("Content-Type") @@ -146,7 +142,7 @@ def transformUnicode(self, result, encoding): def transformIterable(self, result, encoding): """Apply the transform if required""" # Obtain settings. Do nothing if not found - policy = theming_policy(self.request) + policy = utils.theming_policy(self.request) if not policy.isThemeEnabled(): return None settings = policy.getSettings() @@ -177,8 +173,11 @@ def transformIterable(self, result, encoding): cache = policy.getCache() parameterExpressions = settings.parameterExpressions or {} - params = prepareThemeParameters( - findContext(self.request), self.request, parameterExpressions, cache + params = utils.prepareThemeParameters( + utils.findContext(self.request), + self.request, + parameterExpressions, + cache, ) transformed = transform(result.tree, **params) @@ -198,14 +197,15 @@ def transformIterable(self, result, encoding): # Add debug information to end of body body = result.tree.xpath("/html/body")[0] debug_url = ( - findContext(self.request).portal_url() + "/++resource++diazo-debug" + utils.findContext(self.request).portal_url() + + "/++resource++diazo-debug" ) body.insert( -1, generate_debug_html( debug_url, rules=settings.rules, - rules_parser=getParser("rules", settings.readNetwork), + rules_parser=utils.getParser("rules", settings.readNetwork), error_log=error_log, ), ) diff --git a/src/plone/app/theming/utils.py b/src/plone/app/theming/utils.py index 58041df4..7309f14b 100644 --- a/src/plone/app/theming/utils.py +++ b/src/plone/app/theming/utils.py @@ -43,6 +43,11 @@ LOGGER = logging.getLogger("plone.app.theming") +def is_truthy(value): + """Return `true`, if value is truthy.""" + return value and str(value).lower() in ("1", "y", "yes", "t", "true") + + @implementer(INoRequest) class NoRequest: """Fallback to enable querying for the policy adapter