[CMF-checkins] CVS: CMF/CMFCore - ActionProviderBase.py:1.8

Tres Seaver tseaver@zope.com
Tue, 2 Jul 2002 20:43:00 -0400


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

Modified Files:
	ActionProviderBase.py 
Log Message:


  - Reorganize for clarity.

  - ActionProviderBase:  keep actions as a tuple, rather than a list,
    to avoid some possibly strange global interactions.


=== CMF/CMFCore/ActionProviderBase.py 1.7 => 1.8 ===
 # 
 ##############################################################################
+""" Implement a shared base for tools which provide actions.
+
+$Id$
+"""
 
-from OFS.SimpleItem import SimpleItem
 from Globals import DTMLFile
-from CMFCorePermissions import ManagePortal
-from utils import _dtmldir, cookString
 from AccessControl import ClassSecurityInfo
+from OFS.SimpleItem import SimpleItem
+
 from ActionInformation import ActionInformation
+from CMFCorePermissions import ManagePortal
 from Expression import Expression
+from utils import _dtmldir
 
 
-"""Basic action list tool.
-
-$Id$
-"""
-__version__='$Revision$'[11:-2]
-
 class ActionProviderBase:
+
+    """ Provide ActionTabs and management methods for ActionProviders
     """
-    Provide ActionTabs and management methods for ActionProviders
-    """
-    _actions = []
     security = ClassSecurityInfo()
+
+    _actions = ()
     _actions_form = DTMLFile( 'editToolsActions', _dtmldir )
 
-    manage_options = ({ 'label' : 'Actions', 'action' : 'manage_editActionsForm' }
-                    , 
+    manage_options = ( { 'label' : 'Actions'
+                       , 'action' : 'manage_editActionsForm'
+                       }
+                     , 
                      )
 
-    security.declarePrivate('listActions')
-    def listActions(self, info=None):
-        """
-        Return all the actions defined by a tool
-        """
-        if self._actions:
-            return self._actions
-        else:
-            return None
+    security.declarePrivate( 'listActions' )
+    def listActions( self, info=None ):
 
-    security.declareProtected(ManagePortal, 'manage_editActionsForm')
-    def manage_editActionsForm(self, REQUEST, manage_tabs_message=None):
+        """ Return all the actions defined by a provider.
         """
-        Shows the 'Actions' management tab.
+        return self._actions or None
+
+    security.declareProtected( ManagePortal, 'manage_editActionsForm' )
+    def manage_editActionsForm( self, REQUEST, manage_tabs_message=None ):
+
+        """ Show the 'Actions' management tab.
         """
         actions = []
         if self.listActions() is not None:
+
             for a in self.listActions():
+
                 a1 = {}
                 a1['id'] = a.getId()
                 a1['name'] = a.Title()
@@ -75,15 +76,18 @@
                 else:
                     a1['condition'] = ''
                 actions.append(a1)
-                # possible_permissions is in AccessControl.Role.RoleManager.
+
+        # possible_permissions is in AccessControl.Role.RoleManager.
         pp = self.possible_permissions()
-        return self._actions_form(self, REQUEST,
-                                  actions=actions,
-                                  possible_permissions=pp,
-                                  management_view='Actions',
-                                  manage_tabs_message=manage_tabs_message)
+        return self._actions_form( self
+                                 , REQUEST
+                                 , actions=actions
+                                 , possible_permissions=pp
+                                 , management_view='Actions'
+                                 , manage_tabs_message=manage_tabs_message
+                                 )
 
-    security.declareProtected(ManagePortal, 'addAction')
+    security.declareProtected( ManagePortal, 'addAction' )
     def addAction( self
                  , id
                  , name
@@ -94,120 +98,121 @@
                  , visible=1
                  , REQUEST=None
                  ):
+        """ Add an action to our list.
         """
-        Adds an action to the list.
-        """
-        al = self._actions[:]
         if not name:
             raise ValueError('A name is required.')
