Move ITransactionManager adapter from ztfy.scheduler ZTK-1.1
authorThierry Florac <tflorac@ulthar.net>
Mon, 02 Apr 2012 09:07:28 +0200
branchZTK-1.1
changeset 138 05cfdcb2418d
parent 137 8e70a18787e1
child 139 6478aac3b2bd
Move ITransactionManager adapter from ztfy.scheduler
ztfy/utils/configure.zcml
ztfy/utils/zodb.py
--- a/ztfy/utils/configure.zcml	Sat Mar 31 01:32:08 2012 +0200
+++ b/ztfy/utils/configure.zcml	Mon Apr 02 09:07:28 2012 +0200
@@ -5,6 +5,10 @@
 
 	<i18n:registerTranslations directory="locales" />
 
+	<!-- IPersistent adapters copied from zc.twist package -->
+	<adapter
+		factory=".zodb.transactionManager" />
+
 	<!-- Encodings management -->
 	<utility
 		name="ZTFY encodings"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/zodb.py	Mon Apr 02 09:07:28 2012 +0200
@@ -0,0 +1,46 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008-2012 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+__docformat__ = "restructuredtext"
+
+# import standard packages
+
+# import Zope3 interfaces
+from persistent.interfaces import IPersistent
+from transaction.interfaces import ITransactionManager
+from ZODB.interfaces import IConnection
+
+# import local interfaces
+
+# import Zope3 packages
+from zope.component import adapter
+from zope.interface import implementer
+
+# import local packages
+
+
+# IPersistent adapters copied from zc.twist package
+# also register this for adapting from IConnection
+@adapter(IPersistent)
+@implementer(ITransactionManager)
+def transactionManager(obj):
+    conn = IConnection(obj) # typically this will be
+                            # zope.app.keyreference.persistent.connectionOfPersistent
+    try:
+        return conn.transaction_manager
+    except AttributeError:
+        return conn._txn_mgr
+        # or else we give up; who knows.  transaction_manager is the more
+        # recent spelling.