[Checkins] SVN: z3c.form/trunk/ Applied a patch by Carsten Senger that does the following:

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Aug 20 22:15:15 EDT 2008


Log message for revision 90048:
  Applied a patch by Carsten Senger that does the following:
  
  - Feature: Groups now produce detailed `ObjectModifiedEvent` descriptions like
    regular edit forms do.
  
  - Bug: Group classes now implement ``IGroup``. This also helps with the
    detection of group instantiation.
  
  - Bug: The list of changes in a group were updated incorrectly, since it was
    assumed that groups would modify mutually exclusive interfaces. Instead of
    using an overwriting dictionary ``update()`` method, a purely additive merge
    is used now.
  
  

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

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2008-08-21 02:11:22 UTC (rev 90047)
+++ z3c.form/trunk/CHANGES.txt	2008-08-21 02:15:15 UTC (rev 90048)
@@ -5,11 +5,23 @@
 Version 1.9.0 (????-??-??)
 --------------------------
 
+- Feature: Groups now produce detailed `ObjectModifiedEvent` descriptions like
+  regular edit forms do. (Thanks to Carsten Senger for providing a patch.)
+
 - Feature: The widget manager's ``extract()`` method now supports an optional
   ``setErrors`` (default value: True) flag that allows one to not set errors
   on the widgets and widget manager during data extraction. Use case: You want
   to inspect the entered data and handle errors manually.
 
+- Bug: Group classes now implement ``IGroup``. This also helps with the
+  detection of group instantiation. (Thanks to Carsten Senger for providing a
+  patch.)
+
+- Bug: The list of changes in a group were updated incorrectly, since it was
+  assumed that groups would modify mutually exclusive interfaces. Instead of
+  using an overwriting dictionary ``update()`` method, a purely additive merge
+  is used now. (Thanks to Carsten Senger for providing a patch.)
+
 - Bug: Added a widget for ``IDecimal`` field in testing setup.
 
 - Feature: The ``z3c.form.util`` module has a new function, ``createCSSId()``

Modified: z3c.form/trunk/src/z3c/form/group.py
===================================================================
--- z3c.form/trunk/src/z3c/form/group.py	2008-08-21 02:11:22 UTC (rev 90047)
+++ z3c.form/trunk/src/z3c/form/group.py	2008-08-21 02:15:15 UTC (rev 90048)
@@ -19,8 +19,10 @@
 import zope.component
 
 from z3c.form import form, interfaces
+from zope.interface import implements
 
 class Group(form.BaseForm):
+    implements(interfaces.IGroup)
 
     def __init__(self, context, request, parentForm):
         self.context = context
@@ -59,15 +61,22 @@
 
     def applyChanges(self, data):
         '''See interfaces.IEditForm'''
+        descriptions = []
         content = self.getContent()
         changed = form.applyChanges(self, content, data)
         for group in self.groups:
             groupContent = group.getContent()
             groupChanged = form.applyChanges(group, groupContent, data)
-            changed.update(groupChanged)
+            for interface, names in groupChanged.items():
+                changed[interface] = changed.get(interface, []) + names
         if changed:
+            for interface, names in changed.items():
+                descriptions.append(
+                    zope.lifecycleevent.Attributes(interface, *names))
+            # Send out a detailed object-modified event
             zope.event.notify(
-                zope.lifecycleevent.ObjectModifiedEvent(content))
+                zope.lifecycleevent.ObjectModifiedEvent(content, *descriptions))
+
         return changed
 
     def update(self):
@@ -77,10 +86,10 @@
         for groupClass in self.groups:
             # only instantiate the groupClass if it hasn't already
             # been instantiated
-            if isinstance(groupClass, type):
+            if interfaces.IGroup.providedBy(groupClass):
+                group = groupClass
+            else:
                 group = groupClass(self.context, self.request, self)
-            else:
-                group = groupClass
             group.update()
             groups.append(group)
         self.groups = tuple(groups)

Modified: z3c.form/trunk/src/z3c/form/group.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/group.txt	2008-08-21 02:11:22 UTC (rev 90047)
+++ z3c.form/trunk/src/z3c/form/group.txt	2008-08-21 02:15:15 UTC (rev 90048)
@@ -403,6 +403,18 @@
     </body>
   </html>
 
+When an edit form with groups is successfully committed, a detailed 
+object-modified event is sent out telling the system about the changes.
+ To see the error, let's create an event subscriber for object-modified events:
+
+  >>> eventlog = []
+  >>> import zope.lifecycleevent
+  >>> @zope.component.adapter(zope.lifecycleevent.ObjectModifiedEvent)
+  ... def logEvent(event):
+  ...     eventlog.append(event)
+  >>> zope.component.provideHandler(logEvent)
+
+
 Let's now complete the form successfully:
 
   >>> request = testing.TestRequest(form={
@@ -446,6 +458,21 @@
   >>> reg.year
   2006
 
+Let's look at the event:
+ 
+  >>> event = eventlog[-1]
+  >>> event
+  <zope.app.event.objectevent.ObjectModifiedEvent object at ...>
+
+The event's description contains the changed Interface and the names of
+all changed fields, even if they where in different groups:
+
+  >>> attrs = event.descriptions[0]
+  >>> attrs.interface
+  <InterfaceClass __builtin__.IVehicleRegistration>
+  >>> attrs.attributes
+  ('license', 'address', 'model', 'make', 'year')
+
 And that's it!
 
 



More information about the Checkins mailing list