-        if action:
-            a_expr = Expression(text=str(action))
-        else:
-            a_expr = ''
-        if condition:
-            c_expr = Expression(text=str(condition))
-        else:
-            c_expr = ''
-        if permission:            
-            perm = (str(permission),)
-        else:
-            perm = ()
-        al.append(ActionInformation(id=str(id)
-                                         , title=str(name)
-                                         , action=a_expr
-                                         , condition=c_expr
-                                         , permissions=perm
-                                         , category=str(category)
-                                         , visible=int(visible)
-                                          ))
-        self._actions = al
+
+        a_expr = action and Expression(text=str(action)) or ''
+        c_expr = condition and Expression(text=str(condition)) or ''
+        perm = permission and (str(permission),) or ()
+
+        info = ActionInformation( id=str(id)
+                                , title=str(name)
+                                , action=a_expr
+                                , condition=c_expr
+                                , permissions=perm
+                                , category=str(category)
+                                , visible=int(visible)
+                                )
+
+        al = list( self._actions )
+        al.append( info )
+        self._actions = tuple( al )
+
         if REQUEST is not None:
             return self.manage_editActionsForm(
                 REQUEST, manage_tabs_message='Added.')
     
-    security.declareProtected(ManagePortal, 'changeActions')
-    def changeActions(self, properties=None, REQUEST=None):
+    security.declarePrivate( '_extractAction' )
+    def _extractAction( self, properties, index ):
+
+        """ Extract an ActionInformation from the funky form properties.
         """
-        Changes the _actions.
+        id          = str( properties.get( 'id_%d'          % index, '' ) )
+        name        = str( properties.get( 'name_%d'        % index, '' ) )
+        action      = str( properties.get( 'action_%d'      % index, '' ) )
+        condition   = str( properties.get( 'condition_%d'   % index, '' ) )
+        category    = str( properties.get( 'category_%d'    % index, '' ))
+        visible     =      properties.get( 'visible_%d'     % index, 0  )
+        permissions =      properties.get( 'permission_%d'  % index, () )
+
+        if not name:
+            raise ValueError('A name is required.')
+
+        if action is not '':
+            action = Expression( text=action )
+
+        if condition is not '':
+            condition = Expression( text=condition )
+
+        if category == '':
+            category = 'object'
+
+        if type( visible ) is not type( 0 ):
+            visible = 0
+
+        if type( permissions ) is type( '' ):
+            permissions = ( permissions, )
+
+        return ActionInformation( id=id
+                                , title=name
+                                , action=action
+                                , condition=condition
+                                , permissions=permissions
+                                , category=category
+                                , visible=visible
+                                )
+
+    security.declareProtected( ManagePortal, 'changeActions' )
+    def changeActions( self, properties=None, REQUEST=None ):
+
+        """ Update our list of actions.
         """
         if properties is None:
             properties = REQUEST
-        actions = self._actions[:]
-        for idx in range(len(actions)):
-            s_idx = str(idx)
-            action = {
-                'id': str(properties.get('id_' + s_idx, '')),
-                'name': str(properties.get('name_' + s_idx, '')),
-                'action': str(properties.get('action_' + s_idx, '')),
-                'condition': str(properties.get('condition_' + s_idx, '')),
-                'permissions':
-                (properties.get('permission_' + s_idx, ()),),
-                'category': str(properties.get('category_' + s_idx, 'object')),
-                'visible': properties.get('visible_' + s_idx, 0),
-                }
-            if not action['name']:
-                raise ValueError('A name is required.')
-
-	    # DM: do not override shared element!
-            # a = actions[idx]
-	    class Empty: pass
-	    a= Empty()
-
-            a.id = action['id']
-            a.title = action['name']
-            if action['action'] is not '':
-                a._action = Expression(text=action['action'])
-            else:
-                a._action = ''
-            if action['condition'] is not '':
-                a.condition = Expression(text=action['condition'])
-            else:
-                a.condition = ''
-            a.permissions = action['permissions']
-            a.category = action['category']
-            a.visible = action['visible']
-
-	    # DM - unshare! severe sharing bug via class variable "_actions"
-	    actions[idx]= ActionInformation(
-	       id= a.id,
-	       title= a.title,
-	       action= a._action,
-	       condition= a.condition,
-	       permissions= a.permissions,
-	       category= a.category,
-	       visible= a.visible,
-	       )
- 
-        self._actions = actions
+
+        actions = []
+
+        for index in range( len( self._actions ) ):
+            actions.append( self._extractAction( properties, index ) )
+
+        self._actions = tuple( actions )
+
         if REQUEST is not None:
             return self.manage_editActionsForm(REQUEST, manage_tabs_message=
                                                'Actions changed.')
 
