Skip to main content

Creating and subscribing to events in Zope

January 31, 2009

Creating your event

  • An event is just an interface definition and the event object declaration
    import zope.component.interfaces
    from zope.interface import implements
    
    class IMyNewEvent(zope.component.interfaces.IObjectEvent):
        """An object has been modified"""
    
    class MyNewEvent(zope.component.interfaces.ObjectEvent):
        """An object has been modified"""
        implements(IMyNewEvent)
    
  • The real magic happens when fire off the event
    from zope.event import notify
    
    notify(MyNewEvent(context))
  • Now to subscribe to the event do something like this,
    <subscriber
      for="myproduct.myobject
           myproduct.interfaces.IMyNewEvent"
      handler=".subscribers.event_handler"
      />
  • Now in a file subscribers.py add the method event_handler like this,
    def event_handler(obj, event):
      # do something interesting here...
      pass