[Zope3-checkins]
SVN: Zope3/branches/roger-contentprovider/src/zope/portlet/
statehandler stuff working now
Helmut Merz
helmutm at cy55.de
Sun Oct 9 03:44:05 EDT 2005
Log message for revision 38983:
statehandler stuff working now
Changed:
A Zope3/branches/roger-contentprovider/src/zope/portlet/preference.py
D Zope3/branches/roger-contentprovider/src/zope/portlet/preferences.py
U Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.py
U Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.txt
-=-
Added: Zope3/branches/roger-contentprovider/src/zope/portlet/preference.py
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/portlet/preference.py 2005-10-08 22:30:21 UTC (rev 38982)
+++ Zope3/branches/roger-contentprovider/src/zope/portlet/preference.py 2005-10-09 07:44:04 UTC (rev 38983)
@@ -0,0 +1,38 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Portlet preferences implementation
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import Interface
+from zope import schema
+from zope.app.i18n import ZopeMessageIDFactory as _
+
+
+class IPortletPreference(Interface):
+ """User Preference for a single portlet."""
+
+ expanded = schema.Bool(
+ title=_(u"Show portlet expanded"),
+ description=_(u"Shows portlet in its inital state expanded.")
+ )
+
+ state = schema.Choice(
+ title=_(u"Portlet state"),
+ description=_(u"Render portlet in the given mode."),
+ values=(_(u"visible"), _(u"hidden"),),
+ )
+
\ No newline at end of file
Property changes on: Zope3/branches/roger-contentprovider/src/zope/portlet/preference.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Deleted: Zope3/branches/roger-contentprovider/src/zope/portlet/preferences.py
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/portlet/preferences.py 2005-10-08 22:30:21 UTC (rev 38982)
+++ Zope3/branches/roger-contentprovider/src/zope/portlet/preferences.py 2005-10-09 07:44:04 UTC (rev 38983)
@@ -1,38 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2004 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Portlet preferences implementation
-
-$Id$
-"""
-__docformat__ = 'restructuredtext'
-
-from zope.interface import Interface
-from zope import schema
-from zope.app.i18n import ZopeMessageIDFactory as _
-
-
-class IPortletPreferences(Interface):
- """User Preference for a single portlet."""
-
- expanded = schema.Bool(
- title=_(u"Show portlet expanded"),
- description=_(u"Shows portlet in its inital state expanded.")
- )
-
- state = schema.Choice(
- title=_(u"Portlet state"),
- description=_(u"Render portlet in the given mode."),
- values=(_(u"visible"), _(u"hidden"),),
- )
-
\ No newline at end of file
Modified: Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.py
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.py 2005-10-08 22:30:21 UTC (rev 38982)
+++ Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.py 2005-10-09 07:44:04 UTC (rev 38983)
@@ -23,26 +23,44 @@
from zope.contentprovider.interfaces import IContentProviderType
from zope.portlet import interfaces
-from zope.portlet.preferences import IPortletPreferences
+from zope.portlet.preference import IPortletPreference
-class PreferencesStateHandler(object):
+class PreferenceStateHandler(object):
"""State handler based on preferences."""
zope.interface.implements(interfaces.IStateHandler)
def __init__(self, context):
self.context = context
- self.preferences = None
def setState(self, value, name):
- """xxx"""
+ """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,
- schema=IPortletPreferences,
+ cpTypeName + '/' + name,
+ schema=IPortletPreference,
title=u"Portlet User Settings",
description=u""
)
+ 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,
+ schema=IPortletPreference,
+ title=u"Portlet User Settings",
+ description=u""
+ )
+ return settings.state
+
+
+
Modified: Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.txt
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.txt 2005-10-08 22:30:21 UTC (rev 38982)
+++ Zope3/branches/roger-contentprovider/src/zope/portlet/statehandler.txt 2005-10-09 07:44:04 UTC (rev 38983)
@@ -73,17 +73,17 @@
'visible'
-Use preferences for portlet state storage
------------------------------------------
+Use user preferences for portlet state storage
+----------------------------------------------
- >>> from zope.portlet.statehandler import PreferencesStateHandler
+ >>> from zope.portlet.statehandler import PreferenceStateHandler
>>> from zope.publisher.browser import TestRequest
>>> from zope.publisher.browser import IBrowserRequest
- >>> ztapi.provideAdapter(IPortlet, IStateHandler, PreferencesStateHandler)
+ >>> ztapi.provideAdapter(IPortlet, IStateHandler, PreferenceStateHandler)
We need a principal to store preferences with and a corresponding interaction
-to find it.
+to find it; and we have to register a PrincipalAnnotationUtility for this.
>>> class Principal:
... def __init__(self, id):
@@ -101,8 +101,27 @@
>>> zope.security.management.endInteraction()
>>> zope.security.management.newInteraction(participation)
+ >>> from zope.app.principalannotation.interfaces import IPrincipalAnnotationUtility
+ >>> from zope.app.principalannotation import PrincipalAnnotationUtility
+ >>> ztapi.provideUtility(IPrincipalAnnotationUtility, PrincipalAnnotationUtility())
+
+So we are ready to store the portlet's state:
+
>>> handler = zapi.getAdapter(portlet, IStateHandler)
- >>> isinstance(handler, PreferencesStateHandler)
+ >>> isinstance(handler, PreferenceStateHandler)
True
- >>> handler.setState('visible', name='dummy.portlet')
+ >>> handler.setState('hidden', name='dummy.portlet')
+And retrieve it:
+
+ >>> handler.getState(name='dummy.portlet')
+ 'hidden'
+
+If we use a value for the state that is not supported by the preference state
+handler we get an exception:
+
+ >>> handler.setState('exploded', name='dummy.portlet')
+ Traceback (most recent call last):
+ ...
+ ConstraintNotSatisfied: exploded...
+
More information about the Zope3-Checkins
mailing list