Updating properly study status & fixing tests

This commit is contained in:
Carlos Lopez 2020-07-27 22:39:19 -06:00
parent 0ea4c13d09
commit 6aa21638de
3 changed files with 81 additions and 7 deletions

View File

@ -1,3 +1,6 @@
import datetime
import json
import marshmallow
from marshmallow import INCLUDE, fields
from marshmallow_enum import EnumField
@ -26,6 +29,7 @@ class StudyModel(db.Model):
requirements = db.Column(db.ARRAY(db.Integer), nullable=True)
on_hold = db.Column(db.Boolean, default=False)
enrollment_date = db.Column(db.DateTime(timezone=True), nullable=True)
changes_history = db.Column(db.JSON, nullable=True)
def update_from_protocol_builder(self, pbs: ProtocolBuilderStudy):
self.hsr_number = pbs.HSRNUMBER
@ -135,9 +139,29 @@ class Study(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
"""As the case for update was very reduced, it's mostly and specifically
updating only the study status and generating a history record
"""
pb_status = ProtocolBuilderStatus(self.protocol_builder_status)
study_model.last_updated = datetime.datetime.now()
study_model.protocol_builder_status = pb_status
if pb_status == ProtocolBuilderStatus.OPEN:
study_model.enrollment_date = self.enrollment_date
change = {
'status': ProtocolBuilderStatus(self.protocol_builder_status).value,
'comment': '' if not hasattr(self, 'comment') else self.comment,
'date': str(datetime.datetime.now())
}
if study_model.changes_history:
changes_history = json.loads(study_model.changes_history)
changes_history.append(change)
else:
changes_history = [change]
study_model.changes_history = json.dumps(changes_history)
def model_args(self):
"""Arguments that can be passed into the Study Model to update it."""
@ -147,6 +171,26 @@ class Study(object):
return self_dict
class StudyForUpdateSchema(ma.Schema):
id = fields.Integer(required=False, allow_none=True)
protocol_builder_status = EnumField(ProtocolBuilderStatus, by_value=True)
hsr_number = fields.String(allow_none=True)
sponsor = fields.String(allow_none=True)
ind_number = fields.String(allow_none=True)
enrollment_date = fields.DateTime(allow_none=True)
comment = fields.String(allow_none=True)
class Meta:
model = Study
unknown = INCLUDE
@marshmallow.post_load
def make_study(self, data, **kwargs):
"""Can load the basic study data for updates to the database, but categories are write only"""
return Study(**data)
class StudySchema(ma.Schema):
id = fields.Integer(required=False, allow_none=True)

View File

@ -0,0 +1,28 @@
"""empty message
Revision ID: 369d65dcb269
Revises: c4ddb69e7ef4
Create Date: 2020-07-27 20:05:29.524553
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '369d65dcb269'
down_revision = 'c4ddb69e7ef4'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('study', sa.Column('changes_history', sa.JSON(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('study', 'changes_history')
# ### end Alembic commands ###

View File

@ -24,16 +24,17 @@ class TestStudyApi(BaseTest):
"title": "Phase III Trial of Genuine People Personalities (GPP) Autonomous Intelligent Emotional Agents "
"for Interstellar Spacecraft",
"last_updated": datetime.now(tz=timezone.utc),
"protocol_builder_status": ProtocolBuilderStatus.active,
"primary_investigator_id": "tmm2x",
"user_uid": "dhf8r",
}
def add_test_study(self):
study_schema = StudySchema().dump(self.TEST_STUDY)
study_schema['protocol_builder_status'] = ProtocolBuilderStatus.ACTIVE.value
rv = self.app.post('/v1.0/study',
content_type="application/json",
headers=self.logged_in_headers(),
data=json.dumps(StudySchema().dump(self.TEST_STUDY)))
data=json.dumps(study_schema))
self.assert_success(rv)
return json.loads(rv.get_data(as_text=True))
@ -135,11 +136,12 @@ class TestStudyApi(BaseTest):
self.load_example_data()
study: StudyModel = session.query(StudyModel).first()
study.title = "Pilot Study of Fjord Placement for Single Fraction Outcomes to Cortisol Susceptibility"
study.protocol_builder_status = ProtocolBuilderStatus.active
study_schema = StudySchema().dump(study)
study_schema['protocol_builder_status'] = ProtocolBuilderStatus.ACTIVE.value
rv = self.app.put('/v1.0/study/%i' % study.id,
content_type="application/json",
headers=self.logged_in_headers(),
data=json.dumps(StudySchema().dump(study)))
data=json.dumps(study_schema))
self.assert_success(rv)
json_data = json.loads(rv.get_data(as_text=True))
self.assertEqual(study.title, json_data['title'])