[Checkins] SVN: plone.postpublicationhook/trunk/ Refactor to use the new ZPublisher publication events

Laurence Rowe l at lrowe.co.uk
Fri Nov 6 12:32:39 EST 2009


Log message for revision 105504:
  Refactor to use the new ZPublisher publication events

Changed:
  U   plone.postpublicationhook/trunk/README.txt
  U   plone.postpublicationhook/trunk/docs/HISTORY.txt
  U   plone.postpublicationhook/trunk/plone/postpublicationhook/__init__.py
  U   plone.postpublicationhook/trunk/plone/postpublicationhook/configure.zcml
  U   plone.postpublicationhook/trunk/plone/postpublicationhook/event.py
  D   plone.postpublicationhook/trunk/plone/postpublicationhook/hook.py
  U   plone.postpublicationhook/trunk/setup.py

-=-
Modified: plone.postpublicationhook/trunk/README.txt
===================================================================
--- plone.postpublicationhook/trunk/README.txt	2009-11-06 08:21:09 UTC (rev 105503)
+++ plone.postpublicationhook/trunk/README.txt	2009-11-06 17:32:39 UTC (rev 105504)
@@ -1,3 +1,17 @@
+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
 ============
 
@@ -38,6 +52,34 @@
 
     <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" />
+
 .. _zope.event: http://pypi.python.org/pypi/zope.event
 .. _zope.component: http://pypi.python.org/pypi/zope.component
+.. ZPublisherEventsBackport: http://pypi.python.org/pypi/ZPublisherEventsBackport

Modified: plone.postpublicationhook/trunk/docs/HISTORY.txt
===================================================================
--- plone.postpublicationhook/trunk/docs/HISTORY.txt	2009-11-06 08:21:09 UTC (rev 105503)
+++ plone.postpublicationhook/trunk/docs/HISTORY.txt	2009-11-06 17:32:39 UTC (rev 105504)
@@ -1,6 +1,12 @@
 Changelog
 =========
 
+1.0rc2 - Unreleased
+-------------------------
+
+* Backport ZPublisher publication events from Zope 2.12
+  [lrowe]
+
 1.0rc1 - October 15, 2008
 -------------------------
 

Modified: plone.postpublicationhook/trunk/plone/postpublicationhook/__init__.py
===================================================================
--- plone.postpublicationhook/trunk/plone/postpublicationhook/__init__.py	2009-11-06 08:21:09 UTC (rev 105503)
+++ plone.postpublicationhook/trunk/plone/postpublicationhook/__init__.py	2009-11-06 17:32:39 UTC (rev 105504)
@@ -1,5 +1 @@
-
-def initialize(context):
-    from plone.postpublicationhook.hook import InstallHook
-
-    InstallHook()
+# make it a package

Modified: plone.postpublicationhook/trunk/plone/postpublicationhook/configure.zcml
===================================================================
--- plone.postpublicationhook/trunk/plone/postpublicationhook/configure.zcml	2009-11-06 08:21:09 UTC (rev 105503)
+++ plone.postpublicationhook/trunk/plone/postpublicationhook/configure.zcml	2009-11-06 17:32:39 UTC (rev 105504)
@@ -1,7 +1,15 @@
 <configure
     xmlns="http://namespaces.zope.org/zope"
-    xmlns:five="http://namespaces.zope.org/five">
+    xmlns:zcml="http://namespaces.zope.org/zcml">
 
-  <five:registerPackage package="." initialize=".initialize" />
+    <include
+        zcml:condition="not-installed ZPublisher.interfaces"
+        package="ZPublisherEventsBackport"
+        />
 
+    <subscriber
+        for="ZPublisher.interfaces.IPubBeforeCommit"
+        handler=".event.redispatch"
+        />
+
 </configure>

Modified: plone.postpublicationhook/trunk/plone/postpublicationhook/event.py
===================================================================
--- plone.postpublicationhook/trunk/plone/postpublicationhook/event.py	2009-11-06 08:21:09 UTC (rev 105503)
+++ plone.postpublicationhook/trunk/plone/postpublicationhook/event.py	2009-11-06 17:32:39 UTC (rev 105504)
@@ -1,5 +1,6 @@
 from zope.interface import implements
 from zope.component.interfaces import ObjectEvent
+from zope.event import notify
 from plone.postpublicationhook.interfaces import IAfterPublicationEvent
 
 
@@ -11,3 +12,9 @@
         self.request=request
 
 
+def redispatch(event):
+    """Redispatch IPubBeforeCommit as IAfterPublicationEvent
+    """
+    request = event.request
+    object = request['PUBLISHED']
+    notify(AfterPublicationEvent(object, request))

