✨ (folders.py, flow.py): add unique name generation for folders and flows
♻️ (folders.py): refactor folder name generation logic to use helper function ♻️ (flow.py): refactor flow name generation logic to use helper function
This commit is contained in:
parent
83a1264835
commit
016a7dc688
3 changed files with 50 additions and 9 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
23
src/backend/base/langflow/helpers/folders.py
Normal file
23
src/backend/base/langflow/helpers/folders.py
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue