[Checkins] SVN: z3c.form/trunk/ Fixed ButtonActions.update() to correctly remove actions.

Marius Gedminas marius at pov.lt
Mon Jul 11 12:43:49 EDT 2011


Log message for revision 122141:
  Fixed ButtonActions.update() to correctly remove actions.
  
  Previous code could have easily broken the invariant
  
    self.items() == zip(self.keys(), self.values())
  
  if you called update() more than once, when one of the following happened:
  
    * a button got disabled (condition started returning False), provided that
      at least one non-disabled button remained -- it would no longer be listed
      in .keys(), but it would be listed in .values()
  
    * all buttons got disabled -- all actions would remain
  
    * a button somewhere in the middle turned enabled -- .keys() would show
      them in correct order, .values() would show it at the end, instead of in
      the middle.
  
  

Changed:
  U   z3c.form/trunk/CHANGES.txt
  U   z3c.form/trunk/src/z3c/form/button.py
  U   z3c.form/trunk/src/z3c/form/button.txt

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2011-07-11 11:55:39 UTC (rev 122140)
+++ z3c.form/trunk/CHANGES.txt	2011-07-11 16:43:47 UTC (rev 122141)
@@ -7,7 +7,10 @@
 
 - Remove unneeded dependency on deprecated ``zope.app.security``.
 
+- Fixed ButtonActions.update() to correctly remove actions when called again,
+  after the button condition become false.
 
+
 2.4.3 (2011-05-20)
 ------------------
 

Modified: z3c.form/trunk/src/z3c/form/button.py
===================================================================
--- z3c.form/trunk/src/z3c/form/button.py	2011-07-11 11:55:39 UTC (rev 122140)
+++ z3c.form/trunk/src/z3c/form/button.py	2011-07-11 16:43:47 UTC (rev 122141)
@@ -259,6 +259,10 @@
             # Step 1: Only create an action for the button, if the condition is
             #         fulfilled.
             if button.condition is not None and not button.condition(self.form):
+                # Step 1.1: If the action already existed, but now the
+                #           condition became false, remove the old action.
+                if name in self._data:
+                    del self._data[name]
                 continue
             # Step 2: Get the action for the given button.
             newButton = True
@@ -288,12 +292,12 @@
             # Step 7: Add the widget to the manager
             uniqueOrderedKeys.append(name)
             if newButton:
-                self._data_values.append(buttonAction)
                 self._data[name] = buttonAction
                 zope.location.locate(buttonAction, self, name)
-            # allways ensure that we add all keys and keep the order given from
-            # button items
-            self._data_keys = uniqueOrderedKeys
+        # always ensure that we add all keys and keep the order given from
+        # button items
+        self._data_keys = uniqueOrderedKeys
+        self._data_values = [self._data[name] for name in uniqueOrderedKeys]
 
 
 class ButtonActionHandler(action.ActionHandlerBase):

Modified: z3c.form/trunk/src/z3c/form/button.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/button.txt	2011-07-11 11:55:39 UTC (rev 122140)
+++ z3c.form/trunk/src/z3c/form/button.txt	2011-07-11 16:43:47 UTC (rev 122141)
@@ -375,7 +375,7 @@
 be converted to an action:
 
   >>> class Form(object):
-  ...     prefix='form'
+  ...     prefix = 'form'
   ...     showApply = True
   ...
   ...     @button.buttonAndHandler(
@@ -395,10 +395,11 @@
 If we set the show-apply attribute to false, the action will not be available.
 
   >>> myform.showApply = False
-  >>> actions = button.ButtonActions(myform, TestRequest(), None)
   >>> actions.update()
   >>> actions.keys()
   []
+  >>> actions.values()
+  []
 
 This feature is very helpful in multi-forms and wizards.
 



More information about the checkins mailing list