cr-connect-workflow/crc/api/approval.py

46 lines
1.5 KiB
Python
Raw Normal View History

from datetime import datetime
2020-06-02 01:45:09 +00:00
from flask import g
from crc import app, db, session
2020-05-24 05:53:48 +00:00
from crc.api.common import ApiError, ApiErrorSchema
from crc.models.approval import Approval, ApprovalModel, ApprovalSchema
from crc.services.approval_service import ApprovalService
2020-06-02 01:45:09 +00:00
def get_approvals(everything=False):
if everything:
approvals = ApprovalService.get_all_approvals()
2020-05-26 16:21:36 +00:00
else:
approvals = ApprovalService.get_approvals_per_user(g.user.uid)
2020-05-24 05:53:48 +00:00
results = ApprovalSchema(many=True).dump(approvals)
return results
def get_approvals_for_study(study_id=None):
db_approvals = ApprovalService.get_approvals_for_study(study_id)
approvals = [Approval.from_model(approval_model) for approval_model in db_approvals]
results = ApprovalSchema(many=True).dump(approvals)
return results
2020-05-24 05:53:48 +00:00
def update_approval(approval_id, body):
if approval_id is None:
raise ApiError('unknown_approval', 'Please provide a valid Approval ID.')
approval_model = session.query(ApprovalModel).get(approval_id)
if approval_model is None:
2020-05-24 05:53:48 +00:00
raise ApiError('unknown_approval', 'The approval "' + str(approval_id) + '" is not recognized.')
approval: Approval = ApprovalSchema().load(body)
if approval_model.approver_uid != g.user.uid:
raise ApiError("not_your_approval", "You may not modify this approval. It belongs to another user.")
approval.update_model(approval_model)
approval_model.date_approved = datetime.now()
session.commit()
2020-05-24 05:53:48 +00:00
result = ApprovalSchema().dump(approval)
return result