Merge pull request #289 from sartography/document-deletion-on-restart-279
Document deletion on restart #279
This commit is contained in:
commit
8fa79f1285
|
@ -874,6 +874,12 @@ paths:
|
||||||
description: Set this to true to clear data when starting workflow.
|
description: Set this to true to clear data when starting workflow.
|
||||||
schema:
|
schema:
|
||||||
type: boolean
|
type: boolean
|
||||||
|
- name: delete_files
|
||||||
|
in: query
|
||||||
|
required: false
|
||||||
|
description: Set this to true to delete associated files when starting workflow.
|
||||||
|
schema:
|
||||||
|
type: boolean
|
||||||
tags:
|
tags:
|
||||||
- Workflows and Tasks
|
- Workflows and Tasks
|
||||||
responses:
|
responses:
|
||||||
|
|
|
@ -116,11 +116,11 @@ def get_workflow(workflow_id, do_engine_steps=True):
|
||||||
return WorkflowApiSchema().dump(workflow_api_model)
|
return WorkflowApiSchema().dump(workflow_api_model)
|
||||||
|
|
||||||
|
|
||||||
def restart_workflow(workflow_id, clear_data=False):
|
def restart_workflow(workflow_id, clear_data=False, delete_files=False):
|
||||||
"""Restart a workflow with the latest spec.
|
"""Restart a workflow with the latest spec.
|
||||||
Clear data allows user to restart the workflow without previous data."""
|
Clear data allows user to restart the workflow without previous data."""
|
||||||
workflow_model: WorkflowModel = session.query(WorkflowModel).filter_by(id=workflow_id).first()
|
workflow_model: WorkflowModel = session.query(WorkflowModel).filter_by(id=workflow_id).first()
|
||||||
WorkflowProcessor.reset(workflow_model, clear_data=clear_data)
|
WorkflowProcessor.reset(workflow_model, clear_data=clear_data, delete_files=delete_files)
|
||||||
return get_workflow(workflow_model.id)
|
return get_workflow(workflow_model.id)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -174,7 +174,7 @@ class WorkflowProcessor(object):
|
||||||
self.is_latest_spec = False
|
self.is_latest_spec = False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def reset(workflow_model, clear_data=False):
|
def reset(workflow_model, clear_data=False, delete_files=False):
|
||||||
print('WorkflowProcessor: reset: ')
|
print('WorkflowProcessor: reset: ')
|
||||||
|
|
||||||
# Try to execute a cancel notify
|
# Try to execute a cancel notify
|
||||||
|
@ -194,6 +194,10 @@ class WorkflowProcessor(object):
|
||||||
for task_event in task_events:
|
for task_event in task_events:
|
||||||
task_event.form_data = {}
|
task_event.form_data = {}
|
||||||
session.add(task_event)
|
session.add(task_event)
|
||||||
|
if delete_files:
|
||||||
|
files = FileModel.query.filter(FileModel.workflow_id == workflow_model.id).all()
|
||||||
|
for file in files:
|
||||||
|
FileService.delete_file(file.id)
|
||||||
session.commit()
|
session.commit()
|
||||||
return WorkflowProcessor(workflow_model)
|
return WorkflowProcessor(workflow_model)
|
||||||
|
|
||||||
|
|
|
@ -367,11 +367,12 @@ class BaseTest(unittest.TestCase):
|
||||||
self.assertEqual(workflow.workflow_spec_id, workflow_api.workflow_spec_id)
|
self.assertEqual(workflow.workflow_spec_id, workflow_api.workflow_spec_id)
|
||||||
return workflow_api
|
return workflow_api
|
||||||
|
|
||||||
def restart_workflow_api(self, workflow, clear_data=False, user_uid="dhf8r"):
|
def restart_workflow_api(self, workflow, clear_data=False, delete_files=False, user_uid="dhf8r"):
|
||||||
user = session.query(UserModel).filter_by(uid=user_uid).first()
|
user = session.query(UserModel).filter_by(uid=user_uid).first()
|
||||||
self.assertIsNotNone(user)
|
self.assertIsNotNone(user)
|
||||||
url = (f'/v1.0/workflow/{workflow.id}/restart'
|
url = (f'/v1.0/workflow/{workflow.id}/restart'
|
||||||
f'?clear_data={str(clear_data)}')
|
f'?clear_data={str(clear_data)}'
|
||||||
|
f'&delete_files={str(delete_files)}')
|
||||||
workflow_api = self.get_workflow_common(url, user)
|
workflow_api = self.get_workflow_common(url, user)
|
||||||
self.assertEqual(workflow.workflow_spec_id, workflow_api.workflow_spec_id)
|
self.assertEqual(workflow.workflow_spec_id, workflow_api.workflow_spec_id)
|
||||||
return workflow_api
|
return workflow_api
|
||||||
|
|
|
@ -2,6 +2,9 @@ from tests.base_test import BaseTest
|
||||||
|
|
||||||
from crc import session
|
from crc import session
|
||||||
from crc.models.study import StudyModel
|
from crc.models.study import StudyModel
|
||||||
|
from crc.services.file_service import FileService
|
||||||
|
from crc.scripts.is_file_uploaded import IsFileUploaded
|
||||||
|
|
||||||
|
|
||||||
class TestWorkflowRestart(BaseTest):
|
class TestWorkflowRestart(BaseTest):
|
||||||
|
|
||||||
|
@ -33,7 +36,44 @@ class TestWorkflowRestart(BaseTest):
|
||||||
self.assertEqual('Activity_GetData', first_task.name)
|
self.assertEqual('Activity_GetData', first_task.name)
|
||||||
self.assertNotIn('formdata', workflow_api.next_task.data)
|
self.assertNotIn('formdata', workflow_api.next_task.data)
|
||||||
|
|
||||||
print('Nice Test')
|
|
||||||
|
def test_workflow_restart_delete_files(self):
|
||||||
|
self.load_example_data()
|
||||||
|
irb_code = 'Study_Protocol_Document'
|
||||||
|
|
||||||
|
workflow = self.create_workflow('add_delete_irb_document')
|
||||||
|
study_id = workflow.study_id
|
||||||
|
|
||||||
|
workflow_api = self.get_workflow_api(workflow)
|
||||||
|
first_task = workflow_api.next_task
|
||||||
|
|
||||||
|
# Should not have any files yet
|
||||||
|
files = FileService.get_files_for_study(study_id)
|
||||||
|
self.assertEqual(0, len(files))
|
||||||
|
self.assertEqual(False, IsFileUploaded.do_task(
|
||||||
|
IsFileUploaded, first_task, study_id, workflow.id, irb_code))
|
||||||
|
|
||||||
|
# Add a file
|
||||||
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
||||||
|
name="filename.txt", content_type="text",
|
||||||
|
binary_data=b'1234', irb_doc_code=irb_code)
|
||||||
|
# Assert we have the file
|
||||||
|
self.assertEqual(True, IsFileUploaded.do_task(
|
||||||
|
IsFileUploaded, first_task, study_id, workflow.id, irb_code))
|
||||||
|
|
||||||
|
workflow_api = self.restart_workflow_api(workflow_api, delete_files=False)
|
||||||
|
first_task = workflow_api.next_task
|
||||||
|
|
||||||
|
# Assert we still have the file
|
||||||
|
self.assertEqual(True, IsFileUploaded.do_task(
|
||||||
|
IsFileUploaded, first_task, study_id, workflow.id, irb_code))
|
||||||
|
|
||||||
|
workflow_api = self.restart_workflow_api(workflow_api, delete_files=True)
|
||||||
|
first_task = workflow_api.next_task
|
||||||
|
|
||||||
|
# Assert we do not have the file
|
||||||
|
self.assertEqual(False, IsFileUploaded.do_task(
|
||||||
|
IsFileUploaded, first_task, study_id, workflow.id, irb_code))
|
||||||
|
|
||||||
def test_workflow_restart_on_cancel_notify(self):
|
def test_workflow_restart_on_cancel_notify(self):
|
||||||
workflow = self.create_workflow('message_event')
|
workflow = self.create_workflow('message_event')
|
||||||
|
|
Loading…
Reference in New Issue