[CMF-checkins] CVS: CMF/CMFCore - ActionProviderBase.py:1.20.4.1 DiscussionTool.py:1.13.2.1 TypesTool.py:1.62.2.1 WorkflowTool.py:1.41.2.1

Yvo Schubbe cvs-admin at zope.org
Tue Nov 25 09:00:25 EST 2003


Update of /cvs-repository/CMF/CMFCore
In directory cvs.zope.org:/tmp/cvs-serv31480/CMFCore

Modified Files:
      Tag: yuppie-listActionInfos-branch
	ActionProviderBase.py DiscussionTool.py TypesTool.py 
	WorkflowTool.py 
Log Message:
refactoring (part 2):
- added listActionInfos() and getActionInfo() to the ActionProvider Interface
- made OldstyleActionProvider Interface inherit these 2 methods
- added 'object' argument to listActions() of the ActionProvider Interface
- implemented new / changed methods in ActionProviderBase and TypesTool
- added OldstyleActionProviderBase implementing OldstyleActionProvider
- WorkflowTool and CMFCore DiscussionTool now use OldstyleActionProviderBase


=== CMF/CMFCore/ActionProviderBase.py 1.20 => 1.20.4.1 ===
--- CMF/CMFCore/ActionProviderBase.py:1.20	Sat Oct 18 04:33:53 2003
+++ CMF/CMFCore/ActionProviderBase.py	Tue Nov 25 08:59:55 2003
@@ -15,15 +15,22 @@
 $Id$
 """
 
+from types import StringType
+
 from Globals import DTMLFile, InitializeClass
 from AccessControl import ClassSecurityInfo
 
 from ActionInformation import ActionInformation
+from ActionInformation import getOAI
 from CMFCorePermissions import ManagePortal
 from Expression import Expression
-from utils import _dtmldir
-
+from Expression import getExprContext
 from interfaces.portal_actions import ActionProvider as IActionProvider
+from interfaces.portal_actions \
+        import OldstyleActionProvider as IOldstyleActionProvider
+from utils import _checkPermission
+from utils import _dtmldir
+from utils import getToolByName
 
 
 class ActionProviderBase:
@@ -47,12 +54,74 @@
     #
     #   ActionProvider interface
     #
-    security.declarePrivate( 'listActions' )
-    def listActions( self, info=None ):
-        """ Return all the actions defined by a provider.
+    security.declarePrivate('listActions')
+    def listActions(self, info=None, object=None):
+        """ List all the actions defined by a provider.
         """
         return self._actions or ()
 
+    security.declarePublic('listActionInfos')
+    def listActionInfos(self, action_chain=None, object=None,
+                        check_visibility=1, check_permissions=1,
+                        check_condition=1, max=-1):
+        # List Action info mappings.
+        # (method is without docstring to disable publishing)
+        #
+        ec = getExprContext(self, object)
+        actions = self.listActions(object=object)
+
+        if action_chain:
+            filtered_actions = []
+            if isinstance(action_chain, StringType):
+                action_chain = (action_chain,)
+            for action_ident in action_chain:
+                sep = action_ident.rfind('/')
+                category, id = action_ident[:sep], action_ident[sep+1:]
+                for ai in actions:
+                    if id == ai.getId() and category == ai.getCategory():
+                        filtered_actions.append(ai)
+            actions = filtered_actions
+
+        action_infos = []
+        for ai in actions:
+            if check_visibility and not ai.getVisibility():
+                continue
+            if check_permissions:
+                permissions = ai.getPermissions()
+                if permissions:
+                    category = ai.getCategory()
+                    if (object is not None and
+                        (category.startswith('object') or
+                         category.startswith('workflow'))):
+                        context = object
+                    elif (ec.contexts['folder'] is not None and
+                          category.startswith('folder')):
+                        context = ec.contexts['folder']
+                    else:
+                        context = ec.contexts['portal']
+                    for permission in permissions:
+                        allowed = _checkPermission(permission, context)
+                        if allowed:
+                            break
+                    if not allowed:
+                        continue
+            if check_condition and not ai.testCondition(ec):
+                continue
+            action_infos.append( ai.getAction(ec) )
+            if max + 1 and len(action_infos) >= max:
+                break
+        return action_infos
+
+    security.declarePublic('getActionInfo')
+    def getActionInfo(self, action_chain, object=None, check_visibility=0,
+                      check_condition=0):
+        """ Get an Action info mapping specified by a chain of actions.
+        """
+        action_infos = self.listActionInfos(action_chain, object,
+                                       check_visibility=check_visibility,
+                                       check_condition=check_condition, max=1)
+        return action_infos and action_infos[0] or None
+
     #
     #   ZMI methods
     #
