Adding events to the study that is returned via the api.

This commit is contained in:
Dan Funk 2020-07-27 16:32:23 -04:00
parent 842d2ee100
commit 8d42d520a0
3 changed files with 33 additions and 7 deletions

View File

@ -41,8 +41,10 @@ class StudyModel(db.Model):
class WorkflowMetadata(object):
def __init__(self, id, name, display_name, description, spec_version, category_id, category_display_name, state: WorkflowState, status: WorkflowStatus,
total_tasks, completed_tasks, display_order):
def __init__(self, id, name = None, display_name = None, description = None, spec_version = None,
category_id = None, category_display_name = None, state: WorkflowState = None,
status: WorkflowStatus = None, total_tasks = None, completed_tasks = None,
display_order = None):
self.id = id
self.name = name
self.display_name = display_name
@ -176,6 +178,7 @@ class StudySchema(ma.Schema):
files = fields.List(fields.Nested(FileSchema), dump_only=True)
approvals = fields.List(fields.Nested('ApprovalSchema'), dump_only=True)
enrollment_date = fields.Date(allow_none=True)
events = fields.List(fields.Nested('TaskEventSchema'), dump_only=True)
class Meta:
model = Study

View File

@ -1,6 +1,5 @@
from copy import copy
from datetime import datetime
import json
from typing import List
import requests
@ -13,16 +12,15 @@ from crc.api.common import ApiError
from crc.models.file import FileModel, FileModelSchema, File
from crc.models.ldap import LdapSchema
from crc.models.protocol_builder import ProtocolBuilderStudy, ProtocolBuilderStatus
from crc.models.task_event import TaskEventModel
from crc.models.study import StudyModel, Study, Category, WorkflowMetadata
from crc.models.task_event import TaskEventModel, TaskEvent
from crc.models.workflow import WorkflowSpecCategoryModel, WorkflowModel, WorkflowSpecModel, WorkflowState, \
WorkflowStatus
from crc.services.approval_service import ApprovalService
from crc.services.file_service import FileService
from crc.services.ldap_service import LdapService
from crc.services.protocol_builder import ProtocolBuilderService
from crc.services.workflow_processor import WorkflowProcessor
from crc.services.approval_service import ApprovalService
from crc.models.approval import Approval
class StudyService(object):
@ -63,7 +61,7 @@ class StudyService(object):
files = (File.from_models(model, FileService.get_file_data(model.id),
FileService.get_doc_dictionary()) for model in files)
study.files = list(files)
study.events = StudyService.get_events(study_id)
# Calling this line repeatedly is very very slow. It creates the
# master spec and runs it. Don't execute this for Abandoned studies, as
# we don't have the information to process them.
@ -77,6 +75,14 @@ class StudyService(object):
return study
@staticmethod
def get_events(study_id):
event_models = db.session.query(TaskEventModel).filter(TaskEventModel.study_id == study_id).all()
events = []
for event_model in event_models:
events.append(TaskEvent(event_model, None, WorkflowMetadata(id=event_model.workflow_id)))
return events
@staticmethod
def delete_study(study_id):
session.query(TaskEventModel).filter_by(study_id=study_id).delete()

View File

@ -1,4 +1,5 @@
import json
from tests.base_test import BaseTest
from datetime import datetime, timezone
@ -13,6 +14,7 @@ from crc.models.study import StudyModel, StudySchema
from crc.models.workflow import WorkflowSpecModel, WorkflowModel
from crc.services.file_service import FileService
from crc.services.workflow_processor import WorkflowProcessor
from crc.services.workflow_service import WorkflowService
class TestStudyApi(BaseTest):
@ -112,6 +114,21 @@ class TestStudyApi(BaseTest):
for approval in study.approvals:
self.assertEqual(full_study['study'].title, approval['title'])
def test_get_study_has_details_about_events(self):
# Set up the study and attach a file to it.
self.load_example_data()
workflow = self.create_workflow('file_upload_form')
processor = WorkflowProcessor(workflow)
task = processor.next_task()
WorkflowService.log_task_action('dhf8r', processor, task, 'my_action')
api_response = self.app.get('/v1.0/study/%i' % workflow.study_id,
headers=self.logged_in_headers(),
content_type="application/json")
self.assert_success(api_response)
study = json.loads(api_response.get_data(as_text=True))
self.assertEqual(1, len(study['events']))
self.assertEqual('my_action', study['events'][0]['action'])
def test_add_study(self):
self.load_example_data()
study = self.add_test_study()