[CMF-checkins] CVS: CMF/CMFCore - ActionInformation.py:1.17.6.1 ActionsTool.py:1.41.2.1 Expression.py:1.5.44.1

Yvo Schubbe cvs-admin at zope.org
Fri Nov 21 14:22:22 EST 2003


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

Modified Files:
      Tag: yuppie-listActionInfos-branch
	ActionInformation.py ActionsTool.py Expression.py 
Log Message:
refactoring (part 1):
- added getOAI() and getExprContext() to get computed contexts from REQUEST cache or create new ones
- listFilteredActionsFor() is now using these new methods
- moved Action filtering into _listActionInfos for Action Providers that don't implement the new API
- import and whitespace cleanup


=== CMF/CMFCore/ActionInformation.py 1.17 => 1.17.6.1 ===
--- CMF/CMFCore/ActionInformation.py:1.17	Mon Sep  1 11:18:34 2003
+++ CMF/CMFCore/ActionInformation.py	Fri Nov 21 14:21:51 2003
@@ -15,20 +15,22 @@
 $Id$
 """
 
+from types import StringType
+
 from AccessControl import ClassSecurityInfo
 from Globals import InitializeClass
-from Acquisition import aq_inner, aq_parent
+from Acquisition import aq_base, aq_inner, aq_parent
 from OFS.SimpleItem import SimpleItem
 
 from Expression import Expression
 from CMFCorePermissions import View
 from utils import getToolByName
-from types import StringType
+
 
 class ActionInformation( SimpleItem ):
 
     """ Represent a single selectable action.
-    
+
     Actions generate links to views of content, or to specific methods
     of the site.  They can be filtered via their conditions.
     """
@@ -59,10 +61,10 @@
         self.id = id
         self.title = title
         self.description = description
-        self.category = category 
+        self.category = category
         self.condition = condition
         self.permissions = permissions
-        self.priority = priority 
+        self.priority = priority
         self.visible = visible
         self.setActionExpression(action)
 
@@ -106,7 +108,7 @@
         info['permissions'] = self.getPermissions()
         info['category'] = self.getCategory()
         info['visible'] = self.getVisibility()
-        return info 
+        return info
 
     security.declarePrivate( '_getActionObject' )
     def _getActionObject( self ):
@@ -192,6 +194,29 @@
 
 InitializeClass( ActionInformation )
 
+
+def getOAI(context, object=None):
+    cache = context.REQUEST.get('_oai_cache', None)
+    if cache is None:
+        context.REQUEST['_oai_cache'] = cache = {}
+    info = cache.get( str(object), None )
+    if info is None:
+        if object is None or not hasattr(object, 'aq_base'):
+            folder = None
+        else:
+            folder = object
+            # Search up the containment hierarchy until we find an
+            # object that claims it's a folder.
+            while folder is not None:
+                if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
+                    # found it.
+                    break
+                else:
+                    folder = aq_parent(aq_inner(folder))
+        cache[ str(object) ] = info = oai(context, folder, object)
+    return info
+
+
 class oai:
     #Provided for backwards compatability
     # Provides information that may be needed when constructing the list of
@@ -223,4 +248,3 @@
         if hasattr(self, name):
             return getattr(self, name)
         raise KeyError, name
-


=== CMF/CMFCore/ActionsTool.py 1.41 => 1.41.2.1 ===
--- CMF/CMFCore/ActionsTool.py:1.41	Fri Nov 14 03:34:55 2003
+++ CMF/CMFCore/ActionsTool.py	Fri Nov 21 14:21:51 2003
@@ -15,26 +15,29 @@
 $Id$
 """
 
+from types import DictionaryType
+
 from Globals import InitializeClass, DTMLFile, package_home
-from Acquisition import aq_base, aq_inner, aq_parent
 from AccessControl import ClassSecurityInfo
+from Acquisition import aq_base, aq_inner, aq_parent
 from OFS.Folder import Folder
 from OFS.SimpleItem import SimpleItem
 
