From bec59a71d71ac7f99d5c082ec73bf9933692fadb Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Wed, 29 Apr 2020 16:07:39 -0400 Subject: [PATCH] Deleteing stuff is a damn mess, but this is a little cleaner. --- crc/api.yml | 2 +- crc/api/workflow.py | 12 +++++------- crc/services/study_service.py | 15 ++++++++++++--- tests/test_tasks_api.py | 5 +++++ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/crc/api.yml b/crc/api.yml index 0d556c0f..16158ba9 100644 --- a/crc/api.yml +++ b/crc/api.yml @@ -608,7 +608,7 @@ paths: schema: $ref: "#/components/schemas/Workflow" delete: - operationId: crc.api.workflow.delete + operationId: crc.api.workflow.delete_workflow summary: Removes an existing workflow tags: - Workflows and Tasks diff --git a/crc/api/workflow.py b/crc/api/workflow.py index 7b3dad9e..7c531d8e 100644 --- a/crc/api/workflow.py +++ b/crc/api/workflow.py @@ -9,6 +9,7 @@ from crc.models.stats import WorkflowStatsModel, TaskEventModel from crc.models.workflow import WorkflowModel, WorkflowSpecModelSchema, WorkflowSpecModel, WorkflowSpecCategoryModel, \ WorkflowSpecCategoryModelSchema from crc.services.file_service import FileService +from crc.services.study_service import StudyService from crc.services.workflow_processor import WorkflowProcessor from crc.services.workflow_service import WorkflowService @@ -78,8 +79,8 @@ def delete_workflow_specification(spec_id): FileService.delete_file(file.id) # Delete all stats and workflow models related to this specification - session.query(WorkflowStatsModel).filter_by(workflow_spec_id=spec_id).delete() - session.query(WorkflowModel).filter_by(workflow_spec_id=spec_id).delete() + for workflow in session.query(WorkflowModel).filter_by(workflow_spec_id=spec_id): + StudyService.delete_workflow(workflow) session.query(WorkflowSpecModel).filter_by(id=spec_id).delete() session.commit() @@ -115,11 +116,8 @@ def get_workflow(workflow_id, soft_reset=False, hard_reset=False): return WorkflowApiSchema().dump(workflow_api_model) -def delete(workflow_id): - session.query(TaskEventModel).filter_by(workflow_id=workflow_id).delete() - session.query(WorkflowStatsModel).filter_by(workflow_id=workflow_id).delete() - session.query(WorkflowModel).filter_by(id=workflow_id).delete() - session.commit() +def delete_workflow(workflow_id): + StudyService.delete_workflow(workflow_id) def set_current_task(workflow_id, task_id): workflow_model = session.query(WorkflowModel).filter_by(id=workflow_id).first() diff --git a/crc/services/study_service.py b/crc/services/study_service.py index 0a915bb9..515498f4 100644 --- a/crc/services/study_service.py +++ b/crc/services/study_service.py @@ -4,7 +4,7 @@ from SpiffWorkflow import WorkflowException from crc import db, session from crc.api.common import ApiError -from crc.models.file import FileModel +from crc.models.file import FileModel, FileDataModel from crc.models.protocol_builder import ProtocolBuilderStudy, ProtocolBuilderStatus from crc.models.stats import WorkflowStatsModel, TaskEventModel from crc.models.study import StudyModel, Study, Category, WorkflowMetadata @@ -49,12 +49,21 @@ class StudyService(object): @staticmethod def delete_study(study_id): - session.query(WorkflowStatsModel).filter_by(study_id=study_id).delete() session.query(TaskEventModel).filter_by(study_id=study_id).delete() - session.query(WorkflowModel).filter_by(study_id=study_id).delete() + session.query(WorkflowStatsModel).filter_by(study_id=study_id).delete() + for workflow in session.query(WorkflowModel).filter_by(study_id=study_id): + StudyService.delete_workflow(workflow.id) session.query(StudyModel).filter_by(id=study_id).delete() session.commit() + @staticmethod + def delete_workflow(workflow_id): + for file in session.query(FileModel).filter_by(workflow_id=workflow_id).all(): + FileService.delete_file(file.id) + session.query(TaskEventModel).filter_by(workflow_id=workflow_id).delete() + session.query(WorkflowStatsModel).filter_by(workflow_id=workflow_id).delete() + session.query(WorkflowModel).filter_by(id=workflow_id).delete() + @staticmethod def get_categories(): """Returns a list of category objects, in the correct order.""" diff --git a/tests/test_tasks_api.py b/tests/test_tasks_api.py index 2290a61a..f16fc458 100644 --- a/tests/test_tasks_api.py +++ b/tests/test_tasks_api.py @@ -164,6 +164,11 @@ class TestTasksApi(BaseTest): json_data = json.loads(rv.get_data(as_text=True)) files = FileModelSchema(many=True).load(json_data, session=session) self.assertTrue(len(files) == 1) + # Assure we can still delete the study even when there is a file attached to a workflow. + rv = self.app.delete('/v1.0/study/%i' % workflow.study_id, headers=self.logged_in_headers()) + self.assert_success(rv) + + def test_get_documentation_populated_in_end(self): self.load_example_data()