meta field for categories

This commit is contained in:
alicia pritchett 2022-03-16 12:49:35 -04:00
parent 829c810807
commit 5b7e71d42d
5 changed files with 42 additions and 20 deletions

View File

@ -5,7 +5,7 @@ from sqlalchemy.exc import IntegrityError
from crc import session from crc import session
from crc.api.common import ApiError, ApiErrorSchema from crc.api.common import ApiError, ApiErrorSchema
from crc.models.study import Study, StudyEventType, StudyModel, StudySchema, StudyForUpdateSchema, \ from crc.models.study import Study, StudyEventType, StudyModel, StudySchema, StudyForUpdateSchema, \
StudyStatus, StudyAssociatedSchema StudyStatus, StudyAssociatedSchema, Category
from crc.models.task_log import TaskLogQuery, TaskLogQuerySchema from crc.models.task_log import TaskLogQuery, TaskLogQuerySchema
from crc.services.spreadsheet_service import SpreadsheetService from crc.services.spreadsheet_service import SpreadsheetService
from crc.services.study_service import StudyService from crc.services.study_service import StudyService

View File

@ -115,10 +115,10 @@ class StudyEvent(db.Model):
class CategoryMetadata(object): class CategoryMetadata(object):
def __init__(self, id, state: WorkflowState = None, state_message=None): def __init__(self, id=None, state: WorkflowState = None, message=None):
self.id = id self.id = id
self.state = state self.state = state
self.state_message = state_message self.message = message
class CategoryMetadataSchema(ma.Schema): class CategoryMetadataSchema(ma.Schema):
@ -126,7 +126,7 @@ class CategoryMetadataSchema(ma.Schema):
class Meta: class Meta:
model = CategoryMetadata model = CategoryMetadata
additional = ["id", "state_message"] additional = ["id", "message"]
unknown = INCLUDE unknown = INCLUDE
@ -191,6 +191,7 @@ class Category(object):
class CategorySchema(ma.Schema): class CategorySchema(ma.Schema):
workflows = fields.List(fields.Nested(WorkflowMetadataSchema), dump_only=True) workflows = fields.List(fields.Nested(WorkflowMetadataSchema), dump_only=True)
meta = fields.Nested(CategoryMetadataSchema)
class Meta: class Meta:
model = Category model = Category

View File

@ -16,7 +16,7 @@ class WorkflowSpecCategory(object):
self.admin = admin self.admin = admin
self.workflows = [] # For storing Workflow Metadata self.workflows = [] # For storing Workflow Metadata
self.specs = [] # For the list of specifications associated with a category self.specs = [] # For the list of specifications associated with a category
self.meta = [] self.meta = None # For storing category metadata
def __eq__(self, other): def __eq__(self, other):
if not isinstance(other, WorkflowSpecCategory): if not isinstance(other, WorkflowSpecCategory):
@ -29,7 +29,7 @@ class WorkflowSpecCategory(object):
class WorkflowSpecCategorySchema(ma.Schema): class WorkflowSpecCategorySchema(ma.Schema):
class Meta: class Meta:
model = WorkflowSpecCategory model = WorkflowSpecCategory
fields = ["id", "display_name", "display_order", "admin", "meta"] fields = ["id", "display_name", "display_order", "admin"]
@post_load @post_load
def make_cat(self, data, **kwargs): def make_cat(self, data, **kwargs):

View File