-from Expression import Expression, createExprContext
-from ActionInformation import ActionInformation, oai
+from ActionInformation import ActionInformation
+from ActionInformation import getOAI
 from ActionProviderBase import ActionProviderBase
-from TypesTool import TypeInformation
 from CMFCorePermissions import ListFolderContents
 from CMFCorePermissions import ManagePortal
+from Expression import Expression
+from Expression import getExprContext
+from interfaces.portal_actions import portal_actions as IActionsTool
+from TypesTool import TypeInformation
 from utils import _checkPermission
 from utils import _dtmldir
 from utils import getToolByName
 from utils import SimpleItemWithProperties
 from utils import UniqueObject
 
-from interfaces.portal_actions import portal_actions as IActionsTool
-
 
 class ActionsTool(UniqueObject, Folder, ActionProviderBase):
     """
@@ -51,7 +54,7 @@
                                 , action=Expression(
                text='string: ${folder_url}/folder_contents')
                                 , condition=Expression(
-               text='python: folder is not object') 
+               text='python: folder is not object')
                                 , permissions=(ListFolderContents,)
                                 , category='object'
                                 , visible=1
@@ -89,7 +92,7 @@
                          , 'action' : 'manage_overview'
                          }
                      ) + Folder.manage_options
-                     ) 
+                     )
 
     #
     #   ZMI methods
@@ -156,37 +159,25 @@
     def listFilteredActionsFor(self, object=None):
         """ List all actions available to the user.
         """
-        portal = aq_parent(aq_inner(self))
-        if object is None or not hasattr(object, 'aq_base'):
-            folder = portal
-        else:
-            folder = object
-            # Search up the containment hierarchy until we find an
-            # object that claims it's a folder.
-            while folder is not None:
-                if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
-                    # found it.
-                    break
-                else:
-                    folder = aq_parent(aq_inner(folder))
-        ec = createExprContext(folder, portal, object)
         actions = []
-        append = actions.append
-        info = oai(self, folder, object)
 
         # Include actions from specific tools.
         for provider_name in self.listActionProviders():
             provider = getattr(self, provider_name)
-            self._listActions(append,provider,info,ec)
+            if hasattr( aq_base(provider), 'listActionInfos' ):
+                actions.extend( provider.listActionInfos(object=object) )
+            else:
+                # for Action Providers written for CMF versions before 1.5
+                actions.extend( self._listActionInfos(provider, object) )
 
+        # for objects written for CMF versions before 1.5
         # Include actions from object.
         if object is not None:
             base = aq_base(object)
             if hasattr(base, 'listActions'):
-                self._listActions(append,object,info,ec)
+                actions.extend( self._listActionInfos(object, object) )
 
-        # Reorganize the actions by category,
-        # filtering out disallowed actions.
+        # Reorganize the actions by category.
         filtered_actions={'user':[],
                           'folder':[],
                           'object':[],
@@ -195,44 +186,16 @@
                           }
         for action in actions:
             category = action['category']
-            permissions = action.get('permissions', None)
-            visible = action.get('visible', 1)
-            if not visible:
-                continue
-            verified = 0
-            if not permissions:
-                # This action requires no extra permissions.
-                verified = 1
-            else:
-                # startswith() is used so that we can have several
-                # different categories that are checked in the object or
-                # folder context.
-                if (object is not None and
-                    (category.startswith('object') or
-                     category.startswith('workflow'))):
-                    context = object
-                elif (folder is not None and
-                      category.startswith('folder')):
-                    context = folder
-                else:
-                    context = portal
-                for permission in permissions:
-                    # The user must be able to match at least one of
-                    # the listed permissions.
-                    if _checkPermission(permission, context):
-                        verified = 1
-                        break
-            if verified:
-                catlist = filtered_actions.get(category, None)
-                if catlist is None:
-                    filtered_actions[category] = catlist = []
-                # Filter out duplicate actions by identity...
-                if not action in catlist:
-                    catlist.append(action)
-                # ...should you need it, here's some code that filters
-                # by equality (use instead of the two lines above)
-                #if not [a for a in catlist if a==action]:
-                #    catlist.append(action)
+            catlist = filtered_actions.get(category, None)
+            if catlist is None:
+                filtered_actions[category] = catlist = []
+            # Filter out duplicate actions by identity...
+            if not action in catlist:
+                catlist.append(action)
+            # ...should you need it, here's some code that filters
+            # by equality (use instead of the two lines above)
+            #if not [a for a in catlist if a==action]:
+            #    catlist.append(action)
         return filtered_actions
 
     # listFilteredActions() is an alias.
@@ -240,16 +203,65 @@
     listFilteredActions = listFilteredActionsFor
 
     #
-    #   Helper methods
+    #   Helper method for backwards compatibility
     #
-    def _listActions(self,append,object,info,ec):
-        a = object.listActions(info)
-        if a and type(a[0]) is not type({}):
-            for ai in a:
-                if ai.testCondition(ec):
-                    append(ai.getAction(ec))
+    def _listActionInfos(self, provider, object):
+        """ for Action Providers written for CMF versions before 1.5
+        """
+        info = getOAI(self, object)
+        actions = provider.listActions(info)
+
+        action_infos = []
+        if actions and type(actions[0]) is not DictionaryType:
+            ec = getExprContext(self, object)
+            for ai in actions:
+                if not ai.getVisibility():
+                    continue
+                permissions = ai.getPermissions()
+                if permissions:
+                    category = ai.getCategory()
+                    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
+                if not ai.testCondition(ec):
+                    continue
+                action_infos.append( ai.getAction(ec) )
         else:
-            for i in a:
-                append(i)
+            for i in actions:
+                if not i.get('visible', 1):
+                    continue
+                permissions = i.get('permissions', None)
+                if permissions:
+                    category = i['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(i)
+        return action_infos
 
 InitializeClass(ActionsTool)


=== CMF/CMFCore/Expression.py 1.5 => 1.5.44.1 ===
--- CMF/CMFCore/Expression.py:1.5	Thu Aug  1 15:05:11 2002
+++ CMF/CMFCore/Expression.py	Fri Nov 21 14:21:51 2003
@@ -1,14 +1,14 @@
 ##############################################################################
 #
 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
-# 
+#
 # This software is subject to the provisions of the Zope Public License,
 # Version 2.0 (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
-# 
+#
 ##############################################################################
 """ Expressions in a web-configurable workflow.
 
