Move business logic to WorkflowService

Return ordered list of specs
This commit is contained in:
mike cullerton 2021-08-30 10:41:08 -04:00
parent 543fc1fb4a
commit f2201a3cef
2 changed files with 34 additions and 16 deletions

View File

@ -145,24 +145,17 @@ def delete_workflow_specification(spec_id):
def reorder_workflow_specification(spec_id, direction):
# TODO: Need to return list of specs
# TODO: Maybe move some of this code to services.workflow_service
if direction not in ('up', 'down'):
raise ApiError(code='bad_direction',
message='The direction must be `up` or `down`.')
spec = session.query(WorkflowSpecModel).filter(WorkflowSpecModel.id == spec_id).first()
category_id = spec.category_id
if direction == 'up':
neighbor = session.query(WorkflowSpecModel).filter(WorkflowSpecModel.category_id == category_id).filter(WorkflowSpecModel.display_order == spec.display_order - 1).first()
neighbor.display_order += 1
spec.display_order -= 1
if direction == 'down':
neighbor = session.query(WorkflowSpecModel).filter(WorkflowSpecModel.category_id == category_id).filter(WorkflowSpecModel.display_order == spec.display_order + 1).first()
neighbor.display_order -= 1
spec.display_order += 1
session.add(spec)
session.add(neighbor)
session.commit()
if spec:
ordered_specs = WorkflowService.reorder_workflow_spec(spec, direction)
else:
raise ApiError(code='bad_spec_id',
message=f'The spec_id {spec_id} did not return a specification. Please check that it is valid.')
schema = WorkflowSpecModelSchema(many=True)
return schema.dump(ordered_specs)
def get_workflow_from_spec(spec_id):

View File

@ -22,7 +22,7 @@ from SpiffWorkflow.util.metrics import timeit
from jinja2 import Template
from crc import db, app
from crc import db, app, session
from crc.api.common import ApiError
from crc.models.api_models import Task, MultiInstanceType, WorkflowApi
from crc.models.data_store import DataStoreModel
@ -30,7 +30,7 @@ from crc.models.file import LookupDataModel, FileModel, File, FileSchema
from crc.models.study import StudyModel
from crc.models.task_event import TaskEventModel
from crc.models.user import UserModel, UserModelSchema
from crc.models.workflow import WorkflowModel, WorkflowStatus, WorkflowSpecModel
from crc.models.workflow import WorkflowModel, WorkflowStatus, WorkflowSpecModel, WorkflowSpecModelSchema
from crc.services.data_store_service import DataStoreBase
from crc.services.document_service import DocumentService
@ -929,3 +929,28 @@ class WorkflowService(object):
if file:
primary = file
return primary
@staticmethod
def reorder_workflow_spec(spec, direction):
category_id = spec.category_id
if direction == 'up':
neighbor = session.query(WorkflowSpecModel). \
filter(WorkflowSpecModel.category_id == category_id). \
filter(WorkflowSpecModel.display_order == spec.display_order - 1). \
first()
neighbor.display_order += 1
spec.display_order -= 1
if direction == 'down':
neighbor = session.query(WorkflowSpecModel). \
filter(WorkflowSpecModel.category_id == category_id). \
filter(WorkflowSpecModel.display_order == spec.display_order + 1). \
first()
neighbor.display_order -= 1
spec.display_order += 1
session.add(spec)
session.add(neighbor)
session.commit()
ordered_specs = session.query(WorkflowSpecModel). \
filter(WorkflowSpecModel.category_id == category_id). \
order_by(WorkflowSpecModel.display_order).all()
return ordered_specs