def synch_with_protocol_builder_if_enabled(user): """Assures that the studies we have locally for the given user are in sync with the studies available in protocol builder. """ if ProtocolBuilderService.is_enabled(): app.logger.info("The Protocol Builder is enabled. app.config['PB_ENABLED'] = " + str(app.config['PB_ENABLED'])) # Get studies matching this user from Protocol Builder pb_studies: List[ProtocolBuilderStudy] = ProtocolBuilderService.get_studies(user.uid) # Get studies from the database db_studies = session.query(StudyModel).filter_by(user_uid=user.uid).all() # Update all studies from the protocol builder, create new studies as needed. # Futher assures that every active study (that does exist in the protocol builder) # has a reference to every available workflow (though some may not have started yet) for pb_study in pb_studies: db_study = next((s for s in db_studies if s.id == pb_study.STUDYID), None) if not db_study: db_study = StudyModel(id=pb_study.STUDYID) session.add(db_study) db_studies.append(db_study) db_study.update_from_protocol_builder(pb_study) StudyService._add_all_workflow_specs_to_study(db_study) # Mark studies as inactive that are no longer in Protocol Builder for study in db_studies: pb_study = next((pbs for pbs in pb_studies if pbs.STUDYID == study.id), None) if not pb_study: study.protocol_builder_status = ProtocolBuilderStatus.ABANDONED db.session.commit()
def synch_with_protocol_builder_if_enabled(user): """Assures that the studies we have locally for the given user are in sync with the studies available in protocol builder. """ if ProtocolBuilderService.is_enabled(): app.logger.info( "The Protocol Builder is enabled. app.config['PB_ENABLED'] = " + str(app.config['PB_ENABLED'])) # Get studies matching this user from Protocol Builder pb_studies: List[ ProtocolBuilderCreatorStudy] = ProtocolBuilderService.get_studies( user.uid) # Get studies from the database db_studies = session.query(StudyModel).filter_by( user_uid=user.uid).all() # Update all studies from the protocol builder, create new studies as needed. # Further assures that every active study (that does exist in the protocol builder) # has a reference to every available workflow (though some may not have started yet) for pb_study in pb_studies: new_status = None new_progress_status = None db_study = next( (s for s in db_studies if s.id == pb_study.STUDYID), None) if not db_study: db_study = StudyModel(id=pb_study.STUDYID) db_study.status = None # Force a new sa new_status = StudyStatus.in_progress new_progress_status = ProgressStatus.in_progress session.add(db_study) db_studies.append(db_study) db_study.update_from_protocol_builder(pb_study, user.uid) StudyService._add_all_workflow_specs_to_study(db_study) # If there is a new automatic status change and there isn't a manual change in place, record it. if new_status and db_study.status != StudyStatus.hold: db_study.status = new_status # make sure status is `in_progress`, before processing new automatic progress_status. if new_progress_status and db_study.status == StudyStatus.in_progress: db_study.progress_status = new_progress_status StudyService.add_study_update_event( db_study, status=new_status, event_type=StudyEventType.automatic) # Mark studies as inactive that are no longer in Protocol Builder for study in db_studies: pb_study = next( (pbs for pbs in pb_studies if pbs.STUDYID == study.id), None) if not pb_study and study.status != StudyStatus.abandoned: study.status = StudyStatus.abandoned StudyService.add_study_update_event( study, status=StudyStatus.abandoned, event_type=StudyEventType.automatic) db.session.commit()