2020-05-24 20:13:15 +00:00
|
|
|
from crc.api.common import ApiError
|
|
|
|
from crc.scripts.script import Script
|
|
|
|
from crc.services.approval_service import ApprovalService
|
|
|
|
|
|
|
|
|
|
|
|
class RequestApproval(Script):
|
|
|
|
"""This still needs to be fully wired up as a Script task callable from the workflow
|
|
|
|
But the basic logic is here just to get the tests passing and logic sound. """
|
|
|
|
|
|
|
|
def get_description(self):
|
|
|
|
return """
|
|
|
|
Creates an approval request on this workflow, by the given approver_uid(s),"
|
2020-05-27 00:06:50 +00:00
|
|
|
Takes multiple arguments, which should point to data located in current task
|
2020-07-20 03:53:18 +00:00
|
|
|
or be quoted strings. The order is important. Approvals will be processed
|
2020-06-05 18:33:00 +00:00
|
|
|
in this order.
|
2020-05-24 20:13:15 +00:00
|
|
|
|
|
|
|
Example:
|
2020-05-27 00:06:50 +00:00
|
|
|
RequestApproval approver1 "dhf8r"
|
2020-05-24 20:13:15 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
|
|
|
|
self.get_uids(task, args)
|
|
|
|
|
|
|
|
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
|
|
|
|
uids = self.get_uids(task, args)
|
|
|
|
if isinstance(uids, str):
|
|
|
|
ApprovalService.add_approval(study_id, workflow_id, args)
|
|
|
|
elif isinstance(uids, list):
|
|
|
|
for id in uids:
|
2020-06-02 12:35:19 +00:00
|
|
|
if id: ## Assure it's not empty or null
|
|
|
|
ApprovalService.add_approval(study_id, workflow_id, id)
|
2020-05-24 20:13:15 +00:00
|
|
|
|
|
|
|
def get_uids(self, task, args):
|
2020-05-27 00:06:50 +00:00
|
|
|
if len(args) < 1:
|
2020-05-24 20:13:15 +00:00
|
|
|
raise ApiError(code="missing_argument",
|
2020-05-27 00:06:50 +00:00
|
|
|
message="The RequestApproval script requires at least one argument. The "
|
2020-05-24 20:13:15 +00:00
|
|
|
"the name of the variable in the task data that contains user"
|
2020-05-27 00:06:50 +00:00
|
|
|
"id to process. Multiple arguments are accepted.")
|
|
|
|
uids = []
|
|
|
|
for arg in args:
|
|
|
|
id = task.workflow.script_engine.evaluate_expression(task, arg)
|
|
|
|
uids.append(id)
|
|
|
|
if not isinstance(id, str):
|
|
|
|
raise ApiError(code="invalid_argument",
|
|
|
|
message="The RequestApproval script requires 1 argument. The "
|
2020-05-24 20:13:15 +00:00
|
|
|
"the name of the variable in the task data that contains user"
|
|
|
|
"ids to process. This must point to an array or a string, but "
|
|
|
|
"it currently points to a %s " % uids.__class__.__name__)
|
|
|
|
|
|
|
|
return uids
|
|
|
|
|