diff --git a/src/writer/workflows_blocks/__init__.py b/src/writer/workflows_blocks/__init__.py index d72490c96..ae50ce05d 100644 --- a/src/writer/workflows_blocks/__init__.py +++ b/src/writer/workflows_blocks/__init__.py @@ -2,9 +2,10 @@ from writer.workflows_blocks.setstate import SetState from writer.workflows_blocks.writerclassification import WriterClassification from writer.workflows_blocks.writercompletion import WriterCompletion - +from writer.workflows_blocks.runworkflow import RunWorkflow SetState.register("workflows_setstate") WriterClassification.register("workflows_writerclassification") WriterCompletion.register("workflows_writercompletion") -HTTPRequest.register("workflows_httprequest") \ No newline at end of file +HTTPRequest.register("workflows_httprequest") +RunWorkflow.register("workflows_runworkflow") \ No newline at end of file diff --git a/src/writer/workflows_blocks/blocks.py b/src/writer/workflows_blocks/blocks.py index 7acf53000..a596513ec 100644 --- a/src/writer/workflows_blocks/blocks.py +++ b/src/writer/workflows_blocks/blocks.py @@ -1,9 +1,7 @@ -from typing import TYPE_CHECKING +from typing import Dict import writer.workflows_blocks import writer.core - -if TYPE_CHECKING: - from writer.core_ui import Component +import writer.core_ui block_map = {} @@ -13,21 +11,18 @@ class WorkflowBlock: def register(cls, type: str): block_map[type] = cls - def __init__(self, component: "Component", execution: dict, session: "writer.core.WriterSession", result: dict): + def __init__(self, component: "writer.core_ui.Component", execution: Dict, session: "writer.core.WriterSession", execution_env: Dict): self.outcome = None self.component = component self.execution = execution self.session = session - self.result = result + self.execution_env = execution_env + self.result = None self.evaluator = writer.core.Evaluator(session.session_state, session.session_component_tree) self.instance_path = [{"componentId": self.component.id, "instanceNumber": 0}] def _get_field(self, field_key: str, as_json=False): - base_context = { - "result": self.result - } - - v = self.evaluator.evaluate_field(self.instance_path, field_key, base_context=base_context, as_json=as_json) + v = self.evaluator.evaluate_field(self.instance_path, field_key, base_context=self.execution_env, as_json=as_json) return v diff --git a/src/writer/workflows_blocks/foreach.py b/src/writer/workflows_blocks/foreach.py new file mode 100644 index 000000000..162bf9da6 --- /dev/null +++ b/src/writer/workflows_blocks/foreach.py @@ -0,0 +1,64 @@ +from writer.abstract import register_abstract_template +from writer.ss_types import AbstractTemplate +from writer.workflows_blocks.blocks import WorkflowBlock +import writer.workflows + +class ForEach(WorkflowBlock): + + @classmethod + def register(cls, type: str): + super(ForEach, cls).register(type) + register_abstract_template(type, AbstractTemplate( + baseType="workflows_node", + writer={ + "name": "For-each loop", + "description": "Executes a workflow repeatedly, based on the items provided.", + "category": "Content", + "fields": { + "workflowKey": { + "name": "Workflow key", + "desc": "The workflow which will be executed for each item.", + "type": "Text", + }, + "items": { + "name": "Items", + "desc": "The item value will be passed in the execution context and will be available via @{item}.", + "default": "{}", + "type": "Object", + "control": "Textarea" + }, + "context": { + "name": "Context", + "desc": "You can add other values to the execution context.", + "default": "{}", + "type": "Object", + "control": "Textarea" + }, + }, + "outs": { + "success": { + "name": "Success", + "description": "The workflow wasn't executed successfully.", + "style": "success", + }, + "error": { + "name": "Error", + "description": "The workflow wasn't executed successfully.", + "style": "error", + }, + }, + } + )) + + def run(self): + workflow_key = self._get_field("workflowKey") + items = self._get_field("items") + context = self._get_field("context") + + try: + for item in items: + writer.workflows.run_workflow_by_key(self.session, workflow_key) + self.outcome = "success" + except Exception as e: + self.result = "HTTP call failed." + self.outcome = "connectionError" \ No newline at end of file diff --git a/src/writer/workflows_blocks/httprequest.py b/src/writer/workflows_blocks/httprequest.py index 7a5919972..0ee6f1a26 100644 --- a/src/writer/workflows_blocks/httprequest.py +++ b/src/writer/workflows_blocks/httprequest.py @@ -14,7 +14,6 @@ def register(cls, type: str): "name": "HTTP Request", "description": "Executes an HTTP request", "category": "Content", - "allowedParentTypes": ["workflows_workflow"], "fields": { "method": { "name": "Method", diff --git a/src/writer/workflows_blocks/runworkflow.py b/src/writer/workflows_blocks/runworkflow.py new file mode 100644 index 000000000..4724f9e44 --- /dev/null +++ b/src/writer/workflows_blocks/runworkflow.py @@ -0,0 +1,55 @@ +from writer.abstract import register_abstract_template +from writer.ss_types import AbstractTemplate +from writer.workflows_blocks.blocks import WorkflowBlock +import writer.workflows + +class RunWorkflow(WorkflowBlock): + + @classmethod + def register(cls, type: str): + super(RunWorkflow, cls).register(type) + register_abstract_template(type, AbstractTemplate( + baseType="workflows_node", + writer={ + "name": "Run workflow", + "description": "Executes a workflow", + "category": "Content", + "fields": { + "workflowKey": { + "name": "Workflow key", + "type": "Text", + }, + "context": { + "name": "Context", + "desc": "Values passed in the context will be available using the template syntax i.e. @{my_context_var}", + "default": "{}", + "type": "Object", + "control": "Textarea" + }, + }, + "outs": { + "success": { + "name": "Success", + "description": "The request was successful.", + "style": "success", + }, + "error": { + "name": "Error", + "description": "The workflow was executed successfully.", + "style": "error", + }, + }, + } + )) + + def run(self): + workflow_key = self._get_field("workflowKey") + context = self._get_field("context", as_json=True) + + try: + writer.workflows.run_workflow_by_key(self.session, workflow_key, context) + self.outcome = "success" + except Exception as e: + print("running the other workflow " + repr(e)) + self.result = "Running workflow failed." + self.outcome = "error" \ No newline at end of file diff --git a/src/writer/workflows_blocks/setstate.py b/src/writer/workflows_blocks/setstate.py index 4b4c0f98d..4d01b18f2 100644 --- a/src/writer/workflows_blocks/setstate.py +++ b/src/writer/workflows_blocks/setstate.py @@ -13,7 +13,6 @@ def register(cls, type: str): "name": "Set state", "description": "Set the value for a state element", "category": "Content", - "allowedParentTypes": ["workflows_workflow"], "fields": { "element": { "name": "State element", diff --git a/src/writer/workflows_blocks/writerclassification.py b/src/writer/workflows_blocks/writerclassification.py index 2de1fbb86..db9d52e28 100644 --- a/src/writer/workflows_blocks/writerclassification.py +++ b/src/writer/workflows_blocks/writerclassification.py @@ -15,7 +15,6 @@ def register(cls, type: str): "name": "Writer Classification", "description": "Classify a text.", "category": "Content", - "allowedParentTypes": ["workflows_workflow"], "fields": { "text": { "name": "Text", diff --git a/src/writer/workflows_blocks/writercompletion.py b/src/writer/workflows_blocks/writercompletion.py index 46bce986f..e810aedc1 100644 --- a/src/writer/workflows_blocks/writercompletion.py +++ b/src/writer/workflows_blocks/writercompletion.py @@ -13,7 +13,6 @@ def register(cls, type: str): "name": "Writer Completion", "description": "Set the value for a state element", "category": "Content", - "allowedParentTypes": ["workflows_workflow"], "fields": { "prompt": { "name": "Prompt",