@@ -278,3 +347,83 @@
                                 )
 
 InitializeClass(ActionProviderBase)
+
+
+class OldstyleActionProviderBase:
+    """ Base class for ActionProviders with oldstyle Actions.
+    """
+
+    __implements__ = IOldstyleActionProvider
+
+    security = ClassSecurityInfo()
+
+    _actions = ()
+
+    #
+    #   OldstyleActionProvider interface
+    #
+    security.declarePrivate('listActions')
+    def listActions(self, info):
+        """ List all the actions defined by a provider.
+        """
+        return self._actions or ()
+
+    security.declarePublic('listActionInfos')
+    def listActionInfos(self, action_chain=None, object=None,
+                        check_visibility=1, check_permissions=1,
+                        check_condition=1, max=-1):
+        # List Action info mappings.
+        # (method is without docstring to disable publishing)
+        #
+        info = getOAI(self, object)
+        actions = self.listActions(info=info)
+
+        if action_chain:
+            filtered_actions = []
+            if isinstance(action_chain, StringType):
+                action_chain = (action_chain,)
+            for action_ident in action_chain:
+                sep = action_ident.rfind('/')
+                category, id = action_ident[:sep], action_ident[sep+1:]
+                for ai in actions:
+                    if id == ai['id'] and category == ai['category']:
+                        filtered_actions.append(ai)
+            actions = filtered_actions
+
+        action_infos = []
+        for ai in actions:
+            if check_permissions:
+                permissions = ai.get( 'permissions', () )
+                if permissions:
+                    category = ai['category']
+                    if (object is not None and
+                        (category.startswith('object') or
+                         category.startswith('workflow'))):
+                        context = object
+                    elif (info['folder'] is not None and
+                          category.startswith('folder')):
+                        context = info['folder']
+                    else:
+                        context = info['portal']
+                    for permission in permissions:
+                        allowed = _checkPermission(permission, context)
+                        if allowed:
+                            break
+                    if not allowed:
+                        continue
+            action_infos.append(ai)
+            if max + 1 and len(action_infos) >= max:
+                break
+        return action_infos
+
+    security.declarePublic('getActionInfo')
+    def getActionInfo(self, action_chain, object=None, check_visibility=0,
+                      check_condition=0):
+        """ Get an Action info mapping specified by a chain of actions.
+        """
+        action_infos = self.listActionInfos(action_chain, object,
+                                       check_visibility=check_visibility,
+                                       check_condition=check_condition, max=1)
+        return action_infos and action_infos[0] or None
+
+InitializeClass(OldstyleActionProviderBase)


=== CMF/CMFCore/DiscussionTool.py 1.13 => 1.13.2.1 ===
--- CMF/CMFCore/DiscussionTool.py:1.13	Fri Nov 14 03:34:55 2003
+++ CMF/CMFCore/DiscussionTool.py	Tue Nov 25 08:59:55 2003
@@ -20,13 +20,12 @@
 from Acquisition import Implicit
 from AccessControl import ClassSecurityInfo
 
+from ActionProviderBase import OldstyleActionProviderBase
 from CMFCorePermissions import AccessContentsInformation
 from CMFCorePermissions import ManagePortal
 from CMFCorePermissions import ReplyToItem
 from CMFCorePermissions import View
 from interfaces.Discussions import OldDiscussable as IOldDiscussable
-from interfaces.portal_actions \
-        import OldstyleActionProvider as IOldstyleActionProvider
 from interfaces.portal_discussion \
         import oldstyle_portal_discussion as IOldstyleDiscussionTool
 from utils import _dtmldir
@@ -113,9 +112,10 @@
         return ""
 
 
