mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-06 19:25:24 +00:00
feat(library): add multiple folders at a time
This commit is contained in:
parent
7212a2f65b
commit
b48f0f4a89
@ -10,7 +10,7 @@ from .schemas import (
|
||||
Plugin,
|
||||
NewPluginParam,
|
||||
UpdateEntityParam,
|
||||
NewFolderParam,
|
||||
NewFoldersParam,
|
||||
MetadataSource,
|
||||
EntityMetadataParam,
|
||||
)
|
||||
@ -65,12 +65,17 @@ def get_library_by_name(library_name: str, db: Session) -> Library | None:
|
||||
)
|
||||
|
||||
|
||||
def add_folder(library_id: int, folder: NewFolderParam, db: Session) -> Folder:
|
||||
db_folder = FolderModel(path=str(folder.path), library_id=library_id)
|
||||
db.add(db_folder)
|
||||
db.commit()
|
||||
db.refresh(db_folder)
|
||||
return Folder(id=db_folder.id, path=db_folder.path)
|
||||
def add_folders(library_id: int, folders: NewFoldersParam, db: Session) -> Library:
|
||||
db_folders = []
|
||||
for folder_path in folders.folders:
|
||||
db_folder = FolderModel(path=str(folder_path), library_id=library_id)
|
||||
db.add(db_folder)
|
||||
db.commit()
|
||||
db.refresh(db_folder)
|
||||
db_folders.append(Folder(id=db_folder.id, path=db_folder.path))
|
||||
|
||||
db_library = db.query(LibraryModel).filter(LibraryModel.id == library_id).first()
|
||||
return Library(**db_library.__dict__)
|
||||
|
||||
|
||||
def create_entity(library_id: int, entity: NewEntityParam, db: Session) -> Entity:
|
||||
|
@ -25,6 +25,10 @@ class NewFolderParam(BaseModel):
|
||||
path: DirectoryPath
|
||||
|
||||
|
||||
class NewFoldersParam(BaseModel):
|
||||
folders: List[DirectoryPath] = []
|
||||
|
||||
|
||||
class NewEntityParam(BaseModel):
|
||||
filename: str
|
||||
filepath: str
|
||||
|
@ -24,7 +24,7 @@ from .schemas import (
|
||||
Entity,
|
||||
Plugin,
|
||||
NewLibraryParam,
|
||||
NewFolderParam,
|
||||
NewFoldersParam,
|
||||
NewEntityParam,
|
||||
UpdateEntityParam,
|
||||
NewPluginParam,
|
||||
@ -130,10 +130,10 @@ def get_library_by_id(library_id: int, db: Session = Depends(get_db)):
|
||||
return library
|
||||
|
||||
|
||||
@app.post("/libraries/{library_id}/folders", response_model=Folder, tags=["library"])
|
||||
def new_folder(
|
||||
@app.post("/libraries/{library_id}/folders", response_model=Library, tags=["library"])
|
||||
def new_folders(
|
||||
library_id: int,
|
||||
folder: NewFolderParam,
|
||||
folders: NewFoldersParam,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
library = crud.get_library_by_id(library_id, db)
|
||||
@ -143,13 +143,13 @@ def new_folder(
|
||||
)
|
||||
|
||||
existing_folders = [folder.path for folder in library.folders]
|
||||
if str(folder.path) in existing_folders:
|
||||
if any(str(folder) in existing_folders for folder in folders.folders):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Folder already exists in the library",
|
||||
)
|
||||
|
||||
return crud.add_folder(library_id=library.id, folder=folder, db=db)
|
||||
return crud.add_folders(library_id=library.id, folders=folders, db=db)
|
||||
|
||||
|
||||
async def trigger_webhooks(library: Library, entity: Entity, request: Request, plugins: List[int] = None):
|
||||
|
@ -15,7 +15,7 @@ from memos.schemas import (
|
||||
NewLibraryParam,
|
||||
NewEntityParam,
|
||||
UpdateEntityParam,
|
||||
NewFolderParam,
|
||||
NewFoldersParam,
|
||||
EntityMetadataParam,
|
||||
MetadataType,
|
||||
UpdateEntityMetadataParam,
|
||||
@ -46,12 +46,12 @@ def setup_library_with_entity(client):
|
||||
library_id = library_response.json()["id"]
|
||||
|
||||
# Create a new folder in the library
|
||||
new_folder = NewFolderParam(path="/tmp")
|
||||
new_folder = NewFoldersParam(folders=["/tmp"])
|
||||
folder_response = client.post(
|
||||
f"/libraries/{library_id}/folders", json=new_folder.model_dump(mode="json")
|
||||
)
|
||||
assert folder_response.status_code == 200
|
||||
folder_id = folder_response.json()["id"]
|
||||
folder_id = folder_response.json()["folders"][0]["id"]
|
||||
|
||||
# Create a new entity in the folder
|
||||
new_entity = NewEntityParam(
|
||||
@ -290,11 +290,11 @@ def test_list_entities_in_folder(client):
|
||||
)
|
||||
library_id = library_response.json()["id"]
|
||||
|
||||
new_folder = NewFolderParam(path="/tmp")
|
||||
new_folder = NewFoldersParam(folders=["/tmp"])
|
||||
folder_response = client.post(
|
||||
f"/libraries/{library_id}/folders", json=new_folder.model_dump(mode="json")
|
||||
)
|
||||
folder_id = folder_response.json()["id"]
|
||||
folder_id = folder_response.json()["folders"][0]["id"]
|
||||
|
||||
# Create a new entity in the folder
|
||||
new_entity = NewEntityParam(
|
||||
@ -368,30 +368,30 @@ def test_add_folder_to_library(client):
|
||||
os.makedirs(tmp_folder_path)
|
||||
|
||||
# Create a new library
|
||||
new_library = NewLibraryParam(name="Test Library")
|
||||
new_library = NewLibraryParam(name="Test Library", folders=[])
|
||||
library_response = client.post(
|
||||
"/libraries", json=new_library.model_dump(mode="json")
|
||||
)
|
||||
library_id = library_response.json()["id"]
|
||||
|
||||
# Add a new folder to the library
|
||||
new_folder = NewFolderParam(path="/tmp/new_folder")
|
||||
new_folders = NewFoldersParam(folders=[tmp_folder_path])
|
||||
folder_response = client.post(
|
||||
f"/libraries/{library_id}/folders", json=new_folder.model_dump(mode="json")
|
||||
f"/libraries/{library_id}/folders", json=new_folders.model_dump(mode="json")
|
||||
)
|
||||
assert folder_response.status_code == 200
|
||||
assert folder_response.json()["path"] == "/tmp/new_folder"
|
||||
assert any(folder["path"] == tmp_folder_path for folder in folder_response.json()["folders"])
|
||||
|
||||
# Verify the folder is added
|
||||
library_response = client.get(f"/libraries/{library_id}")
|
||||
assert library_response.status_code == 200
|
||||
library_data = library_response.json()
|
||||
folder_paths = [folder["path"] for folder in library_data["folders"]]
|
||||
assert "/tmp/new_folder" in folder_paths
|
||||
assert tmp_folder_path in folder_paths
|
||||
|
||||
# Test for adding a folder that already exists
|
||||
duplicate_folder_response = client.post(
|
||||
f"/libraries/{library_id}/folders", json=new_folder.model_dump(mode="json")
|
||||
f"/libraries/{library_id}/folders", json=new_folders.model_dump(mode="json")
|
||||
)
|
||||
assert duplicate_folder_response.status_code == 400
|
||||
assert duplicate_folder_response.json() == {
|
||||
@ -400,7 +400,7 @@ def test_add_folder_to_library(client):
|
||||
|
||||
# Test for adding a folder to a non-existent library
|
||||
invalid_folder_response = client.post(
|
||||
f"/libraries/9999/folders", json=new_folder.model_dump(mode="json")
|
||||
f"/libraries/9999/folders", json=new_folders.model_dump(mode="json")
|
||||
)
|
||||
assert invalid_folder_response.status_code == 404
|
||||
assert invalid_folder_response.json() == {"detail": "Library not found"}
|
||||
|
Loading…
x
Reference in New Issue
Block a user