diff --git a/papermerge/core/cli/docs.py b/papermerge/core/cli/docs.py index f022d2047..73f7e6605 100644 --- a/papermerge/core/cli/docs.py +++ b/papermerge/core/cli/docs.py @@ -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 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 should be even. There should be at + least two items after . + """ + 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) diff --git a/papermerge/core/db/__init__.py b/papermerge/core/db/__init__.py index 90480a3d9..fedf86a7f 100644 --- a/papermerge/core/db/__init__.py +++ b/papermerge/core/db/__init__.py @@ -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 ( @@ -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", diff --git a/papermerge/core/db/doc.py b/papermerge/core/db/doc.py index c21e0e431..45794c568 100644 --- a/papermerge/core/db/doc.py +++ b/papermerge/core/db/doc.py @@ -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 diff --git a/papermerge/core/db/models.py b/papermerge/core/db/models.py index 411366262..172c8254b 100644 --- a/papermerge/core/db/models.py +++ b/papermerge/core/db/models.py @@ -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"