refactor: Ensure flow names are unique by appending a number if necessary
This commit is contained in:
parent
4365e3d2d8
commit
b05455a9b8
1 changed files with 9 additions and 18 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
import re
|
||||||
from typing import List
|
from typing import List
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
|
|
@ -46,8 +47,14 @@ def create_flow(
|
||||||
select(Flow).where(Flow.name.like(f"{flow.name} (%")).where(Flow.user_id == current_user.id) # type: ignore
|
select(Flow).where(Flow.name.like(f"{flow.name} (%")).where(Flow.user_id == current_user.id) # type: ignore
|
||||||
).all()
|
).all()
|
||||||
if flows:
|
if flows:
|
||||||
numbers = [int(flow.name.split("(")[1].split(")")[0]) for flow in flows]
|
extract_number = re.compile(r"\((\d+)\)$")
|
||||||
flow.name = f"{flow.name} ({max(numbers) + 1})"
|
numbers = []
|
||||||
|
for flow in flows:
|
||||||
|
result = extract_number.search(flow.name)
|
||||||
|
if result:
|
||||||
|
numbers.append(int(result.groups(1)[0]))
|
||||||
|
if numbers:
|
||||||
|
flow.name = f"{flow.name} ({max(numbers) + 1})"
|
||||||
else:
|
else:
|
||||||
flow.name = f"{flow.name} (1)"
|
flow.name = f"{flow.name} (1)"
|
||||||
# Now check if the endpoint is unique
|
# Now check if the endpoint is unique
|
||||||
|
|
@ -210,22 +217,6 @@ def update_flow(
|
||||||
db_flow.webhook = webhook_component is not None
|
db_flow.webhook = webhook_component is not None
|
||||||
db_flow.updated_at = datetime.now(timezone.utc)
|
db_flow.updated_at = datetime.now(timezone.utc)
|
||||||
|
|
||||||
# First check if the flow.name is unique
|
|
||||||
# there might be flows with name like: "MyFlow", "MyFlow (1)", "MyFlow (2)"
|
|
||||||
# so we need to check if the name is unique with `like` operator
|
|
||||||
# if we find a flow with the same name, we add a number to the end of the name
|
|
||||||
# based on the highest number found
|
|
||||||
flow_from_db = session.exec(select(Flow).where(Flow.id == flow_id, Flow.user_id == current_user.id)).first()
|
|
||||||
if flow_from_db:
|
|
||||||
flows = session.exec(
|
|
||||||
select(Flow).where(Flow.name.like(f"{flow.name} (%")).where(Flow.user_id == current_user.id) # type: ignore
|
|
||||||
).all()
|
|
||||||
if flows:
|
|
||||||
numbers = [int(flow.name.split("(")[1].split(")")[0]) for flow in flows]
|
|
||||||
flow.name = f"{flow.name} ({max(numbers) + 1})"
|
|
||||||
else:
|
|
||||||
flow.name = f"{flow.name} (1)"
|
|
||||||
|
|
||||||
if db_flow.folder_id is None:
|
if db_flow.folder_id is None:
|
||||||
default_folder = session.exec(select(Folder).where(Folder.name == DEFAULT_FOLDER_NAME)).first()
|
default_folder = session.exec(select(Folder).where(Folder.name == DEFAULT_FOLDER_NAME)).first()
|
||||||
if default_folder:
|
if default_folder:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue