diff --git a/product/ERP5Type/Base.py b/product/ERP5Type/Base.py index fcc3e8e3cf43292751c1bfee82b2bff20900b8cc..9b0e3b7252afe562d387feb62ec91aff5ce1db27 100644 --- a/product/ERP5Type/Base.py +++ b/product/ERP5Type/Base.py @@ -675,7 +675,7 @@ class Base( CopyContainer, going to edit the related object """ # Push context to prevent loop - tv = getTransactionalVariable() + tv = getTransactionalVariable(self) if isinstance(portal_type, list): portal_type = tuple(portal_type) acquisition_key = ('_getDefaultAcquiredProperty', self.getPath(), key, base_category, @@ -794,7 +794,7 @@ class Base( CopyContainer, """ # Push context to prevent loop - tv = getTransactionalVariable() + tv = getTransactionalVariable(self) if isinstance(portal_type, list): portal_type = tuple(portal_type) acquisition_key = ('_getAcquiredPropertyList', self.getPath(), key, base_category, diff --git a/product/ERP5Type/Cache.py b/product/ERP5Type/Cache.py index 1c18667fc3181985653fec75add073faa419bfa4..2d4d2c62e7ce19874c0c073bccf4af3eeea4bf89 100644 --- a/product/ERP5Type/Cache.py +++ b/product/ERP5Type/Cache.py @@ -225,7 +225,7 @@ allow_class(CachingMethod) def getReadOnlyTransactionCache(context): """Get the transaction cache. """ - tv = getTransactionalVariable() + tv = getTransactionalVariable(context) try: return tv['read_only_transaction_cache'] except KeyError: @@ -234,13 +234,13 @@ def getReadOnlyTransactionCache(context): def enableReadOnlyTransactionCache(context): """Enable the transaction cache. """ - tv = getTransactionalVariable() + tv = getTransactionalVariable(context) tv['read_only_transaction_cache'] = {} def disableReadOnlyTransactionCache(context): """Disable the transaction cache. """ - tv = getTransactionalVariable() + tv = getTransactionalVariable(context) try: del tv['read_only_transaction_cache'] except KeyError: diff --git a/product/ERP5Type/TransactionalVariable.py b/product/ERP5Type/TransactionalVariable.py index 7ccce4e2386b63c3c1841463d2de55d6cc65cc0b..52c433c0bafd1b912e37b9c2d67c3147f42900ed 100644 --- a/product/ERP5Type/TransactionalVariable.py +++ b/product/ERP5Type/TransactionalVariable.py @@ -66,13 +66,7 @@ class TransactionalVariable(TM, IterableUserDict): _finalize = None def _begin(self, *ignored): - """It is required to attach this instance to somewhere in - ZODB so that _finish or _abort will be called at the end - of a transaction. A portal object is used at the moment.""" - # Is there any other way to retrieve a portal object? - from Products.ERP5Type.Utils import get_request - portal = get_request()['PARENTS'].getPortalObject() - portal._v_erp5_transactional_variable = self + pass def _finish(self, *ignored): self.clear() @@ -80,16 +74,23 @@ class TransactionalVariable(TM, IterableUserDict): def _abort(self, *ignored): self.clear() + def __hash__(self): + return hash(id(self)) + def __setitem__(self, key, value): IterableUserDict.__setitem__(self, key, value) self._register() transactional_variable_pool = local() -def getTransactionalVariable(): +def getTransactionalVariable(context): """Return a transactional variable.""" + portal = context.portal_url.getPortalObject() try: - return transactional_variable_pool.instance + instance = transactional_variable_pool.instance + if getattr(portal, '_v_erp5_transactional_variable', None) is not instance: + portal._v_erp5_transactional_variable = instance + return instance except AttributeError: transactional_variable_pool.instance = TransactionalVariable() - return transactional_variable_pool.instance + return getTransactionalVariable() diff --git a/product/ERP5Type/tests/testTransactionalVariable.py b/product/ERP5Type/tests/testTransactionalVariable.py index 69597964bc73e05454d60e49757170432b77740c..3b989d9e096f315d7d46c504314661b753f75cfa 100644 --- a/product/ERP5Type/tests/testTransactionalVariable.py +++ b/product/ERP5Type/tests/testTransactionalVariable.py @@ -73,7 +73,7 @@ class TestTransactionalVariable(ERP5TypeTestCase, LogInterceptor): ZopeTestCase._print('\n '+message) LOG('Testing... ', 0, message) - tv = getTransactionalVariable() + tv = getTransactionalVariable(self.getPortal()) self.failIfEqual(tv, None) # Test frequently used dict methods. This does not cover everything, @@ -106,7 +106,7 @@ class TestTransactionalVariable(ERP5TypeTestCase, LogInterceptor): ZopeTestCase._print('\n '+message) LOG('Testing... ', 0, message) - tv = getTransactionalVariable() + tv = getTransactionalVariable(self.getPortal()) self.failIfEqual(tv, None) tv.clear() @@ -134,7 +134,7 @@ class TestTransactionalVariable(ERP5TypeTestCase, LogInterceptor): ZopeTestCase._print('\n '+message) LOG('Testing... ', 0, message) - tv = getTransactionalVariable() + tv = getTransactionalVariable(self.getPortal()) self.failIfEqual(tv, None) tv.clear()