From 618464aeed8663ceca5debec1b373a61fc42760b Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Sat, 19 Nov 2022 13:24:40 -0500 Subject: [PATCH] process group move api endpoint --- .../src/spiffworkflow_backend/api.yml | 27 ++++++++ .../routes/process_api_blueprint.py | 8 +++ .../services/process_model_service.py | 13 ++++ .../integration/test_process_api.py | 64 +++++++++++++++++++ 4 files changed, 112 insertions(+) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/api.yml b/spiffworkflow-backend/src/spiffworkflow_backend/api.yml index 2840d5ae..f7218003 100755 --- a/spiffworkflow-backend/src/spiffworkflow_backend/api.yml +++ b/spiffworkflow-backend/src/spiffworkflow_backend/api.yml @@ -238,6 +238,33 @@ paths: schema: $ref: "#/components/schemas/ProcessModelCategory" + /process-groups/{modified_process_group_id}/move: + parameters: + - name: modified_process_group_id + in: path + required: true + description: The unique id of an existing process group. + schema: + type: string + - name: new_location + in: query + required: true + description: the new location, as an existing process group id + schema: + type: string + put: + operationId: spiffworkflow_backend.routes.process_api_blueprint.process_group_move + summary: returns the new group + tags: + - Process Groups + responses: + "200": + description: Process Group + content: + application/json: + schema: + $ref: "#/components/schemas/ProcessModelCategory" + /process-models: parameters: - name: process_group_identifier diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/routes/process_api_blueprint.py b/spiffworkflow-backend/src/spiffworkflow_backend/routes/process_api_blueprint.py index 42930765..65509856 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/routes/process_api_blueprint.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/routes/process_api_blueprint.py @@ -234,6 +234,14 @@ def process_group_show( return make_response(jsonify(process_group), 200) +def process_group_move( + modified_process_group_id: str, new_location: str +) -> flask.wrappers.Response: + original_process_group_id = un_modify_modified_process_model_id(modified_process_group_id) + new_process_group = ProcessModelService().process_group_move(original_process_group_id, new_location) + return make_response(jsonify(new_process_group), 201) + + def process_model_create( modified_process_group_id: str, body: Dict[str, Union[str, bool, int]] ) -> flask.wrappers.Response: diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_model_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_model_service.py index 2431289c..44c765d7 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_model_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_model_service.py @@ -231,6 +231,19 @@ class ProcessModelService(FileSystemService): self.write_json_file(json_path, serialized_process_group) return process_group + def process_group_move(self, original_process_group_id, new_location) -> ProcessGroup: + original_group_path = self.process_group_path(original_process_group_id) + original_root, original_group_id = os.path.split(original_group_path) + new_root = f"{FileSystemService.root_path()}/{new_location}" + new_relative_path = f"{new_location}/{original_group_id}" + new_group_path = os.path.abspath( + os.path.join(FileSystemService.root_path(), new_root, original_group_id) + ) + destination = shutil.move(original_group_path, new_group_path) + new_process_group = self.get_process_group(destination) + print("process_group_move") + return new_process_group + def __get_all_nested_models(self, group_path: str) -> list: """__get_all_nested_models.""" all_nested_models = [] diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_process_api.py b/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_process_api.py index fbbf7deb..3279b9df 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_process_api.py +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_process_api.py @@ -2369,3 +2369,67 @@ class TestProcessApi(BaseTest): ) print("test_script_unit_test_run") + + def test_move_model(self): + ... + + def test_move_group( + self, + app: Flask, + client: FlaskClient, + with_db_and_bpmn_file_cleanup: None, + with_super_admin_user: UserModel, + ) -> None: + groups = [ + 'group_a', 'group_b', 'group_b/group_bb' + ] + # setup initial groups + for group in groups: + self.create_process_group( + client, + with_super_admin_user, + group, + display_name=group + ) + # make sure initial groups exist + for group in groups: + persisted = ProcessModelService().get_process_group(group) + assert persisted is not None + assert persisted.id == group + + # add sub group to `group_a` + sub_group_id = 'sub_group' + original_location = 'group_a' + original_sub_path = f"{original_location}/{sub_group_id}" + self.create_process_group( + client, + with_super_admin_user, + original_sub_path, + display_name=sub_group_id + ) + # make sure original subgroup exists + persisted = ProcessModelService().get_process_group(original_sub_path) + assert persisted is not None + assert persisted.id == original_sub_path + + # move sub_group to `group_b/group_bb` + new_location = 'group_b/group_bb' + new_sub_path = f"{new_location}/{sub_group_id}" + modified_original_process_group_id = original_sub_path.replace("/", ":") + response = client.put( + f"/v1.0/process-groups/{modified_original_process_group_id}/move?new_location={new_location}", + headers=self.logged_in_headers(with_super_admin_user), + ) + assert response.status_code == 201 + assert response.json['id'] == new_sub_path + + # make sure the original subgroup does not exist + with pytest.raises(ProcessEntityNotFoundError) as e: + ProcessModelService().get_process_group(original_sub_path) + + assert e.value.args[0] == 'process_group_not_found' + assert e.value.args[1] == f"Process Group Id: {original_sub_path}" + + # make sure the new subgroup does exist + new_process_group = ProcessModelService().get_process_group(new_sub_path) + assert new_process_group.id == new_sub_path