Bugfix: Flows losing reference of folder when renaming current folder (#2269)

* 🐛 (folders.py): fix folder name update logic to ensure name changes are saved correctly

*  (folders.py): add custom_sort function to prioritize "My Projects" folder
♻️ (folders.py): refactor read_folders to return sorted folders using custom_sort
🐛 (folders.py): fix update_folder to refresh session after updating folder name
🐛 (folders.py): correct update statement to exclude flows not in the provided list

* Apply Ruff formatting

* ♻️ (folders.py): remove custom_sort function and replace with inline lambda for sorting folders
🐛 (folders.py): fix update statement to correctly update flows with excluded folder IDs
♻️ (folders.py): refactor folder update logic to use session.add for better clarity and consistency

---------

Co-authored-by: Cristhianzl <Cristhianzl@users.noreply.github.com>
This commit is contained in:
Cristhian Zanforlin Lousa 2024-06-23 17:51:10 -03:00 committed by GitHub
commit 00ec361e7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View file

@ -90,7 +90,8 @@ def read_folders(
or_(Folder.user_id == current_user.id, Folder.user_id == None) # type: ignore # noqa: E711
)
).all()
return folders
sorted_folders = sorted(folders, key=lambda x: x.name != DEFAULT_FOLDER_NAME)
return sorted_folders
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@ -127,7 +128,14 @@ def update_folder(
).first()
if not existing_folder:
raise HTTPException(status_code=404, detail="Folder not found")
folder_data = folder.model_dump(exclude_unset=True)
if folder.name and folder.name != existing_folder.name:
existing_folder.name = folder.name
session.add(existing_folder)
session.commit()
session.refresh(existing_folder)
return existing_folder
folder_data = existing_folder.model_dump(exclude_unset=True)
for key, value in folder_data.items():
if key != "components" and key != "flows":
setattr(existing_folder, key, value)

View file

@ -1,3 +1,4 @@
from langflow.services.database.models.folder.constants import DEFAULT_FOLDER_NAME
from langflow.services.database.models.folder.model import Folder
from sqlalchemy import select