mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-02 21:15:34 +00:00
Enhancement: support custom fields in workflow title templates (#14564)
This commit is contained in:
1 parent
cc54e3e5aa
commit
9c4c9f2386
6 files changed
+182
-27
No files matched your search
@@ -11,6 +11,8 @@ from documents.data_models import DocumentMetadataOverrides
|
||||
from documents.mail import EmailAttachment
|
||||
from documents.mail import send_email
|
||||
from documents.models import Correspondent
|
||||
from documents.models import CustomField
|
||||
from documents.models import CustomFieldInstance
|
||||
from documents.models import Document
|
||||
from documents.models import DocumentType
|
||||
from documents.models import WorkflowAction
|
||||
@@ -48,6 +50,9 @@ def build_workflow_action_context(
|
||||
"added": timezone.localtime(document.added),
|
||||
"created": document.created,
|
||||
"id": document.pk,
|
||||
"custom_fields": CustomFieldInstance.global_objects.filter(
|
||||
document=document,
|
||||
).select_related("field"),
|
||||
}
|
||||
|
||||
correspondent_obj = (
|
||||
@@ -80,6 +85,19 @@ def build_workflow_action_context(
|
||||
"added": timezone.localtime(timezone.now()),
|
||||
"created": overrides.created if overrides else None,
|
||||
"id": "",
|
||||
"custom_fields": [
|
||||
CustomFieldInstance(
|
||||
field=field,
|
||||
**{
|
||||
CustomFieldInstance.get_value_field_name(
|
||||
field.data_type,
|
||||
): overrides.custom_fields[field.pk],
|
||||
},
|
||||
)
|
||||
for field in CustomField.objects.filter(
|
||||
pk__in=overrides.custom_fields or {},
|
||||
)
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@@ -116,6 +134,7 @@ def execute_email_action(
|
||||
context["title"],
|
||||
context["doc_url"],
|
||||
context["id"],
|
||||
custom_fields=context["custom_fields"],
|
||||
)
|
||||
if action.email.subject
|
||||
else ""
|
||||
@@ -133,6 +152,7 @@ def execute_email_action(
|
||||
context["title"],
|
||||
context["doc_url"],
|
||||
context["id"],
|
||||
custom_fields=context["custom_fields"],
|
||||
)
|
||||
if action.email.body
|
||||
else ""
|
||||
@@ -211,6 +231,7 @@ def execute_webhook_action(
|
||||
context["title"],
|
||||
context["doc_url"],
|
||||
context["id"],
|
||||
custom_fields=context["custom_fields"],
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
@@ -230,6 +251,7 @@ def execute_webhook_action(
|
||||
context["title"],
|
||||
context["doc_url"],
|
||||
context["id"],
|
||||
custom_fields=context["custom_fields"],
|
||||
)
|
||||
headers = {}
|
||||
if action.webhook.headers:
|
||||
|
||||
@@ -43,6 +43,33 @@ def apply_assignment_to_document(
|
||||
if action.assign_owner:
|
||||
document.owner = action.assign_owner
|
||||
|
||||
if action.has_assign_custom_fields:
|
||||
for field in action.assign_custom_fields.all():
|
||||
value_field_name = CustomFieldInstance.get_value_field_name(
|
||||
data_type=field.data_type,
|
||||
)
|
||||
args = {
|
||||
value_field_name: action.assign_custom_fields_values.get(
|
||||
str(field.pk),
|
||||
None,
|
||||
),
|
||||
}
|
||||
# for some reason update_or_create doesn't work here
|
||||
instance = CustomFieldInstance.objects.filter(
|
||||
field=field,
|
||||
document=document,
|
||||
).first()
|
||||
# empty string is indistinguishable from no value in the UI
|
||||
if instance and args[value_field_name] not in (None, ""):
|
||||
setattr(instance, value_field_name, args[value_field_name])
|
||||
instance.save()
|
||||
elif not instance:
|
||||
CustomFieldInstance.objects.create(
|
||||
**args,
|
||||
field=field,
|
||||
document=document,
|
||||
)
|
||||
|
||||
if action.assign_title:
|
||||
try:
|
||||
title = parse_w_workflow_placeholders(
|
||||
@@ -57,6 +84,9 @@ def apply_assignment_to_document(
|
||||
"", # dont pass the title to avoid recursion
|
||||
"", # no urls in titles
|
||||
document.pk,
|
||||
custom_fields=CustomFieldInstance.global_objects.filter(
|
||||
document=document,
|
||||
).select_related("field"),
|
||||
)
|
||||
if title:
|
||||
document.title = title
|
||||
@@ -91,33 +121,6 @@ def apply_assignment_to_document(
|
||||
merge=True,
|
||||
)
|
||||
|
||||
if action.has_assign_custom_fields:
|
||||
for field in action.assign_custom_fields.all():
|
||||
value_field_name = CustomFieldInstance.get_value_field_name(
|
||||
data_type=field.data_type,
|
||||
)
|
||||
args = {
|
||||
value_field_name: action.assign_custom_fields_values.get(
|
||||
str(field.pk),
|
||||
None,
|
||||
),
|
||||
}
|
||||
# for some reason update_or_create doesn't work here
|
||||
instance = CustomFieldInstance.objects.filter(
|
||||
field=field,
|
||||
document=document,
|
||||
).first()
|
||||
# empty string is indistinguishable from no value in the UI
|
||||
if instance and args[value_field_name] not in (None, ""):
|
||||
setattr(instance, value_field_name, args[value_field_name])
|
||||
instance.save()
|
||||
elif not instance:
|
||||
CustomFieldInstance.objects.create(
|
||||
**args,
|
||||
field=field,
|
||||
document=document,
|
||||
)
|
||||
|
||||
|
||||
def apply_assignment_to_overrides(
|
||||
action: WorkflowAction,
|
||||
|
||||
Reference in new issue
Block a user