Updated skin interfaces and management classes
authorThierry Florac <thierry.florac@onf.fr>
Mon, 10 Oct 2016 15:27:10 +0200
changeset 156 b8b688fc964e
parent 155 cd3ab32436f0
child 157 c716d9f34203
Updated skin interfaces and management classes
src/pyams_skin/interfaces/__init__.py
src/pyams_skin/locales/fr/LC_MESSAGES/pyams_skin.mo
src/pyams_skin/locales/fr/LC_MESSAGES/pyams_skin.po
src/pyams_skin/locales/pyams_skin.pot
src/pyams_skin/skin.py
--- a/src/pyams_skin/interfaces/__init__.py	Mon Oct 10 11:32:08 2016 +0200
+++ b/src/pyams_skin/interfaces/__init__.py	Mon Oct 10 15:27:10 2016 +0200
@@ -20,7 +20,7 @@
 
 # import packages
 from pyams_template.template import layout_config
-from zope.interface import implementer, Interface, Attribute
+from zope.interface import implementer, invariant, Interface, Attribute, Invalid
 from zope.configuration.fields import GlobalInterface
 from zope.schema import Text, TextLine, Choice, Int, Bool
 
@@ -62,9 +62,15 @@
 
     skin_parent = Attribute("Skin parent (local or inherited)")
 
-    skin = Choice(title=_("Presentation skin"),
-                  description=_("This skin will be used to handle presentation templates"),
-                  vocabulary='PyAMS user skins')
+    skin = Choice(title=_("Custom graphic theme"),
+                  description=_("This theme will be used to handle graphic design (colors and images)"),
+                  vocabulary='PyAMS user skins',
+                  required=False)
+
+    @invariant
+    def check_skin(self):
+        if not (self.skin or self.inherit_skin):
+            raise Invalid(_("You must select a custom skin or inherit from parent!"))
 
     def get_skin(self, request=None):
         """Get skin matching this content"""
Binary file src/pyams_skin/locales/fr/LC_MESSAGES/pyams_skin.mo has changed
--- a/src/pyams_skin/locales/fr/LC_MESSAGES/pyams_skin.po	Mon Oct 10 11:32:08 2016 +0200
+++ b/src/pyams_skin/locales/fr/LC_MESSAGES/pyams_skin.po	Mon Oct 10 15:27:10 2016 +0200
@@ -5,7 +5,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE 1.0\n"
-"POT-Creation-Date: 2016-10-10 11:29+0200\n"
+"POT-Creation-Date: 2016-10-10 15:01+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -205,19 +205,25 @@
 
 #: src/pyams_skin/interfaces/__init__.py:58
 msgid "Inherit parent skin?"
-msgstr "Hériter du skin du parent ?"
+msgstr "Hériter du thème du parent ?"
 
 #: src/pyams_skin/interfaces/__init__.py:59
 msgid "Should we reuse parent skin?"
-msgstr "Si 'oui', le même skin qu'au niveau du parent sera utilisé"
+msgstr "Si 'oui', le même thème que celui du niveau parent sera utilisé"
 
 #: src/pyams_skin/interfaces/__init__.py:65
-msgid "Presentation skin"
-msgstr "Modèle de présentation"
+msgid "Custom graphic theme"
+msgstr "Choisir un thème spécifique"
 
 #: src/pyams_skin/interfaces/__init__.py:66
-msgid "This skin will be used to handle presentation templates"
-msgstr "Ce skin sera utilisé pour prendre en charge le modèle de présentation"
+msgid "This theme will be used to handle graphic design (colors and images)"
+msgstr ""
+"Ce thème sera utilisé pour prendre en charge le modèle de présentation "
+"(palette de couleurs et images)"
+
+#: src/pyams_skin/interfaces/__init__.py:73
+msgid "You must select a custom skin or inherit from parent!"
+msgstr "Vous devez choisir un thème spécifique ou hériter de celui du niveau parent"
 
 #: src/pyams_skin/interfaces/extension.py:32
 msgid "Front-office only"
@@ -290,6 +296,9 @@
 msgid "Mode(s) in which reports are activated"
 msgstr "Mode(s) pour le(s)quel(s) le service est activé"
 
+#~ msgid "Presentation skin"
+#~ msgstr "Modèle de présentation"
+
 #~ msgid "Logout"
 #~ msgstr "Déconnexion"
 
--- a/src/pyams_skin/locales/pyams_skin.pot	Mon Oct 10 11:32:08 2016 +0200
+++ b/src/pyams_skin/locales/pyams_skin.pot	Mon Oct 10 15:27:10 2016 +0200
@@ -6,7 +6,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE 1.0\n"
-"POT-Creation-Date: 2016-10-10 11:29+0200\n"
+"POT-Creation-Date: 2016-10-10 15:01+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -203,11 +203,15 @@
 msgstr ""
 
 #: ./src/pyams_skin/interfaces/__init__.py:65
-msgid "Presentation skin"
+msgid "Custom graphic theme"
 msgstr ""
 
 #: ./src/pyams_skin/interfaces/__init__.py:66
-msgid "This skin will be used to handle presentation templates"
+msgid "This theme will be used to handle graphic design (colors and images)"
+msgstr ""
+
+#: ./src/pyams_skin/interfaces/__init__.py:73
+msgid "You must select a custom skin or inherit from parent!"
 msgstr ""
 
 #: ./src/pyams_skin/interfaces/extension.py:32
--- a/src/pyams_skin/skin.py	Mon Oct 10 11:32:08 2016 +0200
+++ b/src/pyams_skin/skin.py	Mon Oct 10 15:27:10 2016 +0200
@@ -45,8 +45,7 @@
     """Skinnable content base class"""
 
     _inherit_skin = FieldProperty(ISkinnable['inherit_skin'])
-
-    skin = FieldProperty(IUserSkinnable['skin'])
+    _skin = FieldProperty(IUserSkinnable['skin'])
 
     @property
     def can_inherit_skin(self):
@@ -58,15 +57,21 @@
 
     @inherit_skin.setter
     def inherit_skin(self, value):
-        if value and not self.can_inherit_skin:
-            value = False
-        self._inherit_skin = value
+        self._inherit_skin = value if self.can_inherit_skin else False
+
+    @property
+    def skin(self):
+        return self._skin
+
+    @skin.setter
+    def skin(self, value):
+        self._skin = None if self.inherit_skin else value
 
     @property
     def skin_parent(self):
         if (not self._inherit_skin) and self.skin:
             return self
-        parent = get_parent(self, ISkinnable)
+        parent = get_parent(self, ISkinnable, allow_context=False)
         if parent is not None:
             return parent.skin_parent
 
@@ -113,17 +118,16 @@
 def handle_content_skin(event):
     """Apply skin when traversing skinnable object"""
     request = event.request
-    try:
-        skin = event.object.get_skin(request)
-    except NotImplementedError:
-        pass
-    else:
-        if skin is not None:
-            apply_skin(request, skin)
+    skin = event.object.get_skin(request)
+    if skin is not None:
+        apply_skin(request, skin)
 
 
 @subscriber(IBeforeTraverseEvent, context_selector=ISiteRoot)
 def handle_root_skin(event):
     """Apply skin when traversing site root"""
-    if not ISkinnable.providedBy(event.object):
+    context = event.object
+    if not ISkinnable.providedBy(context):
         apply_skin(event.request, PyAMSSkin)
+    elif context.skin is None:
+        apply_skin(event.request, PyAMSSkin)