Added "cache_key" TALES extension
authorThierry Florac <thierry.florac@onf.fr>
Wed, 27 Jun 2018 12:23:22 +0200
changeset 206 5ac9be953a8f
parent 205 4eff8c9109eb
child 207 c164b59809c8
Added "cache_key" TALES extension
src/pyams_utils/cache.py
--- a/src/pyams_utils/cache.py	Fri Jun 22 12:50:19 2018 +0200
+++ b/src/pyams_utils/cache.py	Wed Jun 27 12:23:22 2018 +0200
@@ -18,9 +18,11 @@
 # import interfaces
 from persistent.interfaces import IPersistent
 from pyams_utils.interfaces import ICacheKeyValue
+from pyams_utils.interfaces.tales import ITALESExtension
 
 # import packages
-from pyams_utils.adapter import adapter_config
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from zope.interface import Interface
 
 
 @adapter_config(context=object, provides=ICacheKeyValue)
@@ -39,3 +41,16 @@
         return str(int.from_bytes(obj._p_oid, byteorder='big'))
     else:  # unsaved object
         return str(id(obj))
+
+
+@adapter_config(name='cache_key', context=(Interface, Interface, Interface), provides=ITALESExtension)
+class CacheKeyTalesExtension(ContextRequestViewAdapter):
+    """extension:cache_key(context) TALES extension
+
+    A PyAMS TALES extension which allows to render cache key value for a given context.
+    """
+
+    def render(self, context=None):
+        if context is None:
+            context = self.request.context
+        return ICacheKeyValue(context)