[Zope3-checkins] SVN: Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler. slight refactoring and more tests for statehandler

Helmut Merz helmutm at cy55.de
Sun Oct 9 05:24:51 EDT 2005


Log message for revision 38991:
  slight refactoring and more tests for statehandler

Changed:
  U   Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.py
  U   Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.txt

-=-
Modified: Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.py
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.py	2005-10-09 09:06:32 UTC (rev 38990)
+++ Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.py	2005-10-09 09:24:50 UTC (rev 38991)
@@ -38,29 +38,32 @@
         """Set the state of the portlet; the name parameter is the name
            of the portlet in the portlet manager.
         """
-        cpType = zapi.queryType(self.context, IContentProviderType)
-        cpTypeName = cpType.__module__ + '.' + cpType.__name__
-        settings = preference.PreferenceGroup(
-            cpTypeName + '/' + name,
-            schema=IPortletPreference,
-            title=u"Portlet User Settings",
-            description=u""
-            )
+        settings = self.getSettings(getPortletKeyString(self.context, name))
         settings.state = value
 
     def getState(self, name):
         """Return the state of the portlet; the name parameter is the name
            of the portlet in the portlet manager.
         """
-        cpType = zapi.queryType(self.context, IContentProviderType)
-        cpTypeName = cpType.__module__ + '.' + cpType.__name__
-        settings = preference.PreferenceGroup(
-            cpTypeName + '/' + name,
+        settings = self.getSettings(getPortletKeyString(self.context, name))
+        return settings.state
+
+    def getSettings(self, key):
+        """Return the preferences element belonging to the key given.
+        """
+        return preference.PreferenceGroup(
+            key,
             schema=IPortletPreference,
             title=u"Portlet User Settings",
             description=u""
             )
-        return settings.state
 
 
-
+def getPortletKeyString(context, name):
+    """Return a string for a portlet object (context) and name that
+       may be used for accessing a state setting in a mapping.
+    """
+    cpType = zapi.queryType(context, IContentProviderType)
+    cpTypeName = cpType.__module__ + '.' + cpType.__name__
+    key = cpTypeName + '/' + name
+    return key

Modified: Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.txt
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.txt	2005-10-09 09:06:32 UTC (rev 38990)
+++ Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.txt	2005-10-09 09:24:50 UTC (rev 38991)
@@ -88,14 +88,14 @@
   >>> class Principal:
   ...     def __init__(self, id):
   ...         self.id = id
-  >>> principal = Principal('zope.user')
+  >>> fooPrincipal = Principal('foo')
 
   >>> class Participation:
   ...     interaction = None
   ...     def __init__(self, principal):
   ...         self.principal = principal
 
-  >>> participation = Participation(principal)
+  >>> participation = Participation(fooPrincipal)
 
   >>> import zope.security.management
   >>> zope.security.management.endInteraction()
@@ -125,3 +125,28 @@
   ...
   ConstraintNotSatisfied: exploded...
   
+No try with another principal:
+  
+  >>> barPrincipal = Principal('bar')
+  >>> participation = Participation(barPrincipal)
+  >>> zope.security.management.endInteraction()
+  >>> zope.security.management.newInteraction(participation)
+
+The state does not have a default value:
+  
+  >>> handler.getState(name='dummy.portlet')
+  
+  >>> handler.setState('visible', name='dummy.portlet')
+  >>> handler.getState(name='dummy.portlet')
+  'visible'
+  
+Now switch back to the previous principal:
+
+  >>> participation = Participation(fooPrincipal)
+  >>> zope.security.management.endInteraction()
+  >>> zope.security.management.newInteraction(participation)
+
+  >>> handler.getState(name='dummy.portlet')
+  'hidden'
+
+    
\ No newline at end of file



More information about the Zope3-Checkins mailing list