Added generic adapters and TALES adapters
authorThierry Florac <thierry.florac@onf.fr>
Thu, 12 Jul 2018 11:48:23 +0200
changeset 88 f3beaff7c4fe
parent 87 c598d2f814e3
child 89 62c88c2ad06a
Added generic adapters and TALES adapters
src/pyams_default_theme/shared/common/__init__.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/shared/common/__init__.py	Thu Jul 12 11:48:23 2018 +0200
@@ -0,0 +1,112 @@
+#
+# Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.illustration import ILinkIllustration, IIllustration
+from pyams_content.interfaces import IBaseContent
+from pyams_content.shared.common.interfaces import IWfSharedContent
+from pyams_default_theme.interfaces import IContentNavigationTitle, IContentTag, IContentDate, \
+    IContentNavigationIllustration
+from pyams_i18n.interfaces import II18n
+from pyams_skin.layer import IPyAMSUserLayer
+from pyams_utils.interfaces.tales import ITALESExtension
+from pyams_workflow.interfaces import IWorkflowPublicationInfo
+
+# import packages
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyams_utils.date import format_date, SH_DATE_FORMAT
+from zope.interface import Interface
+
+
+@adapter_config(context=(IBaseContent, IPyAMSUserLayer), provides=IContentNavigationTitle)
+def shared_content_navigation_title(context, request):
+    """Default content navigation title adapter"""
+    return II18n(context).query_attribute('title', request=request)
+
+
+@adapter_config(name='pyams_title',
+                context=(Interface, IPyAMSUserLayer, Interface),
+                provides=ITALESExtension)
+class PyAMSContentTitleTALESExtension(ContextRequestViewAdapter):
+    """PyAMS content title TALES extension"""
+
+    def render(self, context=None):
+        if context is None:
+            context = self.context
+        return self.request.registry.queryMultiAdapter((context, self.request), IContentNavigationTitle)
+
+
+@adapter_config(context=(IWfSharedContent, IPyAMSUserLayer), provides=IContentTag)
+def shared_content_tag_adapter(context, request):
+    """Default shared content tag adapter"""
+    translate = request.localizer.translate
+    return translate(context.content_name)
+
+
+@adapter_config(name='pyams_tag',
+                context=(Interface, IPyAMSUserLayer, Interface),
+                provides=ITALESExtension)
+class PyAMSContentTagTALESExtension(ContextRequestViewAdapter):
+    """PyAMS content tag TALES extension"""
+
+    def render(self, context=None):
+        if context is None:
+            context = self.context
+        return self.request.registry.queryMultiAdapter((context, self.request), IContentTag)
+
+
+@adapter_config(context=(IWfSharedContent, IPyAMSUserLayer), provides=IContentDate)
+def shared_content_date_adapter(context, request):
+    """Default shared content date adapter"""
+    publication_info = IWorkflowPublicationInfo(context, None)
+    if publication_info is not None:
+        return format_date(publication_info.visible_publication_date,
+                           format=SH_DATE_FORMAT, request=request)
+
+
+@adapter_config(name='pyams_date',
+                context=(Interface, IPyAMSUserLayer, Interface),
+                provides=ITALESExtension)
+class PyAMSContentDateTALESExtension(ContextRequestViewAdapter):
+    """PyAMS content date TALES extension"""
+
+    def render(self, context=None):
+        if context is None:
+            context = self.context
+        return self.request.registry.queryMultiAdapter((context, self.request), IContentDate)
+
+
+@adapter_config(context=(IBaseContent, IPyAMSUserLayer), provides=IContentNavigationIllustration)
+def base_content_navigation_illustration_adapter(context, request):
+    """Default content navigation illustration adapter"""
+    illustration = ILinkIllustration(context, None)
+    if not (illustration and illustration.has_data()):
+        illustration = IIllustration(context, None)
+    if illustration and illustration.has_data():
+        return illustration
+
+
+@adapter_config(name='pyams_illustration',
+                context=(Interface, IPyAMSUserLayer, Interface),
+                provides=ITALESExtension)
+class PyAMSContentIllustrationTALESExtension(ContextRequestViewAdapter):
+    """PyAMS content illustration TALES extension"""
+
+    def render(self, context=None):
+        if context is None:
+            context = self.context
+        return self.request.registry.queryMultiAdapter((context, self.request), IContentNavigationIllustration)