Skip to content

Commit

Permalink
custom fields crud
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Oct 14, 2024
1 parent b4eb4bd commit 0883457
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
18 changes: 10 additions & 8 deletions papermerge/core/cli/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ def document_types():


@app.command(name="list-by-type")
def list_documents_by_type(type_id: uuid.UUID):
def list_documents_by_type(type_id: uuid.UUID, parent_id: uuid.UUID):
"""List all documents by specific document type"""
docs = get_documents_by_type(session, type_id=type_id, user_id=uuid.uuid4())
docs = get_documents_by_type(
session, type_id=type_id, user_id=uuid.uuid4(), parent_id=parent_id
)
print_docs(docs)


Expand Down Expand Up @@ -76,27 +78,27 @@ def update_doc_custom_fields(doc_id: uuid.UUID, custom_fields: list[str]):
db.update_document_custom_fields(session, document_id=doc_id, custom_fields=cf)


def print_docs(docs):
def print_docs(docs: list[schemas.DocumentCFV]):
if len(docs) == 0:
print("No entries")
return

table = Table(title="Documents (with custom fields)")
table.add_column("ID", style="cyan", no_wrap=True)
table.add_column("Title", style="magenta")
first_item = list(docs.items())[0]
first_item = list(docs)[0]

for cf in first_item[1]["custom_fields"]:
for cf in first_item.custom_fields:
table.add_column(cf[0])

for key, doc in docs.items():
for doc in docs:
cf_list = []
for label, value in doc["custom_fields"]:
for label, value in doc.custom_fields:
if value is not None:
cf_list.append(str(value))
else:
cf_list.append("-")
table.add_row(str(doc["doc_id"]), doc["title"], *cf_list)
table.add_row(str(doc.id), doc.title, *cf_list)

console = Console()
console.print(table)
Expand Down
22 changes: 18 additions & 4 deletions papermerge/core/db/doc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
import uuid
from datetime import datetime
from typing import Optional
Expand Down Expand Up @@ -442,6 +443,7 @@ def get_document_custom_field_values(
def get_documents_by_type(
session: Session,
type_id: UUID,
parent_id: UUID,
user_id: UUID,
):
"""
Expand Down Expand Up @@ -483,8 +485,20 @@ def get_documents_by_type(
ON cfv.field_id = cf.cf_id AND cfv.document_id = doc_id
WHERE node.parent_id = :parent_id
"""
params = {"parent_id": "hoho", "type_id": str(type_id).replace("-", "")}
for row in session.execute(text(stmt), params):
print(row)
str_parent_id = str(parent_id).replace("-", "")
str_type_id = str(type_id).replace("-", "")
params = {"parent_id": str_parent_id, "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):
items = list(group)
results.append(
schemas.DocumentCFV(
id=uuid.UUID(document_id),
title=items[0].title,
document_type_id=uuid.UUID(items[0].document_type_id),
custom_fields=[(i.cf_name, str(i.cf_value)) for i in items],
)
)

return []
return results
2 changes: 2 additions & 0 deletions papermerge/core/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CustomField,
CustomFieldType,
CustomFieldValue,
DocumentCFV,
UpdateCustomField,
)
from .document_types import CreateDocumentType, DocumentType, UpdateDocumentType
Expand Down Expand Up @@ -58,6 +59,7 @@
"CustomFieldType",
"CustomFieldValue",
"CFV",
"DocumentCFV",
"CreateDocumentType",
"DocumentType",
"UpdateDocumentType",
Expand Down
12 changes: 12 additions & 0 deletions papermerge/core/schemas/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,15 @@ class CFV(BaseModel):
custom_field_value_id: UUID | None = None
# `custom_field_values.value_text` or `custom_field_values.value_int` or ...
value: str | int | date | bool | float | None = None


class DocumentCFV(BaseModel):
id: UUID
title: str
# created_at: datetime
# updated_at: datetime
# parent_id: UUID | None
# user_id: UUID
document_type_id: UUID | None = None
thumbnail_url: str | None = None
custom_fields: list[tuple[str, str]]

0 comments on commit 0883457

Please sign in to comment.