Added custom resources to selected skin
authorThierry Florac <tflorac@ulthar.net>
Wed, 12 Dec 2018 10:14:07 +0100
changeset 487 b6218937c5f9
parent 486 48b7cea0d903
child 488 efdf19b8f453
Added custom resources to selected skin
src/pyams_skin/skin.py
--- a/src/pyams_skin/skin.py	Wed Dec 12 10:13:38 2018 +0100
+++ b/src/pyams_skin/skin.py	Wed Dec 12 10:14:07 2018 +0100
@@ -12,25 +12,24 @@
 
 __docformat__ = 'restructuredtext'
 
-# import standard library
 import logging
 logger = logging.getLogger('PyAMS (skin)')
 
-# import interfaces
-from pyams_skin.interfaces import ISkin, ISkinnable, IUserSkinnable, SkinChangedEvent
-from pyams_skin.layer import IBaseLayer, IPyAMSLayer
-from pyams_utils.interfaces.site import ISiteRoot
-from zope.traversing.interfaces import IBeforeTraverseEvent
-
-# import packages
-from pyams_utils.registry import utility_config
-from pyams_utils.traversing import get_parent
-from pyams_utils.zodb import volatile_property
 from pyramid.events import subscriber
 from pyramid.threadlocal import get_current_request
 from pyramid_zope_request import PyramidPublisherRequest
 from zope.interface import implementer, directlyProvidedBy, directlyProvides
 from zope.schema.fieldproperty import FieldProperty
+from zope.traversing.interfaces import IBeforeTraverseEvent
+
+from pyams_file.interfaces import DELETED_FILE
+from pyams_file.property import FileProperty
+from pyams_skin.interfaces import ISkin, ISkinnable, IUserSkinnable, SkinChangedEvent
+from pyams_skin.layer import IBaseLayer, IPyAMSLayer
+from pyams_utils.interfaces.site import ISiteRoot
+from pyams_utils.registry import utility_config
+from pyams_utils.traversing import get_parent
+from pyams_utils.zodb import volatile_property
 
 from pyams_skin import _
 
@@ -42,6 +41,10 @@
     _inherit_skin = FieldProperty(ISkinnable['inherit_skin'])
     _skin = FieldProperty(IUserSkinnable['skin'])
 
+    _custom_stylesheet = FileProperty(ISkinnable['custom_stylesheet'])
+    _editor_stylesheet = FileProperty(ISkinnable['editor_stylesheet'])
+    _custom_script = FileProperty(ISkinnable['custom_script'])
+
     @property
     def can_inherit_skin(self):
         return get_parent(self, ISkinnable, allow_context=False) is not None
@@ -64,6 +67,14 @@
     def no_inherit_skin(self, value):
         self.inherit_skin = not bool(value)
 
+    @volatile_property
+    def skin_parent(self):
+        if (not self._inherit_skin) and self.skin:
+            return self
+        parent = get_parent(self, ISkinnable, allow_context=False)
+        if parent is not None:
+            return parent.skin_parent
+
     @property
     def skin(self):
         if not self.inherit_skin:
@@ -77,13 +88,47 @@
             self._skin = value
         del self.skin_parent
 
-    @volatile_property
-    def skin_parent(self):
-        if (not self._inherit_skin) and self.skin:
-            return self
-        parent = get_parent(self, ISkinnable, allow_context=False)
-        if parent is not None:
-            return parent.skin_parent
+    @property
+    def custom_stylesheet(self):
+        if not self.inherit_skin:
+            return self._custom_stylesheet
+        else:
+            return self.skin_parent.custom_stylesheet
+
+    @custom_stylesheet.setter
+    def custom_stylesheet(self, value):
+        if not self.inherit_skin:
+            self._custom_stylesheet = value
+            if value and (value is not DELETED_FILE):
+                self._custom_stylesheet.content_type = 'text/css'
+
+    @property
+    def editor_stylesheet(self):
+        if not self.inherit_skin:
+            return self._editor_stylesheet
+        else:
+            return self.skin_parent.editor_stylesheet
+
+    @editor_stylesheet.setter
+    def editor_stylesheet(self, value):
+        if not self.inherit_skin:
+            self._editor_stylesheet = value
+            if value and (value is not DELETED_FILE):
+                self._editor_stylesheet.content_type = 'text/css'
+
+    @property
+    def custom_script(self):
+        if not self.inherit_skin:
+            return self._custom_script
+        else:
+            return self.skin_parent.custom_script
+
+    @custom_script.setter
+    def custom_script(self, value):
+        if not self.inherit_skin:
+            self._custom_script = value
+            if value and (value is not DELETED_FILE):
+                self._custom_script.content_type = 'text/javascript'
 
     def get_skin(self, request=None):
         parent = self.skin_parent