Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install plone.postpublicationhook

How to install plone.postpublicationhook

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install plone.postpublicationhook
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.1 Available View build log
Windows (64-bit)
1.1 Available View build log
Mac OS X (10.5+)
1.1 Available View build log
Linux (32-bit)
1.1 Available View build log
Linux (64-bit)
1.1 Available View build log
Web
 
License
ZPL
Lastest release
version 1.1 on Jan 5th, 2011

Note

This package is provided for backwards compatibility. New code should use the publication events introduced in Zope 2.12 directly.

For Zope 2.10, a backport of the publication events is available in ZPublisherEventsBackport. This is required for this package and may be added to your buildout directly, or by specifying the 'Zope2.10' extra:

eggs =
    Plone
    plone.postpublicationhook [Zope2.10]

Introduction

This package provides a hook into Zope's ZPublisher that is run after the publisher has completed publication, but before the the transaction is committed and the response is returned to the requesting browser. This is practical for caching purposes: it is the ideal place to determine and insert caching headers into the response.

Hooks use zope.event's event mechanism using the plone.validatehook.interfaces.IPostValidationEvent. This is based on the standard ObjectEvent from zope.component.

Example

As an example we will write a bit of code which logs the path of every published object. This is the code for the event handler:

from zope.interface import Interface
from zope.component import adapter
from plone.postpublicationhook.interfaces import IAfterPublicationEvent
import logging

logger = logging.getLogger("LogRequest")

@adapter(Interface, IAfterPublicationEvent)
def LogRequest(object, event):
    if getattr(object, "getPhysicalPath", None) is None:
        path="Unknown path"
    else:
        path="/".join(object.getPhysicalPath()

    logger.info("Request for object %s" % path)

To use this code you need to register it in zcml:

<subscriber handler=".events.LogRequest" />

Using ZPublisher events directly

The IPubBeforeCommit event is equivalent to the IAfterPublicationEvent, however it is not an ObjectEvent so there are a few changes:

from zope.component import adapter
from ZPublisher.interfaces import IPubBeforeCommit
import logging

logger = logging.getLogger("LogRequest")

@adapter(IPubBeforeCommit)
def LogRequest(event):
    request = event.request
    object = request['PUBLISHED']
    if getattr(object, "getPhysicalPath", None) is None:
        path="Unknown path"
    else:
        path="/".join(object.getPhysicalPath()

    logger.info("Request for object %s" % path)

Register it in zcml the same way:

<subscriber handler=".events.LogRequest" />

Changelog

1.1 - 2010-06-08
  • General and metadata cleanup. [hannosch]
  • Refactor to use ZPublisher publication events (available in Zope 2.10 with ZPublisherEventsBackport.) [lrowe]
1.0rc1 - 2008-10-15
  • Initial release [wichert]

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:4

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.