Events¶
Grok provides convenient access to a set of often-used events from Zope 3. Those events include object and containment events. All events are available as interface and implemented class.
Subscription: Event interfaces¶
All events interfaces inherit from the base interface of IObjectEvent.
IApplicationInitializedEvent¶
A Grok Application has been created with success and is now ready to be used.
This event can be used to trigger the creation of contents or other tasks that require the application to be fully operational : utilities installed and indexes created in the catalog.
IObjectModifiedEvent¶
An object has been modified. This is a general event that encompasses any change to a persistent object, such as adding, moving, copying, and removing of objects.
IContainerModifiedEvent¶
The container has been modified. Container modifications is specific to addition, removal or reordering of sub-objects. Inherits from grok.IObjectModifiedEvent.
IObjectMovedEvent¶
An object has been moved.
IObjectAddedEvent¶
An object has been added to a container.
- class grok.IObjectAddedEvent¶
Interface to subscribe to for when an object is added to the database.
Inherits from the grok.IObjectMovedEvent interface.
- object¶
The subject of the event.
- oldParent¶
The container stored in before moving.
- oldName¶
The name before moving.
- newParent¶
The container stored in after moving.
- newName¶
The name after moving.
IObjectCopiedEvent¶
An object has been copied.
IObjectCreatedEvent¶
An object has been created. This event is intended to happen before an object has been made persistent, that is it’s location attributes (__name__ and __parent__) will usually be None.
IObjectRemovedEvent¶
An object has been removed from a container.
IBeforeTraverseEvent¶
The publisher is about to traverse into the object.
Notification: Event implementations¶
Event objects are notifications that are sent when need to “fire off” an event.
All of these event objects share the same minimal implementation of an event. This class is defined at zope.component.interfaces.ObjectEvent and looks like this:
from zope import interface
class ObjectEvent(object):
interface.implements(IObjectEvent)
def __init__(self, object):
self.object = object
ApplicationInitializedEvent¶
Event object to send after an application has been created.
ObjectModifiedEvent¶
Event object to send as a notification when an object is modified.
- class grok.ObjectModifiedEvent(object, *descriptions)¶
Default event implementation of the grok.IObjectMovedEvent interface.
- object¶
The subject of the event.
- descriptions¶
A list of descriptions of the modifications.
Example 1: Send an object modification event with a modified attribute named “field”.
import grok
import zope.event
import zope.lifecycleevent.Attributes
from zope.interface import Interface
class ISample(Interface) :
field = Attribute("A test field")
class Sample(object) :
grok.implements(ISample)
obj = Sample()
obj.field = 42
zope.event.notify(
grok.ObjectModifiedEvent(obj,
zope.lifecycleevent.Attributes(ISample, "field"))
)
ContainerModifiedEvent¶
Event object to send as a notification when a container object modified.
ObjectMovedEvent¶
Event object to send as a notification of when an object is moved.
- class grok.ObjectMovedEvent(object, oldParent, oldName, newParent, newName)¶
Default event implementation of the grok.IObjectMovedEvent interface.
- object¶
The subject of the event.
- oldParent¶
The container stored in before moving.
- oldName¶
The name before moving.
- newParent¶
The container stored in after moving.
- newName¶
The name after moving.
ObjectAddedEvent¶
Event object to send as a notification of when an object is added.
ObjectCopiedEvent¶
Event object to send as a notification of when an object is copied.
- class grok.ObjectCopiedEvent(object, original)¶
Default event implementation of the grok.IObjectCopiedEvent interface.
Initialize this event with the new copy and the original object as positional arguments.
- object¶
The subject of the event.
- original¶
The original object from which the copy was made.
ObjectCreatedEvent¶
Event object to send as a notification of when an object is created.
grok.ObjectRemovedEvent¶
Event object to send as a notification of when an object is removed.
