Event handler

by Maurits van Rees last modified Jun 15, 2011 10:50 AM
zmcl and code for the event handlers

zcml registration

Okay, for the general keywordannotator product we want an event handler that gets activated whenever an object implementing the IAttributeAnnotatable interface gets modified or added. We register that in the configure.zcml file like this:

<subscriber
 for="zope.app.annotation.interfaces.IAttributeAnnotatable
      zope.app.event.interfaces.IObjectModifiedEvent"
 handler=".events.annotationEventHandler" />

That takes care of modified objects. Not shown here is the subscriber for added objects. It looks the same except that we replace 'IObjectModifiedEvent' with 'IObjectCreatedEvent'.

In the case of quadapter we want an event handler that gets activated only for object implementing the IWeblogEntry interface:

<subscriber
 for="Products.Quills.interfaces.IWeblogEntry
      zope.app.event.interfaces.IObjectModifiedEvent"
 handler="Products.keywordannotator.events.annotationEventHandler" />

Event handler code

In keywordannotator the code is this:

def annotationEventHandler(ob, event):
    from zope.component import getUtility
    decider = getUtility(IAnnotationDecider, context=ob)
    if decider.matchesKeywords(ob):
        decider.provideInterfaces(ob)

So this event handler looks for a utility. That utility tells us whether the object the event was fired for has a keyword that has been marked as special in our code or not. If this condition has been met, we instruct the utility to make sure that the object now provides a few more interfaces.

Note: quadapter simply uses this event handler from keywordannotator without overwriting it.

Now let's look at what that utility looks like.