Adjusting study events attribute

This commit is contained in:
Carlos Lopez 2020-08-08 18:25:01 -06:00
parent bf3dae1885
commit a561a5cddc
2 changed files with 19 additions and 3 deletions

View File

@ -49,6 +49,8 @@ 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)
# events = db.relationship("TaskEventModel")
events_history = db.relationship("StudyEvent")
def update_from_protocol_builder(self, pbs: ProtocolBuilderStudy):
self.hsr_number = pbs.HSRNUMBER
@ -69,7 +71,7 @@ class StudyEvent(db.Model):
__tablename__ = 'study_event'
id = db.Column(db.Integer, primary_key=True)
study_id = db.Column(db.Integer, db.ForeignKey(StudyModel.id), nullable=False)
study = db.relationship(StudyModel)
study = db.relationship(StudyModel, back_populates='events_history')
create_date = db.Column(db.DateTime(timezone=True), default=func.now())
status = db.Column(db.Enum(StudyStatus))
comment = db.Column(db.String, default='')
@ -146,7 +148,7 @@ class Study(object):
def __init__(self, title, last_updated, primary_investigator_id, user_uid,
id=None, status=None, irb_status=None, comment="",
sponsor="", hsr_number="", ind_number="", categories=[],
files=[], approvals=[], enrollment_date=None, **argsv):
files=[], approvals=[], enrollment_date=None, events_history=[], **argsv):
self.id = id
self.user_uid = user_uid
self.title = title
@ -163,11 +165,13 @@ class Study(object):
self.warnings = []
self.files = files
self.enrollment_date = enrollment_date
self.events_history = events_history
@classmethod
def from_model(cls, study_model: StudyModel):
id = study_model.id # Just read some value, in case the dict expired, otherwise dict may be empty.
args = dict((k, v) for k, v in study_model.__dict__.items() if not k.startswith('_'))
args['events_history'] = study_model.events_history # For some reason this attribute is not picked up
instance = cls(**args)
return instance
@ -220,6 +224,15 @@ class StudyForUpdateSchema(ma.Schema):
return Study(**data)
class StudyEventSchema(ma.Schema):
id = fields.Integer(required=False)
create_date = fields.DateTime()
status = EnumField(StudyStatus, by_value=True)
comment = fields.String(allow_none=True)
event_type = EnumField(StudyEvent, by_value=True)
class StudySchema(ma.Schema):
id = fields.Integer(required=False, allow_none=True)
@ -233,11 +246,13 @@ 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_history = fields.List(fields.Nested('StudyEventSchema'), dump_only=True)
class Meta:
model = Study
additional = ["id", "title", "last_updated", "primary_investigator_id", "user_uid",
"sponsor", "ind_number", "approvals", "files", "enrollment_date"]
"sponsor", "ind_number", "approvals", "files", "enrollment_date",
"events_history"]
unknown = INCLUDE
@marshmallow.post_load

View File

@ -53,6 +53,7 @@ class StudyService(object):
loading up and executing all the workflows in a study to calculate information."""
if not study_model:
study_model = session.query(StudyModel).filter_by(id=study_id).first()
study = Study.from_model(study_model)
study.categories = StudyService.get_categories()
workflow_metas = StudyService.__get_workflow_metas(study_id)