This adds additional file data details to the study model as well.
This commit is contained in:
parent
dd6c1d2b42
commit
9c7de39b09
|
@ -12,8 +12,9 @@ from crc.services.file_service import FileService
|
|||
|
||||
|
||||
def to_file_api(file_model):
|
||||
"""Converts a FileModel object to something we can return via the aip"""
|
||||
return File.from_models(file_model, FileService.get_file_data(file_model.id))
|
||||
"""Converts a FileModel object to something we can return via the api"""
|
||||
return File.from_models(file_model, FileService.get_file_data(file_model.id),
|
||||
FileService.get_doc_dictionary())
|
||||
|
||||
|
||||
def get_files(workflow_spec_id=None, workflow_id=None, form_field_key=None):
|
||||
|
|
|
@ -86,7 +86,7 @@ class FileModel(db.Model):
|
|||
|
||||
class File(object):
|
||||
@classmethod
|
||||
def from_models(cls, model: FileModel, data_model: FileDataModel):
|
||||
def from_models(cls, model: FileModel, data_model: FileDataModel, doc_dictionary):
|
||||
instance = cls()
|
||||
instance.id = model.id
|
||||
instance.name = model.name
|
||||
|
@ -99,6 +99,15 @@ class File(object):
|
|||
instance.workflow_id = model.workflow_id
|
||||
instance.irb_doc_code = model.irb_doc_code
|
||||
instance.type = model.type
|
||||
if model.irb_doc_code and model.irb_doc_code in doc_dictionary:
|
||||
instance.category = "/".join(filter(None, [doc_dictionary[model.irb_doc_code]['category1'],
|
||||
doc_dictionary[model.irb_doc_code]['category2'],
|
||||
doc_dictionary[model.irb_doc_code]['category3']]))
|
||||
instance.description = doc_dictionary[model.irb_doc_code]['description']
|
||||
instance.download_name = ".".join([instance.category, model.type.value])
|
||||
else:
|
||||
instance.category = ""
|
||||
instance.description = ""
|
||||
if data_model:
|
||||
instance.last_modified = data_model.date_created
|
||||
instance.latest_version = data_model.version
|
||||
|
@ -122,7 +131,8 @@ class FileSchema(ma.Schema):
|
|||
model = File
|
||||
fields = ["id", "name", "is_status", "is_reference", "content_type",
|
||||
"primary", "primary_process_id", "workflow_spec_id", "workflow_id",
|
||||
"irb_doc_code", "last_modified", "latest_version", "type"]
|
||||
"irb_doc_code", "last_modified", "latest_version", "type", "categories",
|
||||
"description", "category", "description", "download_name"]
|
||||
unknown = INCLUDE
|
||||
type = EnumField(FileType)
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ from sqlalchemy import func
|
|||
|
||||
from crc import db, ma
|
||||
from crc.api.common import ApiErrorSchema
|
||||
from crc.models.file import FileModel, SimpleFileSchema
|
||||
from crc.models.file import FileModel, SimpleFileSchema, FileSchema
|
||||
from crc.models.protocol_builder import ProtocolBuilderStatus, ProtocolBuilderStudy
|
||||
from crc.models.workflow import WorkflowSpecCategoryModel, WorkflowState, WorkflowStatus, WorkflowSpecModel, \
|
||||
WorkflowModel
|
||||
|
@ -106,7 +106,8 @@ class Study(object):
|
|||
def __init__(self, title, last_updated, primary_investigator_id, user_uid,
|
||||
id=None,
|
||||
protocol_builder_status=None,
|
||||
sponsor="", hsr_number="", ind_number="", categories=[], **argsv):
|
||||
sponsor="", hsr_number="", ind_number="", categories=[],
|
||||
files=[], **argsv):
|
||||
self.id = id
|
||||
self.user_uid = user_uid
|
||||
self.title = title
|
||||
|
@ -118,7 +119,7 @@ class Study(object):
|
|||
self.ind_number = ind_number
|
||||
self.categories = categories
|
||||
self.warnings = []
|
||||
self.files = []
|
||||
self.files = files
|
||||
|
||||
@classmethod
|
||||
def from_model(cls, study_model: StudyModel):
|
||||
|
@ -149,12 +150,12 @@ class StudySchema(ma.Schema):
|
|||
hsr_number = fields.String(allow_none=True)
|
||||
sponsor = fields.String(allow_none=True)
|
||||
ind_number = fields.String(allow_none=True)
|
||||
files = fields.List(fields.Nested(SimpleFileSchema), dump_only=True)
|
||||
files = fields.List(fields.Nested(FileSchema), dump_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Study
|
||||
additional = ["id", "title", "last_updated", "primary_investigator_id", "user_uid",
|
||||
"sponsor", "ind_number"]
|
||||
"sponsor", "ind_number", "files"]
|
||||
unknown = INCLUDE
|
||||
|
||||
@marshmallow.post_load
|
||||
|
|
|
@ -22,6 +22,14 @@ class FileService(object):
|
|||
DOCUMENT_LIST = "irb_documents.xlsx"
|
||||
INVESTIGATOR_LIST = "investigators.xlsx"
|
||||
|
||||
__doc_dictionary = None
|
||||
|
||||
@staticmethod
|
||||
def get_doc_dictionary():
|
||||
if not FileService.__doc_dictionary:
|
||||
FileService.__doc_dictionary = FileService.get_reference_data(FileService.DOCUMENT_LIST, 'code', ['id'])
|
||||
return FileService.__doc_dictionary
|
||||
|
||||
@staticmethod
|
||||
def add_workflow_spec_file(workflow_spec: WorkflowSpecModel,
|
||||
name, content_type, binary_data, primary=False, is_status=False):
|
||||
|
|
|
@ -9,7 +9,7 @@ from ldap3.core.exceptions import LDAPSocketOpenError
|
|||
|
||||
from crc import db, session, app
|
||||
from crc.api.common import ApiError
|
||||
from crc.models.file import FileModel, FileModelSchema
|
||||
from crc.models.file import FileModel, FileModelSchema, File
|
||||
from crc.models.protocol_builder import ProtocolBuilderStudy, ProtocolBuilderStatus
|
||||
from crc.models.stats import TaskEventModel
|
||||
from crc.models.study import StudyModel, Study, Category, WorkflowMetadata
|
||||
|
@ -54,7 +54,11 @@ class StudyService(object):
|
|||
study = Study.from_model(study_model)
|
||||
study.categories = StudyService.get_categories()
|
||||
workflow_metas = StudyService.__get_workflow_metas(study_id)
|
||||
study.files = FileService.get_files_for_study(study.id)
|
||||
|
||||
files = FileService.get_files_for_study(study.id)
|
||||
files = (File.from_models(model, FileService.get_file_data(model.id),
|
||||
FileService.get_doc_dictionary()) for model in files)
|
||||
study.files = list(files)
|
||||
|
||||
# Calling this line repeatedly is very very slow. It creates the
|
||||
# master spec and runs it.
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import json
|
||||
from tests.base_test import BaseTest
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import patch
|
||||
|
||||
|
@ -8,8 +9,9 @@ from crc.models.protocol_builder import ProtocolBuilderStatus, \
|
|||
ProtocolBuilderStudySchema
|
||||
from crc.models.stats import TaskEventModel
|
||||
from crc.models.study import StudyModel, StudySchema
|
||||
from crc.models.workflow import WorkflowSpecModel, WorkflowModel, WorkflowSpecCategoryModel
|
||||
from crc.services.protocol_builder import ProtocolBuilderService
|
||||
from crc.models.workflow import WorkflowSpecModel, WorkflowModel
|
||||
from crc.services.file_service import FileService
|
||||
from crc.services.workflow_processor import WorkflowProcessor
|
||||
|
||||
|
||||
class TestStudyApi(BaseTest):
|
||||
|
@ -68,6 +70,29 @@ class TestStudyApi(BaseTest):
|
|||
self.assertEqual(0, workflow["total_tasks"])
|
||||
self.assertEqual(0, workflow["completed_tasks"])
|
||||
|
||||
def test_get_study_has_details_about_files(self):
|
||||
|
||||
# Set up the study and attach a file to it.
|
||||
self.load_example_data()
|
||||
self.create_reference_document()
|
||||
workflow = self.create_workflow('file_upload_form')
|
||||
processor = WorkflowProcessor(workflow)
|
||||
task = processor.next_task()
|
||||
irb_code = "UVACompl_PRCAppr" # The first file referenced in pb required docs.
|
||||
FileService.add_workflow_file(workflow_id=workflow.id,
|
||||
name="anything.png", content_type="png",
|
||||
binary_data=b'1234', irb_doc_code=irb_code)
|
||||
|
||||
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 = StudySchema().loads(api_response.get_data(as_text=True))
|
||||
self.assertEquals(1, len(study.files))
|
||||
self.assertEquals("UVA Compliance/PRC Approval", study.files[0]["category"])
|
||||
self.assertEquals("Cancer Center's PRC Approval Form", study.files[0]["description"])
|
||||
self.assertEquals("UVA Compliance/PRC Approval.png", study.files[0]["download_name"])
|
||||
|
||||
|
||||
def test_add_study(self):
|
||||
self.load_example_data()
|
||||
study = self.add_test_study()
|
||||
|
|
Loading…
Reference in New Issue