[Zope3-Users] Re: z3c.form, applyChanges, IModifiedEvent
Philipp von Weitershausen
philipp at weitershausen.de
Mon Aug 13 03:31:14 EDT 2007
Darryl Cousins wrote:
> Following a post on grok-dev I learnt about additional arguments to
> ModifiedEvent.
>
> http://mail.zope.org/pipermail/grok-dev/2007-August/001807.html
>
> By changing z3c.form.form.applyChanges to return a dictionary of
> interfaces and names of the attributes changed (instead of the current
> bool value) then I can easily create the correct additional attributes
> within the applyChanges method of EditForm:
...
Sadly, zope.formlib doesn't send this info along either, therefore
setting a bad example. I filed
https://bugs.launchpad.net/zope3/+bug/98483 a while ago, just never got
around to fix it... Thanks for reminding.
>
> def applyChanges(form, content, data):
> changes = {}
> for name, field in form.fields.items():
> # If the field is not in the data, then go on to the next one
> if name not in data:
> continue
> # Get the datamanager and get the original value
> dm = zope.component.getMultiAdapter(
> (content, field.field), IDataManager)
> oldValue = dm.get()
> # Only update the data, if it is different
> if dm.get() != data[name]:
> dm.set(data[name])
> # get the interface and collect changed attribute names
> changes.setdefault(dm.field.interface, []).append(name)
> return changes
>
>
> class EditForm(Form):
>
> ....
>
> def applyChanges(self, data):
> content = self.getContent()
> changes = applyChanges(self, content, data)
> if changes: # testing a dict as a bool is prone to error???
> descriptions = []
> for interface, names in changes.items():
> descriptions.append(zope.lifecycleevent.Attributes(interface,
> *names))
> zope.event.notify(
> zope.lifecycleevent.ObjectModifiedEvent(content, *descriptions))
> return changes
>
> Hopefully pypi will be back up again later today so I can do a fresh
> build of z3c.form and run the tests with my changes as set out above.
>
> Best regards,
> Darryl Cousins
--
http://worldcookery.com -- Professional Zope documentation and training
More information about the Zope3-users
mailing list