Doing everything I can imagine to get a CSV dump out tomorrow.
This commit is contained in:
parent
e7c130054d
commit
4bf13b0718
13
crc/api.yml
13
crc/api.yml
|
@ -854,6 +854,19 @@ paths:
|
|||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Approval"
|
||||
/approval/csv:
|
||||
get:
|
||||
operationId: crc.api.approval.get_csv
|
||||
summary: Provides a list of all users for all approved studies
|
||||
tags:
|
||||
- Approvals
|
||||
responses:
|
||||
'200':
|
||||
description: An array of approvals
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
components:
|
||||
securitySchemes:
|
||||
jwt:
|
||||
|
|
|
@ -1,13 +1,23 @@
|
|||
import json
|
||||
import pickle
|
||||
from base64 import b64decode
|
||||
from datetime import datetime
|
||||
|
||||
from SpiffWorkflow import Workflow
|
||||
from SpiffWorkflow.serializer.dict import DictionarySerializer
|
||||
from SpiffWorkflow.serializer.json import JSONSerializer
|
||||
from flask import g
|
||||
|
||||
from crc import app, db, session
|
||||
|
||||
from crc.api.common import ApiError, ApiErrorSchema
|
||||
from crc.models.approval import Approval, ApprovalModel, ApprovalSchema
|
||||
from crc.models.approval import Approval, ApprovalModel, ApprovalSchema, ApprovalStatus
|
||||
from crc.models.ldap import LdapSchema
|
||||
from crc.models.study import Study
|
||||
from crc.models.workflow import WorkflowModel
|
||||
from crc.services.approval_service import ApprovalService
|
||||
from crc.services.ldap_service import LdapService
|
||||
from crc.services.workflow_processor import WorkflowProcessor
|
||||
|
||||
|
||||
def get_approvals(everything=False):
|
||||
|
@ -26,6 +36,57 @@ def get_approvals_for_study(study_id=None):
|
|||
return results
|
||||
|
||||
|
||||
# ----- Being decent into madness ---- #
|
||||
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()
|
||||
output = []
|
||||
errors = []
|
||||
ldapService = LdapService()
|
||||
for approval in approvals:
|
||||
try:
|
||||
if approval.status != ApprovalStatus.APPROVED.value:
|
||||
continue
|
||||
for related_approval in approval.related_approvals:
|
||||
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)
|
||||
pi_uid = workflow.study.primary_investigator_id
|
||||
pi_details = ldapService.user_info(pi_uid)
|
||||
details = []
|
||||
details.append(pi_details)
|
||||
for person in personnel:
|
||||
uid = person['PersonnelComputingID']['value']
|
||||
details.append(ldapService.user_info(uid))
|
||||
|
||||
for person in details:
|
||||
output.append({
|
||||
"pi_uid": pi_details.uid,
|
||||
"pi": pi_details.display_name,
|
||||
"name": person.display_name,
|
||||
"email": person.email_address
|
||||
})
|
||||
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 find_task(uuid, task):
|
||||
if task['id']['__uuid__'] == uuid:
|
||||
return task
|
||||
for child in task['children']:
|
||||
task = find_task(uuid, child)
|
||||
if task:
|
||||
return task
|
||||
# ----- come back to the world of the living ---- #
|
||||
|
||||
def update_approval(approval_id, body):
|
||||
if approval_id is None:
|
||||
raise ApiError('unknown_approval', 'Please provide a valid Approval ID.')
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import json
|
||||
from tests.base_test import BaseTest
|
||||
|
||||
from crc import session
|
||||
from crc import session, db
|
||||
from crc.models.approval import ApprovalModel, ApprovalSchema, ApprovalStatus
|
||||
from crc.models.protocol_builder import ProtocolBuilderStatus
|
||||
from crc.models.study import StudyModel
|
||||
|
@ -144,3 +144,11 @@ class TestApprovals(BaseTest):
|
|||
# Updated record should now have the data sent to the endpoint
|
||||
self.assertEqual(approval.message, data['message'])
|
||||
self.assertEqual(approval.status, ApprovalStatus.DECLINED.value)
|
||||
|
||||
def test_csv_export(self):
|
||||
approvals = db.session.query(ApprovalModel).all()
|
||||
for app in approvals:
|
||||
app.status = ApprovalStatus.APPROVED.value
|
||||
db.session.commit()
|
||||
rv = self.app.get(f'/v1.0/approval/csv', headers=self.logged_in_headers())
|
||||
self.assert_success(rv)
|
Loading…
Reference in New Issue