Skip to content

Commit

Permalink
it works!
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Oct 17, 2024
1 parent b784a41 commit 8514f43
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 15 deletions.
4 changes: 4 additions & 0 deletions papermerge/core/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
get_doc,
get_doc_cfv,
get_docs_by_type,
get_docs_count_by_type,
update_doc_cfv,
update_doc_type,
)
from .doc_ver import get_doc_ver, get_last_doc_ver
from .document_types import (
create_document_type,
delete_document_type,
document_type_cf_count,
get_document_type,
get_document_types,
update_document_type,
Expand Down Expand Up @@ -82,4 +84,6 @@
"get_doc_cfv",
"update_doc_type",
"get_docs_by_type",
"document_type_cf_count",
"get_docs_count_by_type",
]
22 changes: 20 additions & 2 deletions papermerge/core/db/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Optional
from uuid import UUID

from sqlalchemy import delete, insert, select, text, update
from sqlalchemy import delete, func, insert, select, text, update
from sqlalchemy.orm import Session

from papermerge.core import schemas
Expand All @@ -21,6 +21,7 @@
from papermerge.core.types import OrderEnum

from .common import get_ancestors
from .document_types import document_type_cf_count


def str2date(value: str | None) -> Optional[datetime.date]:
Expand Down Expand Up @@ -240,6 +241,17 @@ def update_doc_cfv(
return items


def get_docs_count_by_type(session: Session, type_id: UUID):
"""Returns number of documents of specific document type"""
stmt = (
select(func.count())
.select_from(Document)
.where(Document.document_type_id == type_id)
)

return session.scalars(stmt).one()


STMT_WITH_ORDER_BY = """
SELECT node.title,
doc.basetreenode_ptr_id AS doc_id,
Expand Down Expand Up @@ -346,9 +358,15 @@ def get_docs_by_type(
"""
Returns list of documents + doc CFv for all documents with of given type
"""
if page_number < 1:
raise ValueError(f"page_number must be >= 1; got value={page_number}")

if page_size < 1:
raise ValueError(f"page_size must be >= 1; got value={page_size}")

str_type_id = str(type_id).replace("-", "")
results = []
cf_count = 3
cf_count = document_type_cf_count(session, document_type_id=type_id)

if order_by is None:
stmt = STMT + PAGINATION.format(
Expand Down
16 changes: 14 additions & 2 deletions papermerge/core/db/document_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ def get_document_types(session: Session) -> list[schemas.DocumentType]:
return result


def document_type_cf_count(session: Session, document_type_id: uuid.UUID):
"""count number of custom fields associated to document type"""
stmt = select(models.DocumentType).where(models.DocumentType.id == document_type_id)
dtype = session.scalars(stmt).one()
return len(dtype.custom_fields)


def create_document_type(
session: Session,
name: str,
custom_field_ids: list[uuid.UUID],
user_id: uuid.UUID,
custom_field_ids: list[uuid.UUID] | None = None,
) -> schemas.DocumentType:
stmt = select(models.CustomField).where(models.CustomField.id.in_(custom_field_ids))
if custom_field_ids is None:
cf_ids = []
else:
cf_ids = custom_field_ids

stmt = select(models.CustomField).where(models.CustomField.id.in_(cf_ids))
custom_fields = session.execute(stmt).scalars().all()
dtype = models.DocumentType(
id=uuid.uuid4(),
Expand Down
14 changes: 10 additions & 4 deletions papermerge/core/routers/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from papermerge.core import db, schemas, utils
from papermerge.core.auth import get_current_user, scopes
from papermerge.core.models import Document
from papermerge.core.types import OrderEnum
from papermerge.core.types import OrderEnum, PaginatedResponse

OrderBy = Annotated[
str | None,
Expand Down Expand Up @@ -52,14 +52,14 @@ def get_documents_by_type(
db_session: db.Session = Depends(db.get_session),
order_by: OrderBy = None,
order: OrderEnum = OrderEnum.desc,
) -> list[schemas.DocumentCFV]:
) -> PaginatedResponse[schemas.DocumentCFV]:
"""
Get all documents of specific type with all custom field values
Required scope: `{scope}`
"""

docs = db.get_docs_by_type(
items = db.get_docs_by_type(
db_session,
type_id=document_type_id,
user_id=user.id,
Expand All @@ -68,8 +68,14 @@ def get_documents_by_type(
page_number=page_number,
page_size=page_size,
)
total_count = db.get_docs_count_by_type(db_session, type_id=document_type_id)

return docs
return PaginatedResponse(
page_size=page_size,
page_number=page_number,
num_pages=int(total_count / page_size) + 1,
items=items,
)


@router.get("/{document_id}")
Expand Down
74 changes: 74 additions & 0 deletions tests/core/models/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,80 @@ def test_get_docs_by_type_order_by_cfv(db_session: Session, make_document_receip
]


@pytest.mark.django_db(transaction=True)
def test_document_type_cf_count_1(db_session: Session, user: User, make_custom_field):
cf1 = make_custom_field(
name="some-random-cf1", type=schemas.CustomFieldType.boolean
)
cf2 = make_custom_field(
name="some-random-cf2", type=schemas.CustomFieldType.boolean
)

dtype1 = db.create_document_type(
db_session,
name="document_type_1",
custom_field_ids=[cf1.id, cf2.id],
user_id=user.id,
)

assert db.document_type_cf_count(db_session, document_type_id=dtype1.id) == 2


@pytest.mark.django_db(transaction=True)
def test_document_type_cf_count_without_cf(db_session: Session, user: User):
# document type does not have any custom field associated
dtype1 = db.create_document_type(
db_session,
name="document_type_1",
user_id=user.id,
)

actual_cf_count = db.document_type_cf_count(db_session, document_type_id=dtype1.id)
assert actual_cf_count == 0


@pytest.mark.django_db(transaction=True)
def test_get_docs_count_by_type(db_session, user: User):
"""
1. Create document type which does not have any document associated
2. Expected result should be that number of the documents in this
type is 0
"""
dtype1 = db.create_document_type(
db_session,
name="document_type_1",
user_id=user.id,
)
docs_count = db.get_docs_count_by_type(db_session, type_id=dtype1.id)
# there are no documents of type "document_type_1" yet
assert docs_count == 0


@pytest.mark.django_db(transaction=True)
def test_get_docs_count_by_type_with_two_document(db_session, user: User):
"""
Create document type which has two documents associated
and validate that `db.get_docs_count_by_type` returns 2
"""
DOCS_COUNT = 2
dtype = db.create_document_type(
db_session,
name="document_type_1",
user_id=user.id,
)
for idx in range(0, DOCS_COUNT):
document_recipe.make(
user=user,
title=f"Document {idx}",
parent=user.home_folder,
document_type_id=dtype.id,
)

docs_count = db.get_docs_count_by_type(db_session, type_id=dtype.id)
# there are two documents in this category
assert docs_count == DOCS_COUNT


def test_str2date():
assert str2date("2024-10-30") == datetime(2024, 10, 30).date()
assert str2date("2024-10-30 00:00:00") == datetime(2024, 10, 30).date()
4 changes: 2 additions & 2 deletions ui2/src/features/document/apiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PAGINATION_DEFAULT_ITEMS_PER_PAGES
} from "@/cconstants"
import {apiSlice} from "@/features/api/slice"
import type {DocumentCFV} from "@/types"
import type {DocumentCFV, Paginated} from "@/types"
import {
CFV,
DocumentType,
Expand Down Expand Up @@ -206,7 +206,7 @@ export const apiSliceWithDocuments = apiSlice.injectEndpoints({
{type: "DocumentCustomField", id: arg}
]
}),
getDocsByType: builder.query<DocumentCFV[], GetDocsByTypeArgs>({
getDocsByType: builder.query<Paginated<DocumentCFV>, GetDocsByTypeArgs>({
query: ({
document_type_id,
page_number = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function DocumentsByCategoryCommander() {
const currentDocumentTypeID = useAppSelector(s =>
selectCommanderDocumentTypeID(s, mode)
)
const {data: nodes} = useGetDocsByTypeQuery(
const {data} = useGetDocsByTypeQuery(
currentDocumentTypeID
? {
document_type_id: currentDocumentTypeID,
Expand Down Expand Up @@ -72,7 +72,7 @@ export default function DocumentsByCategoryCommander() {
}
}

if (!nodes || (nodes && nodes.length == 0)) {
if (!data || (data && data.items.length == 0)) {
return (
<Box>
<Stack>
Expand All @@ -83,8 +83,8 @@ export default function DocumentsByCategoryCommander() {
)
}

const rows = nodes.map(n => <DocumentRow key={n.id} doc={n} />)
const customFieldsHeaderColumns = nodes[0].custom_fields.map(cf => (
const rows = data.items.map(n => <DocumentRow key={n.id} doc={n} />)
const customFieldsHeaderColumns = data.items[0].custom_fields.map(cf => (
<Th
sorted={orderBy === cf[0]}
reversed={reverseOrderDirection}
Expand Down Expand Up @@ -117,7 +117,7 @@ export default function DocumentsByCategoryCommander() {
pagination={{
pageNumber: page,
pageSize: pageSize,
numPages: 3
numPages: data.num_pages
}}
onPageNumberChange={onPageNumberChange}
onPageSizeChange={onPageSizeChange}
Expand Down

0 comments on commit 8514f43

Please sign in to comment.