Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Oct 10, 2024
1 parent f9cb7bb commit 3d917a3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
9 changes: 8 additions & 1 deletion papermerge/core/cli/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ def list_documents_by_type(type_id: uuid.UUID):


def print_docs(docs):
pass
table = Table(title="Users")

table.add_column("UUID", style="cyan", no_wrap=True)
table.add_column("Title", style="magenta")

for key, value in docs.items():
print("*********************************")
print(f"{value['doc_id']} {value['custom_fields']}")


def print_doc_types(doc_types: list[schemas.DocumentType]):
Expand Down
70 changes: 54 additions & 16 deletions papermerge/core/db/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from uuid import UUID

from sqlalchemy import select
from sqlalchemy import case, desc, select
from sqlalchemy.orm import Session, aliased

from papermerge.core import schemas
Expand All @@ -15,6 +15,7 @@
DocumentType,
DocumentTypeCustomField,
DocumentVersion,
Node,
Page,
)
from papermerge.core.exceptions import InvalidDateFormat
Expand Down Expand Up @@ -319,39 +320,76 @@ def get_document_custom_field_values(
return result


def get_subq(session: Session, type_id: UUID):
nd = aliased(Node)
cfv = aliased(CustomFieldValue)
cf = aliased(CustomField)
dtcf = aliased(DocumentTypeCustomField)
dt = aliased(DocumentType)
doc = aliased(Document)

subq = (
select(doc.id.label("doc_id"), doc.document_type_id.label("document_type_id"))
.select_from(cfv)
.join(cf, cf.id == cfv.field_id)
.join(dtcf, dtcf.custom_field_id == cf.id)
.join(dt, dt.id == dtcf.document_type_id)
.join(doc, doc.document_type_id == dt.id)
.where(
dt.id == type_id,
doc.id == cfv.document_id,
cf.name == "Total",
nd.parent_id == UUID("4fdcfbc9-64cb-46d3-bc7e-e1677eaecc70"),
)
.order_by(desc(cfv.value_monetary))
.subquery()
)

return subq


def get_documents_by_type(
session: Session,
type_id: UUID,
user_id: UUID,
):
subq = get_subq(session, type_id=type_id)
nd = aliased(Node)
cfv = aliased(CustomFieldValue)
cf = aliased(CustomField)
dtcf = aliased(DocumentTypeCustomField)
dt = aliased(DocumentType)
doc = aliased(Document)

stmt = (
select(
doc.id.label("doc_id"),
dt.name.label("document_type"),
dt.id.label("document_type_id"),
subq.c.doc_id.label("doc_id"),
nd.title,
cf.name.label("cf_name"),
cfv.value_text,
cfv.value_date,
cfv.value_monetary,
case(
(cf.data_type == "monetary", cfv.value_monetary),
(cf.data_type == "string", cfv.value_text),
(cf.data_type == "date", cfv.value_date),
(cf.data_type == "boolean", cfv.value_bool),
(cf.data_type == "url", cfv.value_url),
).label("cf_value"),
)
.select_from(cfv)
.join(cf, cf.id == cfv.field_id)
.join(dtcf, dtcf.custom_field_id == cf.id)
.join(dt, dt.id == dtcf.document_type_id)
.join(doc, doc.document_type_id == dt.id)
.where(
dt.id == type_id,
doc.id == cfv.document_id,
)
.join(dt, dtcf.document_type_id == dt.id)
.join(subq, subq.c.document_type_id == dt.id)
.where(subq.c.doc_id == cfv.document_id)
)

documents = {}
for row in session.execute(stmt):
print(row)
if row.doc_id not in documents.keys():
documents[row.doc_id] = {
"doc_id": row.doc_id,
"title": row.title,
"custom_fields": [],
}
else:
documents[row.doc_id]["custom_fields"].append((row.cf_name, row.cf_value))

return []
return documents

0 comments on commit 3d917a3

Please sign in to comment.