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,
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,
......
......@@ -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:
......
......@@ -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()
......@@ -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()
......
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