Commit 01328670 by Dudás Ádám

common, vm: make possibile to specify parent activity for operation

parent 407db0d3
......@@ -25,10 +25,12 @@ class Operation(object):
"""
skip_checks = kwargs.setdefault('system', False)
user = kwargs.setdefault('user', None)
parent_activity = kwargs.pop('parent_activity', None)
if not skip_checks:
self.check_auth(user)
self.check_precond()
return self.create_activity(user=user)
return self.create_activity(parent=parent_activity, user=user)
def _exec_op(self, activity, user, **kwargs):
"""Execute the operation inside the specified activity's context.
......@@ -65,11 +67,14 @@ class Operation(object):
"""Execute the operation (synchronously).
Anticipated keyword arguments:
* user: The User invoking the operation. If this argument is not
present, it'll be provided with a default value of None.
* parent_activity: Parent activity for the operation. If this argument
is present, the operation's activity will be created
as a child activity of it.
* system: Indicates that the operation is invoked by the system, not a
User. If this argument is present and has a value of True,
then authorization checks are skipped.
* user: The User invoking the operation. If this argument is not
present, it'll be provided with a default value of None.
"""
activity = self.__prelude(kwargs)
return self._exec_op(activity=activity, **kwargs)
......@@ -82,7 +87,7 @@ class Operation(object):
raise PermissionDenied("%s doesn't have the required permissions."
% user)
def create_activity(self, user):
def create_activity(self, parent, user):
raise NotImplementedError
def on_abort(self, activity, error):
......
......@@ -39,9 +39,22 @@ class InstanceOperation(Operation):
super(InstanceOperation, self).check_auth(user=user)
def create_activity(self, user):
return InstanceActivity.create(code_suffix=self.activity_code_suffix,
instance=self.instance, user=user)
def create_activity(self, parent, user):
if parent:
if parent.instance != self.instance:
raise ValueError("The instance associated with the specified "
"parent activity does not match the instance "
"bound to the operation.")
if parent.user != user:
raise ValueError("The user associated with the specified "
"parent activity does not match the user "
"provided as parameter.")
return parent.create_sub(code_suffix=self.activity_code_suffix)
else:
return InstanceActivity.create(
code_suffix=self.activity_code_suffix, instance=self.instance,
user=user)
def register_instance_operation(op_cls, op_id=None):
......@@ -420,9 +433,21 @@ class NodeOperation(Operation):
super(NodeOperation, self).__init__(subject=node)
self.node = node
def create_activity(self, user):
return NodeActivity.create(code_suffix=self.activity_code_suffix,
node=self.node, user=user)
def create_activity(self, parent, user):
if parent:
if parent.node != self.node:
raise ValueError("The node associated with the specified "
"parent activity does not match the node "
"bound to the operation.")
if parent.user != user:
raise ValueError("The user associated with the specified "
"parent activity does not match the user "
"provided as parameter.")
return parent.create_sub(code_suffix=self.activity_code_suffix)
else:
return NodeActivity.create(code_suffix=self.activity_code_suffix,
node=self.node, user=user)
def register_node_operation(op_cls, op_id=None):
......
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 sign in to comment