Skip to main content

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]

Release files for plone.postpublicationhook 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for plone.postpublicationhook 1.1
File Size Uploaded
plone.postpublicationhook-1.1.zip 10.5 kB Details

Release files / plone.postpublicationhook-1.1.zip

Download URL plone.postpublicationhook-1.1.zip
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6a8b7f217cfdcbe181efe14be08579587bf7be8b81ed90295f067a4689b6fb09
BLAKE2b-256 checksum
How to use checksums
901657a5a38e1c89058abcef0b4c8a0cff8c9a09ebd7f356fe3ea1c5b9c8fc06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page