Commit 85ecbf53 authored by Julien Muchembled's avatar Julien Muchembled

Helper for migration scripts: new decorator to disable worklows temporarily

'simple_decorator' is changed so that decorators can be used on workflow
methods.
parent 7be5370d
...@@ -61,7 +61,7 @@ from Products.ERP5Type import interfaces ...@@ -61,7 +61,7 @@ from Products.ERP5Type import interfaces
from Products.ERP5Type import Permissions from Products.ERP5Type import Permissions
from Products.ERP5Type.Utils import UpperCase from Products.ERP5Type.Utils import UpperCase
from Products.ERP5Type.Utils import convertToUpperCase, convertToMixedCase from Products.ERP5Type.Utils import convertToUpperCase, convertToMixedCase
from Products.ERP5Type.Utils import createExpressionContext from Products.ERP5Type.Utils import createExpressionContext, simple_decorator
from Products.ERP5Type.Accessor.Accessor import Accessor from Products.ERP5Type.Accessor.Accessor import Accessor
from Products.ERP5Type.Accessor.Constant import PropertyGetter as ConstantGetter from Products.ERP5Type.Accessor.Constant import PropertyGetter as ConstantGetter
from Products.ERP5Type.Accessor.TypeDefinition import list_types from Products.ERP5Type.Accessor.TypeDefinition import list_types
...@@ -256,6 +256,34 @@ class WorkflowMethod(Method): ...@@ -256,6 +256,34 @@ class WorkflowMethod(Method):
# Return result finally # Return result finally
return result return result
_do_interaction = __call__
_no_interaction_lock = threading.Lock()
_no_interaction_log = None
def _no_interaction(self, *args, **kw):
log = "skip interactions for %r" % args[0]
if WorkflowMethod._no_interaction_log != log:
WorkflowMethod._no_interaction_log = log
LOG("WorkflowMethod", INFO, log)
return self.__dict__['_m'](*args, **kw)
@staticmethod
@simple_decorator
def disable(func):
def wrapper(*args, **kw):
WorkflowMethod._no_interaction_lock.acquire()
try:
WorkflowMethod.__call__ = WorkflowMethod.__dict__['_no_interaction']
return func(*args, **kw)
finally:
WorkflowMethod.__call__ = WorkflowMethod.__dict__['_do_interaction']
WorkflowMethod._no_interaction_lock.release()
return wrapper
@staticmethod
def disabled():
return WorkflowMethod._no_interaction_lock.locked()
def registerTransitionAlways(self, portal_type, workflow_id, transition_id): def registerTransitionAlways(self, portal_type, workflow_id, transition_id):
""" """
Transitions registered as always will be invoked always Transitions registered as always will be invoked always
...@@ -328,6 +356,7 @@ class PropertyHolder(object): ...@@ -328,6 +356,7 @@ class PropertyHolder(object):
workflow_method = getattr(self, id, None) workflow_method = getattr(self, id, None)
if workflow_method is None: if workflow_method is None:
# XXX: We should pass 'tr_id' as second parameter.
workflow_method = WorkflowMethod(Base._doNothing) workflow_method = WorkflowMethod(Base._doNothing)
setattr(self, id, workflow_method) setattr(self, id, workflow_method)
if once_per_transaction: if once_per_transaction:
......
...@@ -72,11 +72,16 @@ def simple_decorator(decorator): ...@@ -72,11 +72,16 @@ def simple_decorator(decorator):
See also http://wiki.python.org/moin/PythonDecoratorLibrary See also http://wiki.python.org/moin/PythonDecoratorLibrary
XXX We should use http://pypi.python.org/pypi/decorator/ instead, XXX We should use http://pypi.python.org/pypi/decorator/ instead,
to make decorators ZPublisher-friendly. to make decorators ZPublisher-friendly (but it is probably to slow).
""" """
def new_decorator(f): def new_decorator(f):
g = decorator(f) g = decorator(f)
try:
g.__name__ = f.__name__ g.__name__ = f.__name__
except AttributeError:
# XXX: Should be "convertToMixedCase(f._transition_id)"
g.__name__ = f._m.__name__ # WorkflowMethod
else:
g.__doc__ = f.__doc__ g.__doc__ = f.__doc__
g.__dict__.update(f.__dict__) g.__dict__.update(f.__dict__)
g._original = f # for tab_completion navigation in IPython g._original = f # for tab_completion navigation in IPython
......
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