From 45927a36060b35123162696a9e4855178fb893af Mon Sep 17 00:00:00 2001 From: Eugen Ciur Date: Tue, 10 Dec 2024 21:47:46 +0100 Subject: [PATCH] fixes for i3 worker --- papermerge/core/features/nodes/db/api.py | 31 +++++++++++++++++++ papermerge/core/features/nodes/router.py | 3 +- .../features/nodes/tests/test_nodes_router.py | 12 +++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/papermerge/core/features/nodes/db/api.py b/papermerge/core/features/nodes/db/api.py index 79bdade1d..95e4e668f 100644 --- a/papermerge/core/features/nodes/db/api.py +++ b/papermerge/core/features/nodes/db/api.py @@ -44,6 +44,37 @@ def str2colexpr(keys: list[str]): return result +def get_nodes( + db_session: Session, user_id: UUID, node_ids: list[UUID] | None = None +) -> list[schema.Document | schema.Folder]: + items = [] + if node_ids is None: + node_ids = [] + + if len(node_ids) > 0: + stmt = ( + select(orm.Node) + .options(selectinload(orm.Node.tags)) + .filter(orm.Node.id.in_(node_ids), orm.Node.user_id == user_id) + ) + else: + stmt = ( + select(orm.Node) + .options(selectinload(orm.Node.tags)) + .filter(orm.Node.user_id == user_id) + ) + + nodes = db_session.scalars(stmt).all() + + for node in nodes: + if node.ctype == "folder": + items.append(schema.Folder.model_validate(node)) + else: + items.append(schema.Document.model_validate(node)) + + return items + + def get_folder_by_id(db_session: Session, id: uuid.UUID) -> schema.Folder: stmt = select(Folder).where(Folder.id == id) db_folder = db_session.scalars(stmt).one_or_none() diff --git a/papermerge/core/features/nodes/router.py b/papermerge/core/features/nodes/router.py index 52c4ae583..96b8a8857 100644 --- a/papermerge/core/features/nodes/router.py +++ b/papermerge/core/features/nodes/router.py @@ -306,7 +306,8 @@ def get_nodes_details( if len(node_ids) == 0: return [] - nodes = db.get_nodes(db_session, node_ids) + with Session() as db_session: + nodes = nodes_dbapi.get_nodes(db_session, node_ids=node_ids, user_id=user.id) return nodes diff --git a/papermerge/core/features/nodes/tests/test_nodes_router.py b/papermerge/core/features/nodes/tests/test_nodes_router.py index e3f0696ee..dc5d4e30c 100644 --- a/papermerge/core/features/nodes/tests/test_nodes_router.py +++ b/papermerge/core/features/nodes/tests/test_nodes_router.py @@ -10,6 +10,18 @@ from papermerge.core.tests.types import AuthTestClient +def test_get_node_details(auth_api_client: AuthTestClient, make_document, db_session): + # Arrange + user = auth_api_client.user + doc = make_document(title="letter.pdf", user=user, parent=user.home_folder) + + # Act + response = auth_api_client.get(f"/nodes/?node_ids={doc.id}") + + # Assert + assert response.status_code == 200 + + def test_nodes_move_basic( auth_api_client: AuthTestClient, make_folder, make_document, db_session ):