[CMF-checkins] CVS: CMF/CMFCore - ActionInformation.py:1.7 ActionProviderBase.py:1.9

Tres Seaver tseaver@zope.com
Wed, 3 Jul 2002 10:07:40 -0400


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

Modified Files:
	ActionInformation.py ActionProviderBase.py 
Log Message:



  - ActionInformation:

    o Added 'clone' method.

  - ActionProviderBase:

    o Always clone actions when modifying the list, to avoid sharing
      action instances between the class (where they are regenerated
      on restart) and its instance (where the actions are persistent).


=== CMF/CMFCore/ActionInformation.py 1.6 => 1.7 ===
         return self.visible
 
+    security.declarePrivate( 'clone' )
+    def clone( self ):
+
+        """ Return a newly-created AI just like us.
+        """
+        return self.__class__( id=self.id
+                             , title=self.title
+                             , description=self.description
+                             , category =self.category
+                             , condition=self.condition
+                             , permissions=self.permissions
+                             , priority =self.priority
+                             , visible=self.visible
+                             , action=self.action
+                             )
+
 InitializeClass( ActionInformation )
 
 class oai:


=== CMF/CMFCore/ActionProviderBase.py 1.8 => 1.9 ===
                      )
 
+    #
+    #   ActionProvider interface
+    #
     security.declarePrivate( 'listActions' )
     def listActions( self, info=None ):
 
@@ -47,6 +50,9 @@
         """
         return self._actions or None
 
+    #
+    #   ZMI methods
+    #
     security.declareProtected( ManagePortal, 'manage_editActionsForm' )
     def manage_editActionsForm( self, REQUEST, manage_tabs_message=None ):
 
@@ -107,62 +113,23 @@
         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)
-                                )
+        new_actions = self._cloneActions()
+
+        new_action = 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 )
+        new_actions.append( new_action )
+        self._actions = tuple( new_actions )
 
         if REQUEST is not None:
             return self.manage_editActionsForm(
                 REQUEST, manage_tabs_message='Added.')
-    
-    security.declarePrivate( '_extractAction' )
-    def _extractAction( self, properties, index ):
-
-        """ Extract an ActionInformation from the funky form properties.
-        """
-        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 ):
@@ -189,14 +156,15 @@
         """ Delete actions indicated by indexes in 'selections'.
         """
         sels = list( map( int, selections ) )  # Convert to a list of integers.
-        sels.sort()
-        sels.reverse()  # Delete from end to avoid stepping on indexes
 
-        actions = list( self._actions )
-        for idx in sels:
-            del actions[idx]
+        old_actions = self._cloneActions()
+        new_actions = []
 
-        self._actions = tuple( actions )
+        for index in range( len( old_actions ) ):
+            if index not in sels:
+                new_actions.append( old_actions[ index ] )
+
+        self._actions = tuple( new_actions )
 
         if REQUEST is not None:
             return self.manage_editActionsForm(
@@ -211,17 +179,17 @@
         sels = list( map( int, selections ) )  # Convert to a list of integers.
         sels.sort()
 
-        actions = list( self._actions )
+        new_actions = self._cloneActions()
 
         for idx in sels:
             idx2 = idx - 1
             if idx2 < 0:
                 # Wrap to the bottom.
-                idx2 = len(actions) - 1
+                idx2 = len(new_actions) - 1
             # Swap.
-            a = actions[idx2]
-            actions[idx2] = actions[idx]
-            actions[idx] = a
+            a = new_actions[idx2]
+            new_actions[idx2] = new_actions[idx]
+            new_actions[idx] = a
 
         self._actions = tuple( actions )
 
@@ -239,21 +207,71 @@
         sels.sort()
         sels.reverse()
 
-        actions = list(self._actions)
+        new_actions = self._cloneActions()
 
         for idx in sels:
             idx2 = idx + 1
-            if idx2 >= len(actions):
+            if idx2 >= len(new_actions):
                 # Wrap to the top.
                 idx2 = 0
             # Swap.
             a = actions[idx2]
-            actions[idx2] = actions[idx]
-            actions[idx] = a
+            new_actions[idx2] = new_actions[idx]
+            new_actions[idx] = a
 
-        self._actions = tuple( actions )
+        self._actions = tuple( new_actions )
 
         if REQUEST is not None:
             return self.manage_editActionsForm(
                 REQUEST, manage_tabs_message=(
                 'Moved down %d action(s).' % len(sels)))
+
+    #
+    #   Helper methods
+    #
+    security.declarePrivate( '_cloneActions' )
+    def _cloneActions( self ):
+
+        """ Return a list of actions, cloned from our current list.
+        """
+        return map( lambda x: x.clone(), list( self._actions ) )
+ 
+    security.declarePrivate( '_extractAction' )
+    def _extractAction( self, properties, index ):
+
+        """ Extract an ActionInformation from the funky form properties.
+        """
+        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
+                                )