--- a/src/pyams_skin/interfaces/resources.py Mon Dec 10 15:49:32 2018 +0100
+++ b/src/pyams_skin/interfaces/resources.py Wed Dec 12 10:08:24 2018 +0100
@@ -12,20 +12,15 @@
__docformat__ = 'restructuredtext'
-# import standard library
-
-# import interfaces
-
-# import packages
-from zope.interface import Interface
+from zope.interface import Interface, Attribute
class IResources(Interface):
"""Get list of CSS and Javascript resources associated with given context"""
- def get_resources(self):
- """Include page resources
+ resources = Attribute("Resources to include")
+ """Iterable of resources to include into page
- The best way to handle resources is to use Fanstatic to automatically
- include CSS and Javascript tags
- """
+ The best way to handle resources is to use Fanstatic to automatically
+ include CSS and Javascript tags
+ """
--- a/src/pyams_skin/resources.py Mon Dec 10 15:49:32 2018 +0100
+++ b/src/pyams_skin/resources.py Wed Dec 12 10:08:24 2018 +0100
@@ -12,26 +12,68 @@
__docformat__ = 'restructuredtext'
-
-# import standard library
+from fanstatic import get_library_registry
+from pyramid.interfaces import IRequest
+from zope.dublincore.interfaces import IZopeDublinCore
+from zope.interface import Interface
-# import interfaces
+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 pyramid.interfaces import IRequest
+from pyams_utils.traversing import get_parent
+from pyams_utils.url import absolute_url
-# import packages
-from pyams_skin import myams
-from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
-from zope.interface import Interface
+for library in get_library_registry().values():
+ break
+else:
+ try:
+ from pyams_skin import library
+ except ImportError:
+ from pyams_default_theme import library
@adapter_config(context=(Interface, IRequest, Interface), provides=IResources)
class ResourcesAdapter(ContextRequestViewAdapter):
"""Get context resources"""
- def get_resources(self):
- myams.need()
+ 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:
+ parent_resources = (main_resources.resources[-1],)
+ skinnable = get_parent(self.request.context, ISkinnable)
+ skin_parent = skinnable.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)
@@ -41,6 +83,8 @@
def render(self, context=None):
if context is None:
context = self.context
- for name, adapter in self.request.registry.getAdapters((context, self.request, self.view), IResources):
- adapter.get_resources()
+ 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 ''