@ -19,7 +19,7 @@ from crc.models.ldap import LdapSchema
from crc.models.protocol_builder import ProtocolBuilderCreatorStudy from crc.models.protocol_builder import ProtocolBuilderCreatorStudy
from crc.models.study import StudyModel, Study, StudyStatus, Category, WorkflowMetadata, StudyEventType, StudyEvent, \ from crc.models.study import StudyModel, Study, StudyStatus, Category, WorkflowMetadata, StudyEventType, StudyEvent, \
StudyAssociated, ProgressStatus StudyAssociated, ProgressStatus, CategoryMetadata
from crc.models.task_event import TaskEventModel from crc.models.task_event import TaskEventModel
from crc.models.task_log import TaskLogModel from crc.models.task_log import TaskLogModel
from crc.models.workflow import WorkflowSpecCategory, WorkflowModel, WorkflowSpecInfo, WorkflowState, \ from crc.models.workflow import WorkflowSpecCategory, WorkflowModel, WorkflowSpecInfo, WorkflowState, \
@ -101,12 +101,13 @@ class StudyService(object):
if study.status != StudyStatus.abandoned: if study.status != StudyStatus.abandoned:
for category in study.categories: for category in study.categories:
workflow_metas = StudyService._get_workflow_metas(study_id, category) workflow_metas = StudyService._get_workflow_metas(study_id, category)
category_metas = StudyService._get_category_metas(categories) category_meta = []
if master_workflow_results: if master_workflow_results:
study.warnings = StudyService._update_status_of_workflow_meta(workflow_metas, study.warnings = StudyService._update_status_of_workflow_meta(workflow_metas,
master_workflow_results) master_workflow_results)
StudyService._update_status_of_category_meta(category_metas, master_workflow_results) category_meta = StudyService._update_status_of_category_meta(master_workflow_results, category)
category.workflows = workflow_metas category.workflows = workflow_metas
category.meta = category_meta
return study return study
@staticmethod @staticmethod
@ -122,13 +123,6 @@ class StudyService(object):
workflow_metas.append(WorkflowMetadata.from_workflow(workflow, spec)) workflow_metas.append(WorkflowMetadata.from_workflow(workflow, spec))
return workflow_metas return workflow_metas
@staticmethod
def _get_category_metas(categories):
category_metas = []
for cat in categories:
category_metas.append(cat.meta)
return category_metas
@staticmethod @staticmethod
def get_study_associate(study_id=None, uid=None): def get_study_associate(study_id=None, uid=None):
@ -458,9 +452,15 @@ class StudyService(object):
db.session.commit() db.session.commit()
@staticmethod @staticmethod
def _update_status_of_category_meta(cat_metas, status): def _update_status_of_category_meta(status, cat):
warnings = [] cat_meta = CategoryMetadata()
unused_statuses = status.copy() unused_statuses = status.copy()
if unused_statuses.get(cat.id):
cat_meta.id = cat.id
cat_meta.state = WorkflowState[unused_statuses.get(cat.id)['status']]
cat_meta.message = unused_statuses.get(cat.id)['message']
return cat_meta
@staticmethod @staticmethod
def _update_status_of_workflow_meta(workflow_metas, status): def _update_status_of_workflow_meta(workflow_metas, status):

View File

@ -5,9 +5,10 @@ from unittest.mock import patch
from tests.base_test import BaseTest from tests.base_test import BaseTest
from crc import db, app from crc import db, app
from crc.models.study import StudyModel, StudyStatus, StudyAssociatedSchema from crc.models.study import StudyModel, StudyStatus, StudyAssociatedSchema, CategoryMetadata, StudySchema, \
CategorySchema, Category
from crc.models.user import UserModel from crc.models.user import UserModel
from crc.models.workflow import WorkflowModel, WorkflowStatus, WorkflowSpecCategory from crc.models.workflow import WorkflowModel, WorkflowStatus, WorkflowSpecCategory, WorkflowSpecCategorySchema
from crc.services.ldap_service import LdapService from crc.services.ldap_service import LdapService
from crc.services.study_service import StudyService from crc.services.study_service import StudyService
from crc.services.workflow_processor import WorkflowProcessor from crc.services.workflow_processor import WorkflowProcessor
@ -257,3 +258,23 @@ class TestStudyService(BaseTest):
assoc_json = StudyAssociatedSchema(many=True).dump(associates) assoc_json = StudyAssociatedSchema(many=True).dump(associates)
print(assoc_json) print(assoc_json)
self.assertEquals("Dan", assoc_json[0]['ldap_info']['given_name']) self.assertEquals("Dan", assoc_json[0]['ldap_info']['given_name'])
def test_set_category_metadata(self):
user = self.create_user_with_study_and_workflow()
study = db.session.query(StudyModel).filter_by(user_uid=user.uid).first()
####
wfscat = WorkflowSpecCategory(id='test_cat', display_name='test cat', display_order=0, admin=False)
wfs = self.create_workflow('empty_workflow')
wfscat.workflows = wfs
x = WorkflowSpecCategorySchema().dump(wfscat)
####
s = StudyService.get_study(study.id, [wfscat], None, {'test_cat':{'status':'hidden', 'message': 'msg'}})
d = StudySchema().dump(s)
self.assertEqual({'id': 'test_cat', 'message': 'msg', 'state': 'hidden'}, d['categories'][0]['meta'])