Added "timestamp" TALES extension
authorThierry Florac <thierry.florac@onf.fr>
Wed, 27 Jun 2018 12:23:41 +0200
changeset 207 c164b59809c8
parent 206 5ac9be953a8f
child 208 7c71a08ff111
child 272 e2a747608ef6
Added "timestamp" TALES extension
src/pyams_utils/date.py
--- a/src/pyams_utils/date.py	Wed Jun 27 12:23:22 2018 +0200
+++ b/src/pyams_utils/date.py	Wed Jun 27 12:23:41 2018 +0200
@@ -17,11 +17,15 @@
 from datetime import datetime
 
 # import interfaces
+from pyams_utils.interfaces.tales import ITALESExtension
+from zope.dublincore.interfaces import IZopeDublinCore
 
 # import packages
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
 from pyams_utils.request import check_request
 from pyams_utils.timezone import gmtime, tztime
 from zope.datetime import parseDatetimetz
+from zope.interface import Interface
 
 from pyams_utils import _
 
@@ -245,3 +249,20 @@
                     return translate(_("%d minutes")) % minutes
                 else:
                     return translate(_("%d seconds")) % delta.seconds
+
+
+@adapter_config(name='timestamp', context=(Interface, Interface, Interface), provides=ITALESExtension)
+class TimestampTalesAdapter(ContextRequestViewAdapter):
+    """extension:timestamp(context) TALES adapter
+
+    A PyAMS TALES extension to get timestamp based on last context modification date.
+    """
+
+    def render(self, context=None):
+        if context is None:
+            context = self.request.context
+        dc = IZopeDublinCore(context, None)
+        if dc is None:
+            return datetime.utcnow().timestamp()
+        else:
+            return dc.modified.timestamp()