From 299ad4fc8bdd06114fbff59890da0744e5bd899d Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Wed, 3 Jun 2020 07:58:48 -0400 Subject: [PATCH] Adding more details to the csv output, and assuring we don't miss people with outstanding approvals that were cancelled. --- crc/api/approval.py | 22 ++++++++++++++-------- crc/services/approval_service.py | 13 +++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/crc/api/approval.py b/crc/api/approval.py index f719ba6d..6ab887f4 100644 --- a/crc/api/approval.py +++ b/crc/api/approval.py @@ -40,7 +40,7 @@ def get_approvals_for_study(study_id=None): def get_csv(): """A huge bit of a one-off for RRT, but 3 weeks of midnight work can convince a man to do just about anything""" - approvals = ApprovalService.get_all_approvals() + approvals = ApprovalService.get_all_approvals(ignore_cancelled=True) output = [] errors = [] ldapService = LdapService() @@ -52,7 +52,11 @@ def get_csv(): if related_approval.status != ApprovalStatus.APPROVED.value: continue workflow = db.session.query(WorkflowModel).filter(WorkflowModel.id == approval.workflow_id).first() - personnel = extract_personnel(workflow.bpmn_workflow_json) + data = json.loads(workflow.bpmn_workflow_json) + last_task = find_task(data['last_task']['__uuid__'], data['task_tree']) + personnel = extract_value(last_task, 'personnel') + training_val = extract_value(last_task, 'RequiredTraining') + review_complete = 'AllRequiredTraining' in training_val pi_uid = workflow.study.primary_investigator_id pi_details = ldapService.user_info(pi_uid) details = [] @@ -63,20 +67,22 @@ def get_csv(): for person in details: output.append({ + "study_id": approval.study_id, "pi_uid": pi_details.uid, "pi": pi_details.display_name, "name": person.display_name, - "email": person.email_address + "email": person.email_address, + "review_complete": review_complete, }) except Exception as e: errors.append("Error pulling data for workflow #%i: %s" % (approval.workflow_id, str(e))) return {"results": output, "errors": errors } -def extract_personnel(data): - data = json.loads(data) - last_task = find_task(data['last_task']['__uuid__'], data['task_tree']) - last_task_personnel = pickle.loads(b64decode(last_task['data']['personnel']['__bytes__'])) - return last_task_personnel +def extract_value(task, key): + if key in task['data']: + return pickle.loads(b64decode(task['data'][key]['__bytes__'])) + else: + return "" def find_task(uuid, task): if task['id']['__uuid__'] == uuid: diff --git a/crc/services/approval_service.py b/crc/services/approval_service.py index 63cdaff4..bba53a61 100644 --- a/crc/services/approval_service.py +++ b/crc/services/approval_service.py @@ -15,13 +15,18 @@ class ApprovalService(object): """Provides common tools for working with an Approval""" @staticmethod - def __one_approval_from_study(study, approver_uid = None): + def __one_approval_from_study(study, approver_uid = None, ignore_cancelled=False): """Returns one approval, with all additional approvals as 'related_approvals', the main approval can be pinned to an approver with an optional argument. Will return null if no approvals exist on the study.""" main_approval = None related_approvals = [] - approvals = db.session.query(ApprovalModel).filter(ApprovalModel.study_id == study.id).all() + query = db.session.query(ApprovalModel).\ + filter(ApprovalModel.study_id == study.id) + if ignore_cancelled: + query=query.filter(ApprovalModel.status != ApprovalStatus.CANCELED.value) + + approvals = query.all() for approval_model in approvals: if approval_model.approver_uid == approver_uid: main_approval = Approval.from_model(approval_model) @@ -48,13 +53,13 @@ class ApprovalService(object): return approvals @staticmethod - def get_all_approvals(): + def get_all_approvals(ignore_cancelled=False): """Returns a list of all approval objects (not db models), one record per study, with any associated approvals grouped under the first approval.""" studies = db.session.query(StudyModel).all() approvals = [] for study in studies: - approval = ApprovalService.__one_approval_from_study(study) + approval = ApprovalService.__one_approval_from_study(study, ignore_cancelled=ignore_cancelled) if approval: approvals.append(approval) return approvals