Skip to content

Commit

Permalink
no parent id when displ docs by category
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Oct 16, 2024
1 parent 8b9ae52 commit a9cc39a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 22 deletions.
6 changes: 2 additions & 4 deletions papermerge/core/cli/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
9 changes: 2 additions & 7 deletions papermerge/core/db/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 1 addition & 4 deletions papermerge/core/routers/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand All @@ -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

Expand Down
10 changes: 3 additions & 7 deletions tests/core/models/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit a9cc39a

Please sign in to comment.