From 016a7dc6884e2af605bfabb4ca570627aa875ff5 Mon Sep 17 00:00:00 2001 From: cristhianzl Date: Fri, 7 Jun 2024 11:24:40 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(folders.py,=20flow.py):=20add=20un?= =?UTF-8?q?ique=20name=20generation=20for=20folders=20and=20flows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ♻️ (folders.py): refactor folder name generation logic to use helper function ♻️ (flow.py): refactor flow name generation logic to use helper function --- src/backend/base/langflow/api/v1/folders.py | 15 +++++-------- src/backend/base/langflow/helpers/flow.py | 21 ++++++++++++++++++ src/backend/base/langflow/helpers/folders.py | 23 ++++++++++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 src/backend/base/langflow/helpers/folders.py diff --git a/src/backend/base/langflow/api/v1/folders.py b/src/backend/base/langflow/api/v1/folders.py index 7402881c7..d55f9bd15 100644 --- a/src/backend/base/langflow/api/v1/folders.py +++ b/src/backend/base/langflow/api/v1/folders.py @@ -1,5 +1,7 @@ from typing import List +from langflow.helpers.flow import generate_unique_flow_name +from langflow.helpers.folders import generate_unique_folder_name import orjson from fastapi import APIRouter, Depends, File, HTTPException, Response, UploadFile, status from sqlalchemy import or_, update @@ -203,16 +205,9 @@ async def upload_file( if not data: raise HTTPException(status_code=400, detail="No flows found in the file") - folder_results = session.exec( - select(Folder).where( - Folder.name == data["folder_name"], - Folder.user_id == current_user.id, - ) - ) - existing_folder_names = [folder.name for folder in folder_results] + folder_name = generate_unique_folder_name(data["folder_name"], current_user.id, session) - if existing_folder_names: - data["folder_name"] = f"{data['folder_name']} ({len(existing_folder_names) + 1})" + data["folder_name"] = folder_name folder = FolderCreate(name=data["folder_name"], description=data["folder_description"]) @@ -232,6 +227,8 @@ async def upload_file( raise HTTPException(status_code=400, detail="No flows found in the data") # Now we set the user_id for all flows for flow in flow_list.flows: + flow_name = generate_unique_flow_name(flow.name, current_user.id, session) + flow.name = flow_name flow.user_id = current_user.id flow.folder_id = new_folder.id diff --git a/src/backend/base/langflow/helpers/flow.py b/src/backend/base/langflow/helpers/flow.py index 7eb901274..6ce3b2048 100644 --- a/src/backend/base/langflow/helpers/flow.py +++ b/src/backend/base/langflow/helpers/flow.py @@ -259,3 +259,24 @@ def get_flow_by_id_or_endpoint_name( raise HTTPException(status_code=404, detail=f"Flow identifier {flow_id_or_name} not found") return flow + + +def generate_unique_flow_name(flow_name, user_id, session): + original_name = flow_name + n = 1 + while True: + # Check if a flow with the given name exists + existing_flow = session.exec( + select(Flow).where( + Flow.name == flow_name, + Flow.user_id == user_id, + ) + ).first() + + # If no flow with the given name exists, return the name + if not existing_flow: + return flow_name + + # If a flow with the name already exists, append (n) to the name and increment n + flow_name = f"{original_name} ({n})" + n += 1 \ No newline at end of file diff --git a/src/backend/base/langflow/helpers/folders.py b/src/backend/base/langflow/helpers/folders.py new file mode 100644 index 000000000..fa6f27fcc --- /dev/null +++ b/src/backend/base/langflow/helpers/folders.py @@ -0,0 +1,23 @@ +from langflow.services.database.models.folder.model import Folder +from sqlalchemy import select + + +def generate_unique_folder_name(folder_name, user_id, session): + original_name = folder_name + n = 1 + while True: + # Check if a folder with the given name exists + existing_folder = session.exec( + select(Folder).where( + Folder.name == folder_name, + Folder.user_id == user_id, + ) + ).first() + + # If no folder with the given name exists, return the name + if not existing_folder: + return folder_name + + # If a folder with the name already exists, append (n) to the name and increment n + folder_name = f"{original_name} ({n})" + n += 1 \ No newline at end of file