Adding more details to the csv output, and assuring we don't miss people with outstanding approvals that were cancelled.

This commit is contained in:
Dan Funk 2020-06-03 07:58:48 -04:00
parent 4bf13b0718
commit 299ad4fc8b
2 changed files with 23 additions and 12 deletions

View File

@ -40,7 +40,7 @@ def get_approvals_for_study(study_id=None):
def get_csv(): def get_csv():
"""A huge bit of a one-off for RRT, but 3 weeks of midnight work can convince a """A huge bit of a one-off for RRT, but 3 weeks of midnight work can convince a
man to do just about anything""" man to do just about anything"""
approvals = ApprovalService.get_all_approvals() approvals = ApprovalService.get_all_approvals(ignore_cancelled=True)
output = [] output = []
errors = [] errors = []
ldapService = LdapService() ldapService = LdapService()
@ -52,7 +52,11 @@ def get_csv():
if related_approval.status != ApprovalStatus.APPROVED.value: if related_approval.status != ApprovalStatus.APPROVED.value:
continue continue
workflow = db.session.query(WorkflowModel).filter(WorkflowModel.id == approval.workflow_id).first() 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_uid = workflow.study.primary_investigator_id
pi_details = ldapService.user_info(pi_uid) pi_details = ldapService.user_info(pi_uid)
details = [] details = []
@ -63,20 +67,22 @@ def get_csv():
for person in details: for person in details:
output.append({ output.append({
"study_id": approval.study_id,
"pi_uid": pi_details.uid, "pi_uid": pi_details.uid,
"pi": pi_details.display_name, "pi": pi_details.display_name,
"name": person.display_name, "name": person.display_name,
"email": person.email_address "email": person.email_address,
"review_complete": review_complete,
}) })
except Exception as e: except Exception as e:
errors.append("Error pulling data for workflow #%i: %s" % (approval.workflow_id, str(e))) errors.append("Error pulling data for workflow #%i: %s" % (approval.workflow_id, str(e)))
return {"results": output, "errors": errors } return {"results": output, "errors": errors }
def extract_personnel(data): def extract_value(task, key):
data = json.loads(data) if key in task['data']:
last_task = find_task(data['last_task']['__uuid__'], data['task_tree']) return pickle.loads(b64decode(task['data'][key]['__bytes__']))
last_task_personnel = pickle.loads(b64decode(last_task['data']['personnel']['__bytes__'])) else:
return last_task_personnel return ""
def find_task(uuid, task): def find_task(uuid, task):
if task['id']['__uuid__'] == uuid: if task['id']['__uuid__'] == uuid:

View File

@ -15,13 +15,18 @@ class ApprovalService(object):
"""Provides common tools for working with an Approval""" """Provides common tools for working with an Approval"""
@staticmethod @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', """Returns one approval, with all additional approvals as 'related_approvals',
the main approval can be pinned to an approver with an optional argument. the main approval can be pinned to an approver with an optional argument.
Will return null if no approvals exist on the study.""" Will return null if no approvals exist on the study."""
main_approval = None main_approval = None
related_approvals = [] 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: for approval_model in approvals:
if approval_model.approver_uid == approver_uid: if approval_model.approver_uid == approver_uid:
main_approval = Approval.from_model(approval_model) main_approval = Approval.from_model(approval_model)
@ -48,13 +53,13 @@ class ApprovalService(object):
return approvals return approvals
@staticmethod @staticmethod
def get_all_approvals(): def get_all_approvals(ignore_cancelled=False):
"""Returns a list of all approval objects (not db models), one record """Returns a list of all approval objects (not db models), one record
per study, with any associated approvals grouped under the first approval.""" per study, with any associated approvals grouped under the first approval."""
studies = db.session.query(StudyModel).all() studies = db.session.query(StudyModel).all()
approvals = [] approvals = []
for study in studies: for study in studies:
approval = ApprovalService.__one_approval_from_study(study) approval = ApprovalService.__one_approval_from_study(study, ignore_cancelled=ignore_cancelled)
if approval: if approval:
approvals.append(approval) approvals.append(approval)
return approvals return approvals