-    security.declareProtected(ManagePortal, 'deleteActions')
-    def deleteActions(self, selections=(), REQUEST=None):
-        """
-        Deletes actions.
+    security.declareProtected( ManagePortal, 'deleteActions' )
+    def deleteActions( self, selections=(), REQUEST=None ):
+
+        """ Delete actions indicated by indexes in 'selections'.
         """
-        actions = self._actions[:]
-        sels = list(map(int, selections))  # Convert to a list of integers.
+        sels = list( map( int, selections ) )  # Convert to a list of integers.
         sels.sort()
-        sels.reverse()
+        sels.reverse()  # Delete from end to avoid stepping on indexes
+
+        actions = list( self._actions )
         for idx in sels:
             del actions[idx]
-        self._actions = actions
+
+        self._actions = tuple( actions )
+
         if REQUEST is not None:
             return self.manage_editActionsForm(
                 REQUEST, manage_tabs_message=(
                 'Deleted %d action(s).' % len(sels)))
 
-    security.declareProtected(ManagePortal, 'moveUpActions')
-    def moveUpActions(self, selections=(), REQUEST=None):
-        """
-        Moves the specified actions up one slot.
+    security.declareProtected( ManagePortal, 'moveUpActions' )
+    def moveUpActions( self, selections=(), REQUEST=None ):
+
+        """ Move the specified actions up one slot in our list.
         """
-        actions = list(self._actions)
-        sels = list(map(int, selections))  # Convert to a list of integers.
+        sels = list( map( int, selections ) )  # Convert to a list of integers.
         sels.sort()
+
+        actions = list( self._actions )
+
         for idx in sels:
             idx2 = idx - 1
             if idx2 < 0:
@@ -217,21 +222,25 @@
             a = actions[idx2]
             actions[idx2] = actions[idx]
             actions[idx] = a
-        self._actions = actions
+
+        self._actions = tuple( actions )
+
         if REQUEST is not None:
             return self.manage_editActionsForm(
                 REQUEST, manage_tabs_message=(
                 'Moved up %d action(s).' % len(sels)))
 
-    security.declareProtected(ManagePortal, 'moveDownActions')
-    def moveDownActions(self, selections=(), REQUEST=None):
-        """
-        Moves the specified actions down one slot.
+    security.declareProtected( ManagePortal, 'moveDownActions' )
+    def moveDownActions( self, selections=(), REQUEST=None ):
+
+        """ Move the specified actions down one slot in our list.
         """
-        actions = list(self._actions)
-        sels = list(map(int, selections))  # Convert to a list of integers.
+        sels = list( map( int, selections ) )  # Convert to a list of integers.
         sels.sort()
         sels.reverse()
+
+        actions = list(self._actions)
+
         for idx in sels:
             idx2 = idx + 1
             if idx2 >= len(actions):
@@ -241,7 +250,9 @@
             a = actions[idx2]
             actions[idx2] = actions[idx]
             actions[idx] = a
-        self._actions = actions
+
+        self._actions = tuple( actions )
+
         if REQUEST is not None:
             return self.manage_editActionsForm(
                 REQUEST, manage_tabs_message=(