From 54640988a7a6e8677c092144a9ab1dfc30592d9c Mon Sep 17 00:00:00 2001 From: Carlos Lopez Date: Wed, 27 May 2020 12:06:32 -0600 Subject: [PATCH] Update endpoint fixes --- crc/api/approval.py | 1 - crc/models/approval.py | 19 +++++++++++-------- tests/test_approvals_api.py | 13 +++++++++++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/crc/api/approval.py b/crc/api/approval.py index c51634e4..739773c1 100644 --- a/crc/api/approval.py +++ b/crc/api/approval.py @@ -24,7 +24,6 @@ def update_approval(approval_id, body): approval: Approval = ApprovalSchema().load(body) approval.update_model(approval_model) - session.add(approval_model) session.commit() result = ApprovalSchema().dump(approval) diff --git a/crc/models/approval.py b/crc/models/approval.py index 90fa14e2..2038fcd5 100644 --- a/crc/models/approval.py +++ b/crc/models/approval.py @@ -40,7 +40,7 @@ class ApprovalModel(db.Model): workflow = db.relationship(WorkflowModel) approver_uid = db.Column(db.String) # Not linked to user model, as they may not have logged in yet. status = db.Column(db.String) - message = db.Column(db.String) + message = db.Column(db.String, default='') date_created = db.Column(db.DateTime(timezone=True), default=func.now()) version = db.Column(db.Integer) # Incremented integer, so 1,2,3 as requests are made. workflow_hash = db.Column(db.String) # A hash of the workflow at the moment the approval was created. @@ -71,14 +71,18 @@ class Approval(object): if model.study: instance.title = model.study.title + instance.approver = {} try: ldap_service = LdapService() - user_info = ldap_service.user_info(model.approver_uid) + principal_investigator_id = model.study.primary_investigator_id + user_info = ldap_service.user_info(principal_investigator_id) except (ApiError, LDAPSocketOpenError) as exception: user_info = None + instance.approver['display_name'] = 'Primary Investigator details' + instance.approver['department'] = 'currently not available' if user_info: - instance.approver = {} + # TODO: Rename approver to primary investigator instance.approver['uid'] = model.approver_uid instance.approver['display_name'] = user_info.display_name instance.approver['title'] = user_info.title @@ -94,17 +98,16 @@ class Approval(object): return instance - def update_model(self, study_model: StudyModel): - for k,v in self.__dict__.items(): - if not k.startswith('_'): - study_model.__dict__[k] = v + def update_model(self, approval_model: ApprovalModel): + approval_model.status = self.status + approval_model.message = self.message class ApprovalSchema(ma.Schema): class Meta: model = Approval fields = ["id", "study_id", "workflow_id", "version", "title", - "version", "status", "approver", "associated_files"] + "version", "status", "message", "approver", "associated_files"] unknown = INCLUDE @marshmallow.post_load diff --git a/tests/test_approvals_api.py b/tests/test_approvals_api.py index 8cc14d14..393831e7 100644 --- a/tests/test_approvals_api.py +++ b/tests/test_approvals_api.py @@ -16,6 +16,7 @@ APPROVAL_PAYLOAD = { 'title': 'El Study', 'status': 'DECLINED', 'version': 1, + 'message': 'Incorrect documents', 'associated_files': [ { 'id': 42, @@ -96,9 +97,17 @@ class TestApprovals(BaseTest): approval_id = self.approval.id data = dict(APPROVAL_PAYLOAD) data['id'] = approval_id - data = json.dumps(data) + + self.assertEqual(self.approval.status, ApprovalStatus.WAITING.value) + rv = self.app.put(f'/v1.0/approval/{approval_id}', content_type="application/json", headers=self.logged_in_headers(), - data=data) + data=json.dumps(data)) self.assert_success(rv) + + session.refresh(self.approval) + + # Updated record should now have the data sent to the endpoint + self.assertEqual(self.approval.message, data['message']) + self.assertEqual(self.approval.status, ApprovalStatus.DECLINED.value)