From a99818b17732468a4a176e8e8007f9a42ece9556 Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Mon, 30 Aug 2021 11:42:44 -0400 Subject: [PATCH] Make sure we don't try to move past the top or bottom of the list. --- crc/services/workflow_service.py | 34 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/crc/services/workflow_service.py b/crc/services/workflow_service.py index 2141348f..65e94524 100755 --- a/crc/services/workflow_service.py +++ b/crc/services/workflow_service.py @@ -940,18 +940,21 @@ class WorkflowService(object): filter(WorkflowSpecModel.category_id == category_id). \ filter(WorkflowSpecModel.display_order == spec.display_order - 1). \ first() - neighbor.display_order += 1 - spec.display_order -= 1 + if neighbor: + 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 neighbor: + neighbor.display_order -= 1 + spec.display_order += 1 + if neighbor: + 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() @@ -965,17 +968,20 @@ class WorkflowService(object): neighbor = session.query(WorkflowSpecCategoryModel).\ filter(WorkflowSpecCategoryModel.display_order == category.display_order - 1).\ first() - neighbor.display_order += 1 - category.display_order -= 1 + if neighbor: + neighbor.display_order += 1 + category.display_order -= 1 if direction == 'down': neighbor = session.query(WorkflowSpecCategoryModel).\ filter(WorkflowSpecCategoryModel.display_order == category.display_order + 1).\ first() - neighbor.display_order -= 1 - category.display_order += 1 - session.add(neighbor) - session.add(category) - session.commit() + if neighbor: + neighbor.display_order -= 1 + category.display_order += 1 + if neighbor: + session.add(neighbor) + session.add(category) + session.commit() ordered_categories = session.query(WorkflowSpecCategoryModel).\ order_by(WorkflowSpecCategoryModel.display_order).all() return ordered_categories