Commit 781e79ef authored by Yoshinori Okuji's avatar Yoshinori Okuji

Pass context to getTransactionalVariable rather than using a request. Add...

Pass context to getTransactionalVariable rather than using a request. Add __hash__ so that a transaction can register TransactionalVariable. Make sure that a portal object retains the right object.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@13817 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 7e8f2de2
...@@ -675,7 +675,7 @@ class Base( CopyContainer, ...@@ -675,7 +675,7 @@ class Base( CopyContainer,
going to edit the related object going to edit the related object
""" """
# Push context to prevent loop # Push context to prevent loop
tv = getTransactionalVariable() tv = getTransactionalVariable(self)
if isinstance(portal_type, list): if isinstance(portal_type, list):
portal_type = tuple(portal_type) portal_type = tuple(portal_type)
acquisition_key = ('_getDefaultAcquiredProperty', self.getPath(), key, base_category, acquisition_key = ('_getDefaultAcquiredProperty', self.getPath(), key, base_category,
...@@ -794,7 +794,7 @@ class Base( CopyContainer, ...@@ -794,7 +794,7 @@ class Base( CopyContainer,
""" """
# Push context to prevent loop # Push context to prevent loop
tv = getTransactionalVariable() tv = getTransactionalVariable(self)
if isinstance(portal_type, list): if isinstance(portal_type, list):
portal_type = tuple(portal_type) portal_type = tuple(portal_type)
acquisition_key = ('_getAcquiredPropertyList', self.getPath(), key, base_category, acquisition_key = ('_getAcquiredPropertyList', self.getPath(), key, base_category,
......
...@@ -225,7 +225,7 @@ allow_class(CachingMethod) ...@@ -225,7 +225,7 @@ allow_class(CachingMethod)
def getReadOnlyTransactionCache(context): def getReadOnlyTransactionCache(context):
"""Get the transaction cache. """Get the transaction cache.
""" """
tv = getTransactionalVariable() tv = getTransactionalVariable(context)
try: try:
return tv['read_only_transaction_cache'] return tv['read_only_transaction_cache']
except KeyError: except KeyError:
...@@ -234,13 +234,13 @@ def getReadOnlyTransactionCache(context): ...@@ -234,13 +234,13 @@ def getReadOnlyTransactionCache(context):
def enableReadOnlyTransactionCache(context): def enableReadOnlyTransactionCache(context):
"""Enable the transaction cache. """Enable the transaction cache.
""" """
tv = getTransactionalVariable() tv = getTransactionalVariable(context)
tv['read_only_transaction_cache'] = {} tv['read_only_transaction_cache'] = {}
def disableReadOnlyTransactionCache(context): def disableReadOnlyTransactionCache(context):
"""Disable the transaction cache. """Disable the transaction cache.
""" """
tv = getTransactionalVariable() tv = getTransactionalVariable(context)
try: try:
del tv['read_only_transaction_cache'] del tv['read_only_transaction_cache']
except KeyError: except KeyError:
......
...@@ -66,13 +66,7 @@ class TransactionalVariable(TM, IterableUserDict): ...@@ -66,13 +66,7 @@ class TransactionalVariable(TM, IterableUserDict):
_finalize = None _finalize = None
def _begin(self, *ignored): def _begin(self, *ignored):
"""It is required to attach this instance to somewhere in pass
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
def _finish(self, *ignored): def _finish(self, *ignored):
self.clear() self.clear()
...@@ -80,16 +74,23 @@ class TransactionalVariable(TM, IterableUserDict): ...@@ -80,16 +74,23 @@ class TransactionalVariable(TM, IterableUserDict):
def _abort(self, *ignored): def _abort(self, *ignored):
self.clear() self.clear()
def __hash__(self):
return hash(id(self))
def __setitem__(self, key, value): def __setitem__(self, key, value):
IterableUserDict.__setitem__(self, key, value) IterableUserDict.__setitem__(self, key, value)
self._register() self._register()
transactional_variable_pool = local() transactional_variable_pool = local()
def getTransactionalVariable(): def getTransactionalVariable(context):
"""Return a transactional variable.""" """Return a transactional variable."""
portal = context.portal_url.getPortalObject()
try: 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: except AttributeError:
transactional_variable_pool.instance = TransactionalVariable() transactional_variable_pool.instance = TransactionalVariable()
return transactional_variable_pool.instance return getTransactionalVariable()
...@@ -73,7 +73,7 @@ class TestTransactionalVariable(ERP5TypeTestCase, LogInterceptor): ...@@ -73,7 +73,7 @@ class TestTransactionalVariable(ERP5TypeTestCase, LogInterceptor):
ZopeTestCase._print('\n '+message) ZopeTestCase._print('\n '+message)
LOG('Testing... ', 0, message) LOG('Testing... ', 0, message)
tv = getTransactionalVariable() tv = getTransactionalVariable(self.getPortal())
self.failIfEqual(tv, None) self.failIfEqual(tv, None)
# Test frequently used dict methods. This does not cover everything, # Test frequently used dict methods. This does not cover everything,
...@@ -106,7 +106,7 @@ class TestTransactionalVariable(ERP5TypeTestCase, LogInterceptor): ...@@ -106,7 +106,7 @@ class TestTransactionalVariable(ERP5TypeTestCase, LogInterceptor):
ZopeTestCase._print('\n '+message) ZopeTestCase._print('\n '+message)
LOG('Testing... ', 0, message) LOG('Testing... ', 0, message)
tv = getTransactionalVariable() tv = getTransactionalVariable(self.getPortal())
self.failIfEqual(tv, None) self.failIfEqual(tv, None)
tv.clear() tv.clear()
...@@ -134,7 +134,7 @@ class TestTransactionalVariable(ERP5TypeTestCase, LogInterceptor): ...@@ -134,7 +134,7 @@ class TestTransactionalVariable(ERP5TypeTestCase, LogInterceptor):
ZopeTestCase._print('\n '+message) ZopeTestCase._print('\n '+message)
LOG('Testing... ', 0, message) LOG('Testing... ', 0, message)
tv = getTransactionalVariable() tv = getTransactionalVariable(self.getPortal())
self.failIfEqual(tv, None) self.failIfEqual(tv, None)
tv.clear() tv.clear()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment