Enable CSV download

This commit is contained in:
Carlos Lopez 2020-06-22 09:24:58 -06:00
parent b8d60ca944
commit e5541e4950
3 changed files with 60 additions and 21 deletions

View File

@ -917,6 +917,28 @@ paths:
application/json: application/json:
schema: schema:
type: object 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: components:
securitySchemes: securitySchemes:
jwt: jwt:

View File

@ -1,9 +1,11 @@
import csv
import io
import json import json
import pickle import pickle
from base64 import b64decode from base64 import b64decode
from datetime import datetime from datetime import datetime
from flask import g from flask import g, make_response
from crc import db, session from crc import db, session
from crc.api.common import ApiError from crc.api.common import ApiError
@ -88,6 +90,17 @@ def get_approvals_for_study(study_id=None):
return results 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 ---- # # ----- Begin descent into madness ---- #
def get_csv(): 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 """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

View File

@ -110,24 +110,27 @@ class ApprovalService(object):
return [Approval.from_model(approval_model) for approval_model in db_approvals] return [Approval.from_model(approval_model) for approval_model in db_approvals]
@staticmethod @staticmethod
def get_health_attesting_for_today(): def get_health_attesting_records(all_approvals=True):
"""Return a CSV with prepared information related to approvals """Return a list with prepared information related to all approvals
created today""" approved or filtered by today """
# import pdb; pdb.set_trace() if all_approvals:
today = datetime.now() - timedelta(days=3)
today = today.date()
approvals = session.query(ApprovalModel).filter( approvals = session.query(ApprovalModel).filter(
# func.date(ApprovalModel.date_created)==today, 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 ApprovalModel.status==ApprovalStatus.APPROVED.value
) )
health_attesting_rows = [ health_attesting_rows = [
'university_computing_id', ['university_computing_id',
'last_name', 'last_name',
'first_name', 'first_name',
'department', 'department',
'job_title', 'job_title',
'supervisor_university_computing_id' 'supervisor_university_computing_id']
] ]
for approval in approvals: for approval in approvals:
pi_info = LdapService.user_info(approval.study.primary_investigator_id) pi_info = LdapService.user_info(approval.study.primary_investigator_id)
@ -147,13 +150,14 @@ class ApprovalService(object):
@staticmethod @staticmethod
def update_approval(approval_id, approver_uid): 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) db_approval = session.query(ApprovalModel).get(approval_id)
status = db_approval.status status = db_approval.status
if db_approval: if db_approval:
# db_approval.status = status
# session.add(db_approval)
# session.commit()
if status == ApprovalStatus.APPROVED.value: if status == ApprovalStatus.APPROVED.value:
# second_approval = ApprovalModel().query.filter_by( # second_approval = ApprovalModel().query.filter_by(
# study_id=db_approval.study_id, workflow_id=db_approval.workflow_id, # study_id=db_approval.study_id, workflow_id=db_approval.workflow_id,