Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Oct 11, 2024
1 parent 7e3554c commit 24feb33
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
24 changes: 23 additions & 1 deletion papermerge/core/cli/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,35 @@ def document_types():
print_doc_types(doc_types)


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


@app.command(name="update-cf")
def update_doc_custom_fields(doc_id: uuid.UUID, custom_fields: list[str]):
"""Update custom fields for specific document
Example: docs update-cf <UUID> Total "22.89" Shop lidl Date "2024-12-26"
Note that name of the custom field is case-sensitive i.e. "Total" and "total"
are different things.
Number of items after <UUID> should be even. There should be at
least two items after <UUID>.
"""
cf = {}
if len(custom_fields) % 2 != 0:
raise ValueError("Number of items after UUID must be even")

half_length = int(len(custom_fields) / 2)

for i in range(0, half_length):
cf[custom_fields[i]] = custom_fields[i + 1]

db.update_document_custom_fields(session, id=doc_id, custom_fields=cf)


def get_subq():
nd = aliased(Node)
cfv = aliased(CustomFieldValue)
Expand Down
2 changes: 2 additions & 0 deletions papermerge/core/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
get_document_custom_field_values,
get_documents_by_type,
update_document_custom_field_values,
update_document_custom_fields,
)
from .doc_ver import get_doc_ver, get_last_doc_ver
from .document_types import (
Expand Down Expand Up @@ -79,6 +80,7 @@
"delete_document_type",
"update_document_type",
"update_document_custom_field_values",
"update_document_custom_fields",
"add_document_custom_field_values",
"get_document_custom_field_values",
"get_documents_by_type",
Expand Down
62 changes: 62 additions & 0 deletions papermerge/core/db/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,68 @@ def get_page(doc_ver_id):
return model_doc


def update_document_custom_fields(
session: Session,
id: UUID,
custom_fields: dict, # if of the document
):
"""
SELECT doc.basetreenode_ptr_id AS 'Doc ID',
dt.name AS 'Document Type',
cf.name AS 'Custom Field Name',
CASE
WHEN cf.data_type == 'monetary' THEN cfv.value_monetary
WHEN cf.data_type == 'date' THEN cfv.value_date
WHEN cf.data_type == 'string' THEN cfv.value_text
END AS 'CF VALUE'
FROM core_document AS doc
JOIN core_documenttype AS dt ON doc.document_type_id = dt.id
JOIN core_documenttypecustomfield dtcf ON dtcf.document_type_id = dt.id
JOIN core_customfield cf ON dtcf.custom_field_id = cf.id
LEFT JOIN core_customfieldvalue cfv ON cfv.document_id = doc.basetreenode_ptr_id
WHERE cf.name in ('Total', 'Shop')
AND doc.basetreenode_ptr_id = 'b0c90f2f7380404c81179903c55f113b';
"""
stmt = (
select(
CustomFieldValue.id.label("cfv_id"),
CustomField.name.label("cf_name"),
CustomField.id.label("cf_id"),
CustomField.data_type.label("cf_data_type"),
Document.id.label("doc_id"),
)
.join(DocumentType, DocumentType.id == Document.document_type_id)
.join(
DocumentTypeCustomField,
DocumentTypeCustomField.document_type_id == DocumentType.id,
)
.join(CustomField, CustomField.id == DocumentTypeCustomField.custom_field_id)
.join(
CustomFieldValue, CustomFieldValue.document_id == Document.id, isouter=True
)
).where(
Document.id == id,
CustomField.name.in_(custom_fields.keys()),
)
print(stmt)
for row in session.execute(stmt).all():
# new_value = custom_fields[row.cf_name]
print(
f"CFV_ID = {row.cfv_id} | CF_Name = {row.cf_name} | CF_ID = {row.cf_id} | DOC ID = {row.doc_id}"
)
# cfv = session.query(CustomFieldValue).where(id == row.cfv_id).one()
# match row.cf_data_type:
# case "monetary":
# cfv.value_monetary = new_value
# case "string":
# cfv.value_text = new_value
# case "date":
# cfv.value_date = new_value

# session.commit()


def update_document_custom_field_values(
session: Session,
id: UUID, # id of the document
Expand Down
3 changes: 3 additions & 0 deletions papermerge/core/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ class CustomFieldValue(Base):
value_select: Mapped[str]
created_at: Mapped[datetime] = mapped_column(insert_default=func.now())

def __repr__(self):
return f"CustomFieldValue(ID={self.id})"


class DocumentType(Base):
__tablename__ = "core_documenttype"
Expand Down

0 comments on commit 24feb33

Please sign in to comment.