Deleted: plone.postpublicationhook/trunk/plone/postpublicationhook/hook.py
===================================================================
--- plone.postpublicationhook/trunk/plone/postpublicationhook/hook.py	2009-11-06 08:21:09 UTC (rev 105503)
+++ plone.postpublicationhook/trunk/plone/postpublicationhook/hook.py	2009-11-06 17:32:39 UTC (rev 105504)
@@ -1,147 +0,0 @@
-import logging
-import sys
-from ZPublisher.Publish import call_object
-from ZPublisher.Publish import missing_name
-from ZPublisher.Publish import dont_publish_class
-from ZPublisher.Publish import get_module_info
-from ZPublisher.Publish import Retry
-from ZPublisher.mapply import mapply
-from zope.publisher.browser import setDefaultSkin
-from zope.security.management import newInteraction
-from zope.security.management import endInteraction
-from zExceptions import Redirect
-
-import zope.event
-from plone.postpublicationhook.event import AfterPublicationEvent
-
-
-def publish(request, module_name, after_list, debug=0,
-            # Optimize:
-            call_object=call_object,
-            missing_name=missing_name,
-            dont_publish_class=dont_publish_class,
-            mapply=mapply,
-            ):
-
-    (bobo_before, bobo_after, object, realm, debug_mode, err_hook,
-     validated_hook, transactions_manager)= get_module_info(module_name)
-
-    parents=None
-    response=None
-
-    try:
-        # TODO pass request here once BaseRequest implements IParticipation
-        newInteraction()
-
-        request.processInputs()
-
-        request_get=request.get
-        response=request.response
-
-        # First check for "cancel" redirect:
-        if request_get('SUBMIT','').strip().lower()=='cancel':
-            cancel=request_get('CANCEL_ACTION','')
-            if cancel:
-                raise Redirect, cancel
-
-        after_list[0]=bobo_after
-        if debug_mode:
-            response.debug_mode=debug_mode
-        if realm and not request.get('REMOTE_USER',None):
-            response.realm=realm
-
-        if bobo_before is not None:
-            bobo_before()
-
-        # Get the path list.
-        # According to RFC1738 a trailing space in the path is valid.
-        path=request_get('PATH_INFO')
-
-        request['PARENTS']=parents=[object]
-
-        if transactions_manager:
-            transactions_manager.begin()
-
-        object=request.traverse(path, validated_hook=validated_hook)
-
-        if transactions_manager:
-            transactions_manager.recordMetaData(object, request)
-
-        result=mapply(object, request.args, request,
-                      call_object,1,
-                      missing_name,
-                      dont_publish_class,
-                      request, bind=1)
-
-        if result is not response:
-            response.setBody(result)
-
-        # This is the only change from the canonical publish method
-        zope.event.notify(AfterPublicationEvent(object, request))
-
-        if transactions_manager:
-            transactions_manager.commit()
-        endInteraction()
-
-        return response
-    except:
-        # DM: provide nicer error message for FTP
-        sm = None
-        if response is not None:
-            sm = getattr(response, "setMessage", None)
-
-        if sm is not None:
-            from asyncore import compact_traceback
-            cl,val= sys.exc_info()[:2]
-            sm('%s: %s %s' % (
-                getattr(cl,'__name__',cl), val,
-                debug_mode and compact_traceback()[-1] or ''))
-
-        if err_hook is not None:
-            if parents:
-                parents=parents[0]
-            try:
-                try:
-                    return err_hook(parents, request,
-                                    sys.exc_info()[0],
-                                    sys.exc_info()[1],
-                                    sys.exc_info()[2],
-                                    )
-                except Retry:
-                    if not request.supports_retry():
-                        return err_hook(parents, request,
-                                        sys.exc_info()[0],
-                                        sys.exc_info()[1],
-                                        sys.exc_info()[2],
-                                        )
-            finally:
-                if transactions_manager:
-                    transactions_manager.abort()
-                endInteraction()
-
-            # Only reachable if Retry is raised and request supports retry.
-            newrequest=request.retry()
-            request.close()  # Free resources held by the request.
-            # Set the default layer/skin on the newly generated request
-            setDefaultSkin(newrequest)
-            try:
-                return publish(newrequest, module_name, after_list, debug)
-            finally:
-                newrequest.close()
-
-        else:
-            if transactions_manager:
-                transactions_manager.abort()
-            endInteraction()
-            raise
-
-
-
-def InstallHook():
-    """Install our own publish method."""
-    from plone.postpublicationhook import hook
-    import ZPublisher.Publish
-
-    ZPublisher.Publish.publish=hook.publish
-    logging.info("Monkeypatch ZPublisher publish method to send AfterPublicationEvent")
-

Modified: plone.postpublicationhook/trunk/setup.py
===================================================================
--- plone.postpublicationhook/trunk/setup.py	2009-11-06 08:21:09 UTC (rev 105503)
+++ plone.postpublicationhook/trunk/setup.py	2009-11-06 17:32:39 UTC (rev 105504)
@@ -1,7 +1,7 @@
 from setuptools import setup, find_packages
 import os.path
 
-version = '1.0'
+version = '1.1'
 
 setup(name='plone.postpublicationhook',
       version=version,
@@ -31,5 +31,8 @@
           'zope.interface',
           'zope.security',
       ],
+      extras_require={
+          'Zope2.10': ['ZPublisherEventsBackport'],
+      },
       )
 



More information about the checkins mailing list