-class DiscussionTool (UniqueObject, SimpleItem):
+class DiscussionTool (UniqueObject, SimpleItem, OldstyleActionProviderBase):
 
-    __implements__ = (IOldstyleDiscussionTool, IOldstyleActionProvider)
+    __implements__ = (IOldstyleDiscussionTool,
+                      OldstyleActionProviderBase.__implements__)
 
     id = 'portal_discussion'
     meta_type = 'Oldstyle CMF Discussion Tool'


=== CMF/CMFCore/TypesTool.py 1.62 => 1.62.2.1 ===
--- CMF/CMFCore/TypesTool.py:1.62	Fri Nov 14 03:34:55 2003
+++ CMF/CMFCore/TypesTool.py	Tue Nov 25 08:59:55 2003
@@ -252,8 +252,7 @@
         return self.global_allow
 
     security.declarePublic('listActions')
-    def listActions( self, info=None ):
-
+    def listActions(self, info=None, object=None):
         """ Return a sequence of the action info objects for this type.
         """
         if self._actions and type( self._actions[0] ) == type( {} ):
@@ -888,16 +887,15 @@
             RESPONSE.redirect( immediate_url )
 
     security.declarePrivate( 'listActions' )
-    def listActions( self, info=None ):
-        """
-            List type-related actions.
+    def listActions(self, info=None, object=None):
+        """ List all the actions defined by a provider.
         """
         actions = list( self._actions )
 
-        if info is not None:
-
-            type_info = self.getTypeInfo( info.content )
-
+        if object is None and info is not None:
+            object = info.content
+        if object is not None:
+            type_info = self.getTypeInfo(object)
             if type_info is not None:
                 actions.extend( type_info.listActions() )
 


=== CMF/CMFCore/WorkflowTool.py 1.41 => 1.41.2.1 ===
--- CMF/CMFCore/WorkflowTool.py:1.41	Fri Nov 14 03:34:55 2003
+++ CMF/CMFCore/WorkflowTool.py	Tue Nov 25 08:59:55 2003
@@ -22,9 +22,8 @@
 from AccessControl import ClassSecurityInfo
 from Acquisition import aq_base, aq_inner, aq_parent
 
+from ActionProviderBase import OldstyleActionProviderBase
 from CMFCorePermissions import ManagePortal
-from interfaces.portal_actions \
-        import OldstyleActionProvider as IOldstyleActionProvider
 from interfaces.portal_workflow import portal_workflow as IWorkflowTool
 from utils import _dtmldir
 from utils import getToolByName
@@ -53,13 +52,13 @@
         raise KeyError, name
 
 
-class WorkflowTool (UniqueObject, Folder):
-
+class WorkflowTool(UniqueObject, Folder, OldstyleActionProviderBase):
     """ Mediator tool, mapping workflow objects
     """
     id = 'portal_workflow'
     meta_type = 'CMF Workflow Tool'
-    __implements__ = (IWorkflowTool, IOldstyleActionProvider)
+    __implements__ = (IWorkflowTool,
+                      OldstyleActionProviderBase.__implements__)
 
     _chains_by_type = None  # PersistentMapping
     _default_chain = ('default_workflow',)
@@ -344,7 +343,7 @@
                         'Requested workflow definition not found.')
                 else:
                     return default
-        res = apply(wf.getInfoFor, (ob, name, default) + args, kw)
+        res = wf.getInfoFor(ob, name, default, *args, **kw)
         if res is _marker:
             raise WorkflowException('Could not get info: %s' % name)
         return res
@@ -590,7 +589,7 @@
             wfs = ()
         if wf is None:
             # No workflow wraps this method.
-            return apply(func, args, kw)
+            return func(*args, **kw)
         return self._invokeWithNotification(
             wfs, ob, method_id, wf.wrapWorkflowMethod,
             (ob, method_id, func, args, kw), {})
@@ -619,7 +618,7 @@
         for w in wfs:
             w.notifyBefore(ob, action)
         try:
-            res = apply(func, args, kw)
+            res = func(*args, **kw)
         except ObjectDeleted, ex:
             res = ex.getResult()
             reindex = 0




More information about the CMF-checkins mailing list