@@ -17,7 +17,7 @@
 
 import Globals
 from Globals import Persistent
-from Acquisition import aq_inner, aq_parent
+from Acquisition import aq_base, aq_inner, aq_parent
 from AccessControl import getSecurityManager, ClassSecurityInfo
 
 from utils import getToolByName
@@ -25,6 +25,7 @@
 from Products.PageTemplates.TALES import SafeMapping
 from Products.PageTemplates.Expressions import SecureModuleImporter
 
+
 class Expression (Persistent):
     text = ''
     _v_compiled = None
@@ -48,6 +49,30 @@
         return res
 
 Globals.InitializeClass(Expression)
+
+
+def getExprContext(context, object=None):
+    cache = context.REQUEST.get('_ec_cache', None)
+    if cache is None:
+        context.REQUEST['_ec_cache'] = cache = {}
+    ec = cache.get( str(object), None )
+    if ec is None:
+        utool = getToolByName(context, 'portal_url')
+        portal = utool.getPortalObject()
+        if object is None or not hasattr(object, 'aq_base'):
+            folder = portal
+        else:
+            folder = object
+            # Search up the containment hierarchy until we find an
+            # object that claims it's a folder.
+            while folder is not None:
+                if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
+                    # found it.
+                    break
+                else:
+                    folder = aq_parent(aq_inner(folder))
+        cache[ str(object) ] = ec = createExprContext(folder, portal, object)
+    return ec
 
 
 def createExprContext(folder, portal, object):




More information about the CMF-checkins mailing list