Skip to content

Commit

Permalink
Allow to disable rule caching by setting "DIAZO_ALWAYS_CACHE_RULES" t…
Browse files Browse the repository at this point in the history
…o a value which evaluates to False.
  • Loading branch information
thet committed Sep 4, 2024
1 parent fa8a76a commit a1c6314
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
4 changes: 3 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand All @@ -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``.

Expand Down
2 changes: 2 additions & 0 deletions news/245.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow to disable rule caching by setting "DIAZO_ALWAYS_CACHE_RULES" to a value which evaluates to False.
[thet]
3 changes: 1 addition & 2 deletions src/plone/app/theming/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions src/plone/app/theming/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions src/plone/app/theming/transform.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -89,7 +85,7 @@ def setupTransform(self, runtrace=False):
readNetwork = settings.readNetwork
parameterExpressions = settings.parameterExpressions

transform = compileThemeTransform(
transform = utils.compileThemeTransform(
rules,
absolutePrefix,
readNetwork,
Expand All @@ -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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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,
),
)
Expand Down
5 changes: 5 additions & 0 deletions src/plone/app/theming/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a1c6314

Please sign in to comment.