2020-05-25 21:40:24 +00:00
|
|
|
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-05-22 22:25:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_approvals(approver_uid = None):
|
2020-05-26 16:21:36 +00:00
|
|
|
if not approver_uid:
|
|
|
|
db_approvals = ApprovalService.get_all_approvals()
|
|
|
|
else:
|
|
|
|
db_approvals = ApprovalService.get_approvals_per_user(approver_uid)
|
2020-05-24 05:53:48 +00:00
|
|
|
approvals = [Approval.from_model(approval_model) for approval_model in db_approvals]
|
2020-05-31 23:24:23 +00:00
|
|
|
|
2020-05-24 05:53:48 +00:00
|
|
|
results = ApprovalSchema(many=True).dump(approvals)
|
|
|
|
return results
|
2020-05-22 22:25:00 +00:00
|
|
|
|
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.')
|
|
|
|
|
2020-05-25 21:40:24 +00:00
|
|
|
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.')
|
|
|
|
|
2020-05-25 21:40:24 +00:00
|
|
|
approval: Approval = ApprovalSchema().load(body)
|
|
|
|
approval.update_model(approval_model)
|
|
|
|
session.commit()
|
|
|
|
|
2020-05-24 05:53:48 +00:00
|
|
|
result = ApprovalSchema().dump(approval)
|
|
|
|
return result
|