From e5541e4950416ebb9746a893c5386b3219f5826b Mon Sep 17 00:00:00 2001 From: Carlos Lopez Date: Mon, 22 Jun 2020 09:24:58 -0600 Subject: [PATCH] Enable CSV download --- crc/api.yml | 22 ++++++++++++++++ crc/api/approval.py | 15 ++++++++++- crc/services/approval_service.py | 44 +++++++++++++++++--------------- 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/crc/api.yml b/crc/api.yml index 64f6086a..71710881 100644 --- a/crc/api.yml +++ b/crc/api.yml @@ -917,6 +917,28 @@ paths: application/json: schema: type: object + /health_attesting: + parameters: + - name: all_approvals + in: query + required: false + description: If set to false, returns just approvals for today. + schema: + type: string + get: + operationId: crc.api.approval.get_health_attesting_csv + summary: Returns a CSV file with health attesting records + tags: + - Approvals + responses: + '200': + description: A CSV file + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/Approval" components: securitySchemes: jwt: diff --git a/crc/api/approval.py b/crc/api/approval.py index b3ee0fed..a44dfc5b 100644 --- a/crc/api/approval.py +++ b/crc/api/approval.py @@ -1,9 +1,11 @@ +import csv +import io import json import pickle from base64 import b64decode from datetime import datetime -from flask import g +from flask import g, make_response from crc import db, session from crc.api.common import ApiError @@ -88,6 +90,17 @@ def get_approvals_for_study(study_id=None): return results +def get_health_attesting_csv(all_approvals=True): + records = ApprovalService.get_health_attesting_records(all_approvals) + si = io.StringIO() + cw = csv.writer(si) + cw.writerows(records) + output = make_response(si.getvalue()) + output.headers["Content-Disposition"] = "attachment; filename=health_attesting.csv" + output.headers["Content-type"] = "text/csv" + return output + + # ----- Begin descent into madness ---- # def get_csv(): """A damn lie, it's a json file. A huge bit of a one-off for RRT, but 3 weeks of midnight work can convince a diff --git a/crc/services/approval_service.py b/crc/services/approval_service.py index 81608a34..f98733a5 100644 --- a/crc/services/approval_service.py +++ b/crc/services/approval_service.py @@ -110,24 +110,27 @@ class ApprovalService(object): return [Approval.from_model(approval_model) for approval_model in db_approvals] @staticmethod - def get_health_attesting_for_today(): - """Return a CSV with prepared information related to approvals - created today""" - # import pdb; pdb.set_trace() - today = datetime.now() - timedelta(days=3) - today = today.date() - approvals = session.query(ApprovalModel).filter( - # func.date(ApprovalModel.date_created)==today, - ApprovalModel.status==ApprovalStatus.APPROVED.value - ) + def get_health_attesting_records(all_approvals=True): + """Return a list with prepared information related to all approvals + approved or filtered by today """ + if all_approvals: + approvals = session.query(ApprovalModel).filter( + ApprovalModel.status==ApprovalStatus.APPROVED.value + ) + else: + today = datetime.now().date() + approvals = session.query(ApprovalModel).filter( + func.date(ApprovalModel.date_created)==today, + ApprovalModel.status==ApprovalStatus.APPROVED.value + ) health_attesting_rows = [ - 'university_computing_id', - 'last_name', - 'first_name', - 'department', - 'job_title', - 'supervisor_university_computing_id' + ['university_computing_id', + 'last_name', + 'first_name', + 'department', + 'job_title', + 'supervisor_university_computing_id'] ] for approval in approvals: pi_info = LdapService.user_info(approval.study.primary_investigator_id) @@ -147,13 +150,14 @@ class ApprovalService(object): @staticmethod def update_approval(approval_id, approver_uid): - """Update a specific approval""" + """Update a specific approval + NOTE: Actual update happens in the API layer, this + funtion is currently in charge of only sending + corresponding emails + """ db_approval = session.query(ApprovalModel).get(approval_id) status = db_approval.status if db_approval: - # db_approval.status = status - # session.add(db_approval) - # session.commit() if status == ApprovalStatus.APPROVED.value: # second_approval = ApprovalModel().query.filter_by( # study_id=db_approval.study_id, workflow_id=db_approval.workflow_id,