diff --git a/crc/api/study.py b/crc/api/study.py index 21363605..38ad15bc 100644 --- a/crc/api/study.py +++ b/crc/api/study.py @@ -1,18 +1,16 @@ from typing import List -from dateutil.parser import parse from connexion import NoContent from flask import g from crc import session, auth from crc.api.common import ApiError, ApiErrorSchema from crc.api.workflow import __get_workflow_api_model +from crc.models.protocol_builder import ProtocolBuilderStatus, ProtocolBuilderStudy from crc.models.study import StudyModelSchema, StudyModel -from models.protocol_builder import ProtocolBuilderStatus, ProtocolBuilderStudy from crc.models.workflow import WorkflowModel, WorkflowApiSchema, WorkflowSpecModel -from crc.services import protocol_builder from crc.services.workflow_processor import WorkflowProcessor -from services.protocol_builder import ProtocolBuilderService +from crc.services.protocol_builder import ProtocolBuilderService @auth.login_required @@ -77,10 +75,13 @@ def update_from_protocol_builder(): db_studies = session.query(StudyModel).filter_by(user_uid=user.uid).all() db_study_ids = list(map(lambda s: s.id, db_studies)) + pb_study_ids = list(map(lambda s: s.id, pb_studies)) + # Add studies from Protocol Builder that aren't in the database yet for pb_study in pb_studies: if pb_study['HSRNUMBER'] not in db_study_ids: - status = ProtocolBuilderStatus.complete._value_ if pb_study['Q_COMPLETE'] else ProtocolBuilderStatus.in_process._value_ + status = ProtocolBuilderStatus.complete._value_ if pb_study[ + 'Q_COMPLETE'] else ProtocolBuilderStatus.in_process._value_ add_study({ 'id': pb_study['HSRNUMBER'], 'title': pb_study['TITLE'], @@ -89,6 +90,11 @@ def update_from_protocol_builder(): 'last_updated': pb_study['DATE_MODIFIED'] }) + # Mark studies as inactive that are no longer in Protocol Builder + for study_id in db_study_ids: + if study_id not in pb_study_ids: + update_study(study_id=study_id, body={'inactive': True}) + def post_update_study_from_protocol_builder(study_id): """Update a single study based on data received from diff --git a/crc/models/study.py b/crc/models/study.py index 34383b50..8ba96f2e 100644 --- a/crc/models/study.py +++ b/crc/models/study.py @@ -3,7 +3,7 @@ from marshmallow_sqlalchemy import ModelSchema from sqlalchemy import func from crc import db -from models.protocol_builder import ProtocolBuilderStatus +from crc.models.protocol_builder import ProtocolBuilderStatus class StudyModel(db.Model): @@ -17,6 +17,7 @@ class StudyModel(db.Model): ind_number = db.Column(db.String) user_uid = db.Column(db.String, db.ForeignKey('user.uid'), nullable=True) investigator_uids = db.Column(db.ARRAY(db.String)) + inactive = db.Column(db.Boolean, default=False) class StudyModelSchema(ModelSchema): diff --git a/crc/services/protocol_builder.py b/crc/services/protocol_builder.py index 7bb01a5c..e8387272 100644 --- a/crc/services/protocol_builder.py +++ b/crc/services/protocol_builder.py @@ -4,7 +4,7 @@ from typing import List, Optional import requests from crc import app -from models.protocol_builder import ProtocolBuilderStudy, ProtocolBuilderStudySchema, ProtocolBuilderInvestigator, \ +from crc.models.protocol_builder import ProtocolBuilderStudy, ProtocolBuilderStudySchema, ProtocolBuilderInvestigator, \ ProtocolBuilderRequiredDocument, ProtocolBuilderStudyDetails, ProtocolBuilderInvestigatorSchema, \ ProtocolBuilderRequiredDocumentSchema, ProtocolBuilderStudyDetailsSchema diff --git a/migrations/versions/2c88e49d0ffc_.py b/migrations/versions/2c88e49d0ffc_.py new file mode 100644 index 00000000..13ac0986 --- /dev/null +++ b/migrations/versions/2c88e49d0ffc_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: 2c88e49d0ffc +Revises: 726d09a4fa0c +Create Date: 2020-02-27 11:17:01.768161 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '2c88e49d0ffc' +down_revision = '726d09a4fa0c' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('study', sa.Column('inactive', sa.Boolean(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('study', 'inactive') + # ### end Alembic commands ### diff --git a/tests/test_study_api.py b/tests/test_study_api.py index 1c971311..3bc6518b 100644 --- a/tests/test_study_api.py +++ b/tests/test_study_api.py @@ -3,7 +3,7 @@ from datetime import datetime, timezone from crc import session from crc.models.study import StudyModel, StudyModelSchema -from models.protocol_builder import ProtocolBuilderStatus +from crc.models.protocol_builder import ProtocolBuilderStatus from crc.models.workflow import WorkflowSpecModel, WorkflowSpecModelSchema, WorkflowModel, WorkflowStatus, \ WorkflowApiSchema from tests.base_test import BaseTest