(イベント)

Description

How to add event hooks to your Plone code to perform actions when something happens on a Plone site.

はじめに

This document briefly discuss about doing event handling using zope.event module. Zope component architecture’s ‘zope.event package <http://pypi.python.org/pypi/zope.event>’ is used to manage subscribeable events in Plone.

There are few notable characters in Plone event system

  • It is simple
  • Subscriber calling order is not modifiable - you cannot set the order how the event handlers are called
  • Event cannot be cancelled - all handlers will get the event always
  • Event handlers cannot have return values
  • Exceptions raised from an event handler will interrupt the request processing

For more information

Registering event handler

Subscribing using five.grok API

Example:

from five import grok
from Products.Archetypes.interfaces import IObjectEditedEvent

class ORAResearcher(folder.ATFolder):

    meta_type = "ORAResearcher"
    schema = ORAResearcherSchema

    def doSomethingDependingOnTheData():
        """
        Do something with the object
        """


@grok.subscribe(ORAResearcher, IObjectEditedEvent)
def object_edited(context, event):
    """
    Catch edit events.

    @param context: Object for which the event was fired
    """

    # Call content type methods
    context.doSomethingDependingOnTheData()

ノート

You need to have five.grok initialization code in your product’s root configure.zcml to be able to use Grok like event subscribers. Otherwise your event handlers won’t get called.

More information

Subscribing in ZCML

Custom event example:

<subscriber
  for=".interfaces.IMyObject
       .interfaces.IMyEvent"
  handler=".content.MyObject.myEventHandler"
  />

Life cycle events example:

<subscriber
  zcml:condition="installed zope.lifecycleevent"
  for=".interfaces.ISitsPatient
       zope.lifecycleevent.IObjectModifiedEvent"
  handler=".content.SitsPatient.objectModified"
  />

Subscribing in Python

The following subscription is valid through the process life cycle. In unit tests, it is important to clear test event handlers between the test steps.

Example:

import zope.component

def my_event_handler(context, event):
    """
    @param context: Zope object for which the event was fired for. Usually this is Plone content object.

    @param event: Subclass of event.
    """
    pass

gsm = zope.component.getGlobalSiteManager()
gsm.registerHandler(my_event_handler, (IMyObject,IMyEvent))

Firing an event

Use zope.event.notify() to fire event objects to their subscribers.

Example how to fire an event in unit tests:

import zope.event
from plone.postpublicationhook.event import AfterPublicationEvent

event = AfterPublicationEvent(self.portal, self.portal.REQUEST)
zope.event.notify(event)

Event types

Creation events

  • Products.Archetypes.interfaces.IObjectInitializedEvent is fired for Archetypes object when it’s being initialised, i.e. populated for the first time
  • Products.Archetypes.interfaces.IWebDAVObjectInitializedEvent is fired for Archetypes object when it’s being initialised via WebDAV
  • zope.lifecycleevent.IObjectCreatedEvent is fired for all Zopeish objects when an object is created (doesn’t necessarily need to be content object)

警告

Archetypes and Zope 3 events might not be compatible with each other. Please see links below.

Other resources

Modified events

Two different content event types are available and might work differently depending on your scenario

  • Products.Archetypes.interfaces.IObjectEditedEvent - called for Archetypes object which are not anymore in the creation stage

ノート

Products.Archetypes.interfaces.IObjectEditedEvent is fired after reindexObject() is called. If you manipulate your content object in a handler for this event, you need to manually reindex new values, or the changes are not reflected back to portal_catalog.

  • zope.lifecycleevent.IObjectModifiedEvent - called for creation stage events too
  • Products.Archetypes.interfaces.IWebDAVObjectEditedEvent - called for Archetypes object when it’s being edited via WebDAV
  • Products.Archetypes.interfaces.IEditBegunEvent - called for Archetypes object when an edit operation is begun
  • Products.Archetypes.interfaces.IEditCancelledEvent - called for Archetypes object when an edit operation is cancelled

Delete events

Delete events can be fired several times for the same object. Some delete event transactions are rolled back.

Copy events

  • zope.lifecycleevent.IObjectCopiedEvent is triggered when an object is copied