src/pyams_skin/resources.py
changeset 557 bca7a7e058a3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_skin/resources.py	Thu Feb 13 11:43:31 2020 +0100
@@ -0,0 +1,82 @@
+#
+# Copyright (c) 2008-2015 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'
+
+from pyramid.interfaces import IRequest
+from zope.dublincore.interfaces import IZopeDublinCore
+from zope.interface import Interface
+
+from pyams_skin import myams
+from pyams_skin.interfaces import ISkinnable
+from pyams_skin.interfaces.resources import IResources
+from pyams_skin.layer import IPyAMSUserLayer
+from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyams_utils.fanstatic import ExternalResource
+from pyams_utils.interfaces.tales import ITALESExtension
+from pyams_utils.traversing import get_parent
+from pyams_utils.url import absolute_url
+
+
+@adapter_config(context=(Interface, IRequest, Interface), provides=IResources)
+class ResourcesAdapter(ContextRequestViewAdapter):
+    """Get context resources"""
+
+    resources = (myams,)
+
+
+@adapter_config(name='custom-skin', context=(Interface, IPyAMSUserLayer, Interface), provides=IResources)
+class CustomSkinResourcesAdapter(ContextRequestViewAdapter):
+    """Custom skin resources adapter"""
+
+    weight = 999
+
+    @property
+    def resources(self):
+        main_resources = self.request.registry.queryMultiAdapter((self.context, self.request, self.view), IResources)
+        if main_resources is not None:
+            main_resource = main_resources.resources[-1]
+            library = main_resource.library
+            parent_resources = (main_resource,)
+            skin_parent = get_parent(self.request.context, ISkinnable).skin_parent
+            custom_css = skin_parent.custom_stylesheet
+            if custom_css:
+                modified = IZopeDublinCore(custom_css).modified
+                custom_css_url = absolute_url(custom_css, self.request, query={'_': modified.timestamp()})
+                resource = library.known_resources.get(custom_css_url)
+                if resource is None:
+                    resource = ExternalResource(library, custom_css_url, resource_type='css',
+                                                depends=parent_resources)
+                yield resource
+            custom_js = skin_parent.custom_script
+            if custom_js:
+                modified = IZopeDublinCore(custom_js).modified
+                custom_js_url = absolute_url(custom_js, self.request, query={'_': modified.timestamp()})
+                resource = library.known_resources.get(custom_js_url)
+                if resource is None:
+                    resource = ExternalResource(library, custom_js_url, resource_type='js',
+                                                depends=parent_resources, bottom=True)
+                yield resource
+
+
+@adapter_config(name='resources', context=(Interface, IRequest, Interface), provides=ITALESExtension)
+class ResourcesTalesExtension(ContextRequestViewAdapter):
+    """extension:resources TALES extension"""
+
+    def render(self, context=None):
+        if context is None:
+            context = self.context
+        for name, adapter in sorted(self.request.registry.getAdapters((context, self.request, self.view), IResources),
+                                    key=lambda x: getattr(x[1], 'weight', 0)):
+            for resource in adapter.resources:
+                resource.need()
+        return ''