From d50fb61bb9942c99451de1599e63cd01441b5ae0 Mon Sep 17 00:00:00 2001 From: jasquat <2487833+jasquat@users.noreply.github.com> Date: Tue, 27 Jun 2023 10:04:04 -0400 Subject: [PATCH] =?UTF-8?q?added=20a=20script=20task=20script=20to=20get?= =?UTF-8?q?=20a=20task=20data=20value=20based=20on=20key=20name=E2=80=A6?= =?UTF-8?q?=20(#353)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added a script task script to get a task data value based on key name w/ burnettk * pyl --------- Co-authored-by: jasquat --- .../scripts/get_task_data_value.py | 38 +++++++++++++++++++ .../scripts/test_get_task_data_value.py | 23 +++++++++++ 2 files changed, 61 insertions(+) create mode 100644 spiffworkflow-backend/src/spiffworkflow_backend/scripts/get_task_data_value.py create mode 100644 spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_task_data_value.py diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/get_task_data_value.py b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/get_task_data_value.py new file mode 100644 index 00000000..c68fbee9 --- /dev/null +++ b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/get_task_data_value.py @@ -0,0 +1,38 @@ +from typing import Any + +from spiffworkflow_backend.models.script_attributes_context import ScriptAttributesContext +from spiffworkflow_backend.scripts.script import Script + + +class GetTaskDataValue(Script): + @staticmethod + def requires_privileged_permissions() -> bool: + """We have deemed this function safe to run without elevated permissions.""" + return False + + def get_description(self) -> str: + return ( + "Checks to see if given value is in task data and returns its value. " + "If does not exist or is None, it returns the default value." + ) + + def run(self, script_attributes_context: ScriptAttributesContext, *args: Any, **kwargs: Any) -> Any: + variable_to_check = args[0] + default_value = args[1] + + task = script_attributes_context.task + if task is None: + return default_value + + task_data = task.data + if task_data is None: + return default_value + + if variable_to_check not in task_data.keys(): + return default_value + + value = task_data[variable_to_check] + if value is None: + return default_value + + return value diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_task_data_value.py b/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_task_data_value.py new file mode 100644 index 00000000..c5660f61 --- /dev/null +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_task_data_value.py @@ -0,0 +1,23 @@ +from flask import Flask +from spiffworkflow_backend.models.script_attributes_context import ScriptAttributesContext +from spiffworkflow_backend.scripts.get_task_data_value import GetTaskDataValue + +from tests.spiffworkflow_backend.helpers.base_test import BaseTest + + +class TestGetTaskDataValue(BaseTest): + def test_get_task_data_value_script( + self, + app: Flask, + with_db_and_bpmn_file_cleanup: None, + ) -> None: + process_model_identifier = "test_process_model" + process_instance_id = 1 + script_attributes_context = ScriptAttributesContext( + task=None, + environment_identifier="unit_testing", + process_instance_id=process_instance_id, + process_model_identifier=process_model_identifier, + ) + result = GetTaskDataValue().run(script_attributes_context, "the_var_name", "the_default_value") + assert result == "the_default_value"