diff --git a/papermerge/core/cli/docs.py b/papermerge/core/cli/docs.py index 3778325cf..b193d9b46 100644 --- a/papermerge/core/cli/docs.py +++ b/papermerge/core/cli/docs.py @@ -20,11 +20,9 @@ def document_types(): @app.command(name="list-by-type") -def list_documents_by_type(type_id: uuid.UUID, parent_id: uuid.UUID): +def list_documents_by_type(type_id: uuid.UUID): """List all documents by specific document type""" - docs = get_docs_by_type( - session, type_id=type_id, user_id=uuid.uuid4(), ancestor_id=parent_id - ) + docs = get_docs_by_type(session, type_id=type_id, user_id=uuid.uuid4()) print_docs(docs) diff --git a/papermerge/core/db/doc.py b/papermerge/core/db/doc.py index 7abcdd409..bf97d3612 100644 --- a/papermerge/core/db/doc.py +++ b/papermerge/core/db/doc.py @@ -242,13 +242,10 @@ def update_doc_cfv( def get_docs_by_type( session: Session, type_id: UUID, - ancestor_id: UUID, user_id: UUID, ) -> list[schemas.DocumentCFV]: """ Returns list of documents + doc CFv for all documents with of given type - - All fetched documents are descendants of `ancestor_id` node. """ stmt = """ SELECT node.title, @@ -284,12 +281,10 @@ def get_docs_by_type( ) AS cf ON cf.cf_id = dtcf.custom_field_id LEFT OUTER JOIN custom_field_values AS cfv ON cfv.field_id = cf.cf_id AND cfv.document_id = doc_id - WHERE node.parent_id = :parent_id - AND doc.document_type_id = :document_type_id + WHERE doc.document_type_id = :document_type_id """ - str_parent_id = str(ancestor_id).replace("-", "") str_type_id = str(type_id).replace("-", "") - params = {"parent_id": str_parent_id, "document_type_id": str_type_id} + params = {"document_type_id": str_type_id} results = [] rows = session.execute(text(stmt), params) for document_id, group in itertools.groupby(rows, lambda r: r.doc_id): diff --git a/papermerge/core/routers/documents.py b/papermerge/core/routers/documents.py index a5f8c1eda..d7c1395bc 100644 --- a/papermerge/core/routers/documents.py +++ b/papermerge/core/routers/documents.py @@ -30,7 +30,6 @@ def get_documents_by_type( user: Annotated[ schemas.User, Security(get_current_user, scopes=[scopes.NODE_VIEW]) ], - ancestor_id: uuid.UUID, db_session: db.Session = Depends(db.get_session), ) -> list[schemas.DocumentCFV]: """ @@ -39,9 +38,7 @@ def get_documents_by_type( Required scope: `{scope}` """ - docs = db.get_docs_by_type( - db_session, type_id=document_type_id, ancestor_id=ancestor_id, user_id=user.id - ) + docs = db.get_docs_by_type(db_session, type_id=document_type_id, user_id=user.id) return docs diff --git a/tests/core/models/test_document.py b/tests/core/models/test_document.py index bbc72f2ae..5fdd46d4d 100644 --- a/tests/core/models/test_document.py +++ b/tests/core/models/test_document.py @@ -497,11 +497,10 @@ def test_get_docs_by_type_basic(db_session: Session, make_document_receipt): doc_1: Document = make_document_receipt(title="receipt_1.pdf") make_document_receipt(title="receipt_2.pdf") user_id = doc_1.user.id - parent_id = doc_1.parent.id type_id = doc_1.document_type.id items: list[schemas.DocumentCFV] = db.get_docs_by_type( - db_session, type_id=type_id, user_id=user_id, ancestor_id=parent_id + db_session, type_id=type_id, user_id=user_id ) assert len(items) == 2 @@ -527,7 +526,6 @@ def test_get_docs_by_type_one_doc_with_nonempty_cfv( doc_1: Document = make_document_receipt(title="receipt_1.pdf") make_document_receipt(title="receipt_2.pdf") user_id = doc_1.user.id - parent_id = doc_1.parent.id type_id = doc_1.document_type.id # update all CFV of receipt_1.pdf to non-empty values @@ -538,7 +536,7 @@ def test_get_docs_by_type_one_doc_with_nonempty_cfv( ) items: list[schemas.DocumentCFV] = db.get_docs_by_type( - db_session, type_id=type_id, user_id=user_id, ancestor_id=parent_id + db_session, type_id=type_id, user_id=user_id ) assert len(items) == 2 @@ -580,7 +578,6 @@ def test_get_docs_by_type_missmatching_type(db_session: Session, make_document_r doc_1: Document = make_document_receipt(title="receipt_1.pdf") make_document_receipt(title="receipt_2.pdf") user_id = doc_1.user.id - parent_id = doc_1.parent.id groceries_type_id = doc_1.document_type.id # to reproduce the bug bill document type should share at least one @@ -600,10 +597,9 @@ def test_get_docs_by_type_missmatching_type(db_session: Session, make_document_r db_session, type_id=billType.id, user_id=user_id, - ancestor_id=parent_id, ) groceriesDocs: list[schemas.DocumentCFV] = db.get_docs_by_type( - db_session, type_id=groceries_type_id, user_id=user_id, ancestor_id=parent_id + db_session, type_id=groceries_type_id, user_id=user_id ) # because there are no documents of type "Bill" diff --git a/ui2/src/features/document/apiSlice.ts b/ui2/src/features/document/apiSlice.ts index 1820f5255..fa8d02363 100644 --- a/ui2/src/features/document/apiSlice.ts +++ b/ui2/src/features/document/apiSlice.ts @@ -57,11 +57,6 @@ type UpdateDocumentCustomFields = { }> } -type GetDocsByTypeArgs = { - document_type_id: string - ancestor_id: string -} - type UpdateDocumentTypeArgs = { document_id?: string invalidatesTags: { @@ -199,12 +194,12 @@ export const apiSliceWithDocuments = apiSlice.injectEndpoints({ {type: "DocumentCustomField", id: arg} ] }), - getDocsByType: builder.query({ - query: args => ({ - url: `/documents/type/${args.document_type_id}?ancestor_id=${args.ancestor_id}` + getDocsByType: builder.query({ + query: document_type_id => ({ + url: `/documents/type/${document_type_id}` }), - providesTags: (_result, _error, arg) => [ - {type: "DocumentCFV", id: arg.document_type_id} + providesTags: (_result, _error, document_type_id) => [ + {type: "DocumentCFV", id: document_type_id} ] }) }) diff --git a/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentRow.tsx b/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentRow.tsx index df9a8fb8e..f6172412d 100644 --- a/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentRow.tsx +++ b/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentRow.tsx @@ -1,8 +1,7 @@ +import {useAppDispatch} from "@/app/hooks" +import {currentNodeChanged} from "@/features/ui/uiSlice" import {Checkbox, Table} from "@mantine/core" -import {useSelector} from "react-redux" -import {Link} from "react-router-dom" - -import {selectSelectedIds} from "@/features/users/usersSlice" +import {useNavigate} from "react-router-dom" import type {DocumentCFV} from "@/types" @@ -11,20 +10,33 @@ type Args = { } export default function DocumentRow({doc}: Args) { - const selectedIds = useSelector(selectSelectedIds) + const dispatch = useAppDispatch() + const navigate = useNavigate() + const customFieldsDataColumns = doc.custom_fields.map(cf => ( {cf[1]} )) - const onChange = () => {} + const onClick = (e: React.MouseEvent) => { + e.preventDefault() + if (e.ctrlKey) { + dispatch( + currentNodeChanged({id: doc.id, ctype: "document", panel: "secondary"}) + ) + } else { + navigate(`/document/${doc.id}`) + } + } return ( - + - {doc.title} + + {doc.title} + {customFieldsDataColumns} diff --git a/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentTypeFilter.tsx b/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentTypeFilter.tsx index 37a25c9ed..cfa89a90c 100644 --- a/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentTypeFilter.tsx +++ b/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentTypeFilter.tsx @@ -1,7 +1,10 @@ -import {useAppDispatch} from "@/app/hooks" +import {useAppDispatch, useAppSelector} from "@/app/hooks" import PanelContext from "@/contexts/PanelContext" import {useGetDocumentTypesQuery} from "@/features/document-types/apiSlice" -import {commanderDocumentTypeIDUpdated} from "@/features/ui/uiSlice" +import { + commanderDocumentTypeIDUpdated, + selectCommanderDocumentTypeID +} from "@/features/ui/uiSlice" import {Select} from "@mantine/core" import {useContext, useState} from "react" @@ -9,11 +12,14 @@ import type {PanelMode} from "@/types" export default function DocumentTypeFilter() { const mode: PanelMode = useContext(PanelContext) + const lastDocumentTypeID = useAppSelector(s => + selectCommanderDocumentTypeID(s, mode) + ) const dispatch = useAppDispatch() const {data: allDocumentTypes = []} = useGetDocumentTypesQuery() const [currentDocumentTypeID, setCurrentDocumentTypeID] = useState< string | undefined - >("") + >(lastDocumentTypeID) const onDocumentTypeChanged = (value: string | null) => { let newValue diff --git a/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentsByTypeCommander.tsx b/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentsByTypeCommander.tsx index 1f56b499a..81f3a0d7d 100644 --- a/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentsByTypeCommander.tsx +++ b/ui2/src/features/nodes/components/Commander/DocumentsByTypeCommander/DocumentsByTypeCommander.tsx @@ -1,13 +1,8 @@ import {useAppSelector} from "@/app/hooks" -import Breadcrumbs from "@/components/Breadcrumbs" import PanelContext from "@/contexts/PanelContext" import {useGetDocsByTypeQuery} from "@/features/document/apiSlice" -import {useGetFolderQuery} from "@/features/nodes/apiSlice" -import { - selectCommanderDocumentTypeID, - selectCurrentNodeID -} from "@/features/ui/uiSlice" -import type {NType, PanelMode} from "@/types" +import {selectCommanderDocumentTypeID} from "@/features/ui/uiSlice" +import type {PanelMode} from "@/types" import {Box, Checkbox, Stack, Table} from "@mantine/core" import {skipToken} from "@reduxjs/toolkit/query" import {useContext} from "react" @@ -16,29 +11,18 @@ import DocumentRow from "./DocumentRow" export default function DocumentsByCategoryCommander() { const mode: PanelMode = useContext(PanelContext) - const currentNodeID = useAppSelector(s => selectCurrentNodeID(s, mode)) const currentDocumentTypeID = useAppSelector(s => selectCommanderDocumentTypeID(s, mode) ) - const {data: currentFolder} = useGetFolderQuery(currentNodeID ?? skipToken) const {data: nodes} = useGetDocsByTypeQuery( - currentNodeID && currentDocumentTypeID - ? {document_type_id: currentDocumentTypeID, ancestor_id: currentNodeID} - : skipToken + currentDocumentTypeID ?? skipToken ) - const onClick = (_node: NType) => {} - if (!nodes || (nodes && nodes.length == 0)) { return ( - Empty @@ -54,14 +38,9 @@ export default function DocumentsByCategoryCommander() { - - +