Skip to content

Commit

Permalink
get_cfv
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Oct 12, 2024
1 parent 16e1e66 commit 720a258
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 43 deletions.
43 changes: 39 additions & 4 deletions papermerge/core/cli/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,40 @@ def list_documents_by_type(type_id: uuid.UUID):
print_docs(docs)


@app.command(name="cfv")
def get_cfv(doc_id: uuid.UUID, custom_fields: list[str]):
cf = {}
if len(custom_fields) % 2 != 0:
raise ValueError("Number of items after UUID must be even")

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

items: list[schemas.CFV] = db.update_document_custom_fields(
session, document_id=doc_id, custom_fields=cf
)

table = Table(title="Document's Custom Field Values")

table.add_column("CF ID", style="magenta")
table.add_column("CF Name")
table.add_column("CF Type")
table.add_column("CF Extra Data")
table.add_column("CF Value")

for item in items:
table.add_row(
str(item.custom_field_id),
item.name,
item.type,
item.extra_data,
str(item.value),
)

console = Console()
console.print(table)


@app.command(name="update-cf")
def update_doc_custom_fields(doc_id: uuid.UUID, custom_fields: list[str]):
"""Update custom fields for specific document
Expand All @@ -50,12 +84,13 @@ def update_doc_custom_fields(doc_id: uuid.UUID, custom_fields: list[str]):
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):
for i in range(0, len(custom_fields), 2):
cf[custom_fields[i]] = custom_fields[i + 1]

db.update_document_custom_fields(session, id=doc_id, custom_fields=cf)
items: list[schemas.CFV] = db.update_document_custom_fields(
session, document_id=doc_id, custom_fields=cf
)
print(items)


def get_subq():
Expand Down
109 changes: 72 additions & 37 deletions papermerge/core/db/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def get_page(doc_ver_id):
return model_doc


def get_doc_cfv(session: Session, document_id: UUID, cf_names: list[str]):
def get_doc_cfv(
session: Session, document_id: UUID, cf_names: list[str]
) -> list[schemas.CFV]:
"""
Fetch document's custom field values for each CF name, even of CFV is NULL
Expand Down Expand Up @@ -149,57 +151,90 @@ def get_doc_cfv(session: Session, document_id: UUID, cf_names: list[str]):
AND doc.basetreenode_ptr_id = 'b0c90f2f7380404c81179903c55f113b';
```
"""
cfv = aliased(CustomFieldValue)
cf = aliased(CustomField)

def get_subq():
cfv = aliased(CustomFieldValue)
cf = aliased(CustomField)

subq = (
select(
cf.id.label("cf_id"),
cf.name.label("cf_name"),
cf.data_type.label("cf_data_type"),
cf.extra_data.label("cf_extra_data"),
cfv.id.label("cfv_id"),
cfv.value_monetary,
cfv.value_text,
cfv.value_date,
cfv.value_bool,
)
.select_from(cf)
.join(cfv, cfv.field_id == cf.id, isouter=True)
.where(
or_(
and_(cfv.document_id == document_id, cf.name.in_(cf_names)),
and_(cfv.document_id == None, cf.name.in_(cf_names)),
)
)
.subquery()
)
return subq

subq = get_subq()
doc = aliased(Document)
dt = aliased(DocumentType)
dtcf = aliased(DocumentTypeCustomField)
stmt = (
select(
cf.id.label("cf_id"),
cf.name,
cf.data_type,
cfv.id.label("cfv_id"),
cfv.value_monetary,
cfv.value_text,
cfv.value_date,
)
.select_from(cf)
.join(cfv, cfv.field_id == cf.id, isouter=True)
.where(
or_(
and_(cfv.document_id == document_id, cf.name.in_(cf_names)),
and_(cfv.document_id is None, cf.name.in_(cf_names)),
)
doc.id.label("doc_id"),
doc.document_type_id,
subq.c.cf_name.label("cf_name"),
subq.c.cf_data_type.label("cf_type"),
subq.c.cf_extra_data.label("cf_extra_data"),
subq.c.cf_id.label("cf_id"),
subq.c.cfv_id.label("cfv_id"),
case(
(subq.c.cf_data_type == "monetary", subq.c.value_monetary),
(subq.c.cf_data_type == "string", subq.c.value_text),
(subq.c.cf_data_type == "date", subq.c.value_date),
"--",
).label("cf_value"),
)
.select_from(doc)
.join(dt, dt.id == doc.document_type_id)
.join(dtcf, dt.id == dtcf.document_type_id)
.join(subq, subq.c.cf_id == dtcf.custom_field_id)
.where(subq.c.cf_name.in_(cf_names), doc.id == document_id)
)
result = []
for row in session.execute(stmt):
print(row)
result.append(
schemas.CFV(
document_id=row.doc_id,
document_type_id=row.document_type_id,
custom_field_id=row.cf_id,
name=row.cf_name,
type=row.cf_type,
extra_data=row.cf_extra_data,
custom_field_value_id=row.cfv_id,
value=row.cf_value,
)
)

return []
return result


def update_document_custom_fields(
session: Session,
document_id: UUID,
custom_fields: dict, # if of the document
):
) -> list[schemas.CFV]:
""" """
rows = get_doc_cfv(session, document_id=document_id)
items = get_doc_cfv(
session, document_id=document_id, cf_names=list(custom_fields.keys())
)

for row in rows:
# 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()
return items


def update_document_custom_field_values(
Expand Down
2 changes: 2 additions & 0 deletions papermerge/core/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydantic import BaseModel

from .custom_fields import (
CFV,
CreateCustomField,
CustomField,
CustomFieldType,
Expand Down Expand Up @@ -56,6 +57,7 @@
"UpdateCustomField",
"CustomFieldType",
"CustomFieldValue",
"CFV",
"CreateDocumentType",
"DocumentType",
"UpdateDocumentType",
Expand Down
23 changes: 21 additions & 2 deletions papermerge/core/schemas/custom_fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import date
from enum import Enum
from uuid import UUID

Expand All @@ -12,8 +13,6 @@ class CustomFieldType(str, Enum):
int = "int"
float = "float"
monetary = "monetary"
select = "select"
document_link = "document_link"


class CustomField(BaseModel):
Expand Down Expand Up @@ -47,3 +46,23 @@ class CustomFieldValue(CustomField):
value: str | None = None
# the ID of the custom field
field_id: UUID


class CFV(BaseModel):
# custom field value
# `core_documents.id`
document_id: UUID
# `core_documents.document_type_id`
document_type_id: UUID
# `custom_fields.id`
custom_field_id: UUID
# `custom_fields.name`
name: str
# `custom_fields.type`
type: CustomFieldType
# `custom_fields.extra_data`
extra_data: str | None
# `custom_field_values.id`
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

0 comments on commit 720a258

Please sign in to comment.