From aa5582833c82361fb8b897945c01a1ecdd564f97 Mon Sep 17 00:00:00 2001 From: jkmin3 Date: Mon, 1 Apr 2024 20:17:59 -0500 Subject: [PATCH] better handling for lists in inputs --- ai_ta_backend/flows.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/ai_ta_backend/flows.py b/ai_ta_backend/flows.py index ee0e2d09..4c79956f 100644 --- a/ai_ta_backend/flows.py +++ b/ai_ta_backend/flows.py @@ -37,6 +37,7 @@ def get_users(self, limit: int = 50, pagination: bool = True, api_key: str = "") return all_users def execute_flow(self, hook: str, data=None) -> None: + print("Executing flow") if not data: data = {'field-0': ''} url = hook @@ -67,7 +68,6 @@ def get_executions(self, limit, id=None, pagination: bool = True, api_key: str = cursor = executions.get('nextCursor') if id: for execution in all_executions: - print(execution[0]['id'], ":ID:") if execution[0]['id'] == id: return execution @@ -130,24 +130,21 @@ def format_data(self, inputted, api_key: str, workflow_name): print("Got workflow") values = [] if isinstance(work_flow, dict) and 'nodes' in work_flow: - print("passed if") for node in work_flow['nodes']: if node['name'] == 'n8n Form Trigger': values = node['parameters']['formFields']['values'] - print("found value") data = {} - print(inputted) inputted = json.loads(inputted) - print("Done with json") inputted = dict(inputted) - print("Got data") for i, value in enumerate(values): field_name = 'field-' + str(i) data[value['fieldLabel']] = field_name new_data = {} for k, v in inputted.items(): - new_data[data[k]] = v - print("Done with formatting") + if type(v) == list: + new_data[data[k]] = json.dumps(v) + else: + new_data[data[k]] = v return new_data # TODO: activate and disactivate workflows @@ -210,14 +207,20 @@ def main_flow(self, name: str, api_key: str = "", data: str = ""): try: start_time = time.monotonic() + print("Inserting") self.supabase_client.table('n8n_workflows').insert({"latest_workflow_id": id, "is_locked": True}).execute() + print("inserted") self.execute_flow(hook, new_data) print("Executed") print(f"⏰ Runtime to execute_flow(): {(time.monotonic() - start_time):.4f} seconds") - except: + except Exception as e: + # TODO: Decrease number by one, is locked false + # self.supabase_client.table('n8n_workflows').update({"latest_workflow_id": str(int(id) - 1), "is_locked": False}).eq('latest_workflow_id', id).execute() self.supabase_client.table('n8n_workflows').delete().eq('latest_workflow_id', id).execute() + return {"error": str(e)} finally: # TODO: Remove lock from Supabase table. + print("id: ", id) self.supabase_client.table('n8n_workflows').update({"is_locked": False}).eq('latest_workflow_id', id).execute() try: