Merge pull request #260 from sartography/bug/delete_study

Allows us to delete a study even if there are emails, and associated …
This commit is contained in:
Mike Cullerton 2021-03-09 13:51:25 -05:00 committed by GitHub
commit eaf52a21ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 5 deletions

View File

@ -9,6 +9,7 @@ from ldap3.core.exceptions import LDAPSocketOpenError
from crc import db, session, app from crc import db, session, app
from crc.api.common import ApiError from crc.api.common import ApiError
from crc.models.email import EmailModel
from crc.models.file import FileDataModel, FileModel, FileModelSchema, File, LookupFileModel, LookupDataModel from crc.models.file import FileDataModel, FileModel, FileModelSchema, File, LookupFileModel, LookupDataModel
from crc.models.ldap import LdapSchema from crc.models.ldap import LdapSchema
from crc.models.protocol_builder import ProtocolBuilderStudy, ProtocolBuilderStatus from crc.models.protocol_builder import ProtocolBuilderStudy, ProtocolBuilderStatus
@ -210,6 +211,10 @@ class StudyService(object):
@staticmethod @staticmethod
def delete_study(study_id): def delete_study(study_id):
session.query(TaskEventModel).filter_by(study_id=study_id).delete() session.query(TaskEventModel).filter_by(study_id=study_id).delete()
session.query(StudyAssociated).filter_by(study_id=study_id).delete()
session.query(EmailModel).filter_by(study_id=study_id).delete()
session.query(StudyEvent).filter_by(study_id=study_id).delete()
for workflow in session.query(WorkflowModel).filter_by(study_id=study_id): for workflow in session.query(WorkflowModel).filter_by(study_id=study_id):
StudyService.delete_workflow(workflow.id) StudyService.delete_workflow(workflow.id)
study = session.query(StudyModel).filter_by(id=study_id).first() study = session.query(StudyModel).filter_by(id=study_id).first()

View File

@ -1,17 +1,19 @@
import json import json
from profile import Profile from profile import Profile
from crc.services.ldap_service import LdapService
from tests.base_test import BaseTest from tests.base_test import BaseTest
from datetime import datetime, timezone from datetime import datetime, timezone
from unittest.mock import patch from unittest.mock import patch
from crc.models.email import EmailModel
from crc import session, app from crc import session, app
from crc.models.protocol_builder import ProtocolBuilderStatus, \ from crc.models.protocol_builder import ProtocolBuilderStatus, \
ProtocolBuilderStudySchema ProtocolBuilderStudySchema
from crc.models.file import FileModel from crc.models.file import FileModel
from crc.models.task_event import TaskEventModel from crc.models.task_event import TaskEventModel
from crc.models.study import StudyEvent, StudyModel, StudySchema, StudyStatus, StudyEventType from crc.models.study import StudyEvent, StudyModel, StudySchema, StudyStatus, StudyEventType, StudyAssociated
from crc.models.workflow import WorkflowSpecModel, WorkflowModel from crc.models.workflow import WorkflowSpecModel, WorkflowModel
from crc.services.file_service import FileService from crc.services.file_service import FileService
from crc.services.workflow_processor import WorkflowProcessor from crc.services.workflow_processor import WorkflowProcessor
@ -274,7 +276,7 @@ class TestStudyApi(BaseTest):
self.assertTrue(file.archived) self.assertTrue(file.archived)
self.assertIsNone(file.workflow_id) self.assertIsNone(file.workflow_id)
def test_delete_study_with_workflow_and_status(self): def test_delete_study_with_workflow_and_status_etc(self):
self.load_example_data() self.load_example_data()
workflow = session.query(WorkflowModel).first() workflow = session.query(WorkflowModel).first()
stats1 = StudyEvent( stats1 = StudyEvent(
@ -284,6 +286,14 @@ class TestStudyApi(BaseTest):
event_type=StudyEventType.user, event_type=StudyEventType.user,
user_uid=self.users[0]['uid'], user_uid=self.users[0]['uid'],
) )
LdapService.user_info('dhf8r') # Assure that there is a dhf8r in ldap for StudyAssociated.
email = EmailModel(subject="x", study_id=workflow.study_id)
associate = StudyAssociated(study_id=workflow.study_id, uid=self.users[0]['uid'])
event = StudyEvent(study_id=workflow.study_id)
session.add_all([email, associate, event])
stats2 = TaskEventModel(study_id=workflow.study_id, workflow_id=workflow.id, user_uid=self.users[0]['uid']) stats2 = TaskEventModel(study_id=workflow.study_id, workflow_id=workflow.id, user_uid=self.users[0]['uid'])
session.add_all([stats1, stats2]) session.add_all([stats1, stats2])
session.commit() session.commit()
@ -293,7 +303,6 @@ class TestStudyApi(BaseTest):
self.assertIsNone(del_study) self.assertIsNone(del_study)
# """ # """
# Workflow Specs that have been made available (or not) to a particular study via the status.bpmn should be flagged # Workflow Specs that have been made available (or not) to a particular study via the status.bpmn should be flagged
# as available (or not) when the list of a study's workflows is retrieved. # as available (or not) when the list of a study's workflows is retrieved.

View File

@ -66,7 +66,7 @@ class DataStoreTest(BaseTest):
def test_update_study(self): def test_update_datastore(self):
self.load_example_data() self.load_example_data()
new_study = self.add_test_study_data() new_study = self.add_test_study_data()
new_study = session.query(DataStoreModel).filter_by(id=new_study["id"]).first() new_study = session.query(DataStoreModel).filter_by(id=new_study["id"]).first()
@ -87,7 +87,7 @@ class DataStoreTest(BaseTest):
def test_delete_study(self): def test_delete_datastore(self):
self.load_example_data() self.load_example_data()
new_study = self.add_test_study_data() new_study = self.add_test_study_data()
oldid = new_study['id'] oldid = new_study['id']