pythontestingevent-handlingplonez3c.form

How to programatically create a detailed event like z3c.form does?


I have a simple event handler that looks for what has actually been changed (it's registered for a IObjectModifiedEvent events), the code looks like:

def on_change_do_something(obj, event):
    modified = False
    # check if the publication has changed
    for change in event.descriptions:
        if change.interface == IPublication:
            modified = True
            break

    if modified:
        # do something

So my question is: how can I programmatically generate those descriptions? I'm using plone.app.dexterity everywhere, so z3c.form is doing that automagically when using a form, but I want to test it with a unittest.


Solution

  • event.description is nominally an IModificationDescription object, which is essentially a list of IAttributes objects: each Attributes object having an interface (e.g. schema) and attributes (e.g. list of field names) modified.

    Simplest solution is to create a zope.lifecycleevent.Attributes object for each field changed, and pass as arguments to the event constructor -- example:

    # imports elided...
    
    changelog = [
        Attributes(IFoo, 'some_fieldname_here'),
        Attributes(IMyBehaviorHere, 'some_behavior_provided_fieldname_here',
        ]
    notify(ObjectModifiedEvent(context, *changelog)