From 2bb3e09c0d573b73f33b220499caa1840e153df3 Mon Sep 17 00:00:00 2001 From: jasquat Date: Tue, 7 Mar 2023 09:05:06 -0500 Subject: [PATCH] fixed mypy issues --- .../models/script_attributes_context.py | 4 ++-- .../scripts/markdown_file_download_link.py | 7 +++++++ .../scripts/save_process_instance_metadata.py | 7 +++++++ .../src/spiffworkflow_backend/scripts/script.py | 8 ++++++++ .../services/process_instance_processor.py | 15 +++++++-------- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/models/script_attributes_context.py b/spiffworkflow-backend/src/spiffworkflow_backend/models/script_attributes_context.py index 9405c8b3..8be7f44e 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/models/script_attributes_context.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/models/script_attributes_context.py @@ -11,5 +11,5 @@ class ScriptAttributesContext: task: Optional[SpiffTask] environment_identifier: str - process_instance_id: int - process_model_identifier: str + process_instance_id: Optional[int] + process_model_identifier: Optional[str] diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/markdown_file_download_link.py b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/markdown_file_download_link.py index cb03ae24..de1c16c8 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/markdown_file_download_link.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/markdown_file_download_link.py @@ -7,6 +7,7 @@ from spiffworkflow_backend.models.process_model import ProcessModelInfo from spiffworkflow_backend.models.script_attributes_context import ( ScriptAttributesContext, ) +from spiffworkflow_backend.scripts.script import ProcessModelIdentifierMissingError from spiffworkflow_backend.scripts.script import Script @@ -36,6 +37,12 @@ class GetMarkdownFileDownloadLink(Script): digest = parts[2].split(",")[1][-64:] label = parts[1].split("=")[1] process_model_identifier = script_attributes_context.process_model_identifier + if process_model_identifier is None: + raise ProcessModelIdentifierMissingError( + "The process model identifier was not given to script" + " 'markdown_file_download_link'. This script needs to be run from" + " within the context of a process model." + ) modified_process_model_identifier = ( ProcessModelInfo.modify_process_identifier_for_path_param( process_model_identifier diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/save_process_instance_metadata.py b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/save_process_instance_metadata.py index 3d2054df..52bd7b38 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/save_process_instance_metadata.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/save_process_instance_metadata.py @@ -8,6 +8,7 @@ from spiffworkflow_backend.models.process_instance_metadata import ( from spiffworkflow_backend.models.script_attributes_context import ( ScriptAttributesContext, ) +from spiffworkflow_backend.scripts.script import ProcessInstanceIdMissingError from spiffworkflow_backend.scripts.script import Script @@ -26,6 +27,12 @@ class SaveProcessInstanceMetadata(Script): ) -> Any: """Run.""" metadata_dict = args[0] + if script_attributes_context.process_instance_id is None: + raise ProcessInstanceIdMissingError( + "The process instance id was not given to script" + " 'save_process_instance_metadata'. This script needs to be run from" + " within the context of a process instance." + ) for key, value in metadata_dict.items(): pim = ProcessInstanceMetadataModel.query.filter_by( process_instance_id=script_attributes_context.process_instance_id, diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/script.py b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/script.py index 897bc1be..337c67ee 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/script.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/script.py @@ -26,6 +26,14 @@ class ScriptUnauthorizedForUserError(Exception): """ScriptUnauthorizedForUserError.""" +class ProcessInstanceIdMissingError(Exception): + pass + + +class ProcessModelIdentifierMissingError(Exception): + pass + + class Script: """Provides an abstract class that defines how scripts should work, this must be extended in all Script Tasks.""" diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py index 6e1e43eb..923c3e7e 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py @@ -319,14 +319,13 @@ class CustomBpmnScriptEngine(PythonScriptEngine): # type: ignore def __get_augment_methods(self, task: Optional[SpiffTask]) -> Dict[str, Callable]: """__get_augment_methods.""" tld = current_app.config.get("THREAD_LOCAL_DATA") - if not hasattr(tld, "process_model_identifier"): - process_model_identifier = None - else: - process_model_identifier = tld.process_model_identifier - if not hasattr(tld, "process_instance_id"): - process_instance_id = None - else: - process_instance_id = tld.process_instance_id + process_model_identifier = None + process_instance_id = None + if tld: + if hasattr(tld, "process_model_identifier"): + process_model_identifier = tld.process_model_identifier + if hasattr(tld, "process_instance_id"): + process_instance_id = tld.process_instance_id script_attributes_context = ScriptAttributesContext( task=task, environment_identifier=current_app.config["ENV_IDENTIFIER"],