[Zope-CMF] Patch: CMFCore.skinnable.getCurrentSkinName

Laurence Rowe l at lrowe.co.uk
Sat Nov 5 16:01:51 EST 2005


Hi,

In the end I followed Tres' suggestion of adding the skin name to the 
tuple cached in the module global ('SKINDATA[tid]') as it was easier to 
write a test for it without REQUEST being involved.

I'd like for this to get into the next 1.5 release if possible as that 
will allow us to make a release of ResourceRegistries 1.1 for Plone.


Laurence
-------------- next part --------------
Index: Skinnable.py
===================================================================
--- Skinnable.py	(revision 39892)
+++ Skinnable.py	(working copy)
@@ -40,7 +40,7 @@
 _marker = []  # Create a new marker object.
 
 
-SKINDATA = {} # mapping thread-id -> (skinobj, ignore, resolve)
+SKINDATA = {} # mapping thread-id -> (skinobj, skinname, ignore, resolve)
 
 class SkinDataCleanup:
     """Cleanup at the end of the request."""
@@ -71,7 +71,7 @@
         if not name.startswith('_') and not name.startswith('aq_'):
             sd = SKINDATA.get(get_ident())
             if sd is not None:
-                ob, ignore, resolve = sd
+                ob, skinname, ignore, resolve = sd
                 if not ignore.has_key(name):
                     if resolve.has_key(name):
                         return resolve[name]
@@ -126,11 +126,29 @@
         skinobj = self.getSkin(skinname)
         if skinobj is not None:
             tid = get_ident()
-            SKINDATA[tid] = (skinobj, {}, {})
+            SKINDATA[tid] = (skinobj, skinname, {}, {})
             REQUEST = getattr(self, 'REQUEST', None)
             if REQUEST is not None:
                 REQUEST._hold(SkinDataCleanup(tid))
 
+    security.declarePublic('getCurrentSkinName')
+    def getCurrentSkinName(self):
+        '''Return the current skin name.
+        '''
+        sd = SKINDATA.get(get_ident())
+        if sd is not None:
+            ob, skinname, ignore, resolve = sd
+            if skinname is not None:
+                return skinname
+        # nothing here, so assume the default skin
+        sfn = self.getSkinsFolderName()
+        if sfn is not None:
+            sf = getattr(self, sfn, None)
+            if sf is not None:
+                return sf.getDefaultSkin()
+        # and if that fails...
+        return None
+
     security.declarePublic('clearCurrentSkin')
     def clearCurrentSkin(self):
         """Clear the current skin."""
Index: tests/test_SkinsTool.py
===================================================================
--- tests/test_SkinsTool.py	(revision 39892)
+++ tests/test_SkinsTool.py	(working copy)
@@ -102,10 +102,47 @@
         self.failUnless(paths.find('.svn') == -1)
 
 
+class SkinnableTests(TestCase):
+
+    def _makeOne(self):
+        from Products.CMFCore.SkinsTool import SkinsTool
+        from Products.CMFCore.Skinnable import SkinnableObjectManager
+
+        class TestSkinnableObjectManager(SkinnableObjectManager):
+            tool = SkinsTool()
+            # This is needed otherwise REQUEST is the string
+            # '<Special Object Used to Force Acquisition>'
+            REQUEST = None 
+            def getSkinsFolderName(self):
+                '''tool'''
+                return 'tool'
+        
+        return TestSkinnableObjectManager()
+    
+    def test_getCurrentSkinName(self):
+        som = self._makeOne()
+
+        pathA = ('foo, bar')
+        pathB = ('bar, foo')
+
+        som.tool.addSkinSelection('skinA', pathA)
+        som.tool.addSkinSelection('skinB', pathB)
+        
+        som.tool.manage_properties(default_skin='skinA')
+
+        # Expect the default skin name to be returned
+        self.failUnless(som.getCurrentSkinName() == 'skinA')
+
+        # after a changeSkin the new skin name should be returned
+        som.changeSkin('skinB')
+        self.failUnless(som.getCurrentSkinName() == 'skinB')
+        
+
 def test_suite():
     return TestSuite((
         makeSuite(SkinsContainerTests),
         makeSuite(SkinsToolTests),
+        makeSuite(SkinnableTests),
         ))
 
 if __name__ == '__main__':


More information about the Zope-CMF mailing list