mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-10-13 11:37:13 +00:00
Pull the ai workflow through serializer and check for required triggers
This commit is contained in:
1 parent
d4d5a060dd
commit
91c826314f
1 file changed
+47
@@ -3235,6 +3235,9 @@ class WorkflowActionSerializer(serializers.ModelSerializer[WorkflowAction]):
|
|||||||
"email",
|
"email",
|
||||||
"webhook",
|
"webhook",
|
||||||
"passwords",
|
"passwords",
|
||||||
|
"ai_suggestion_fields",
|
||||||
|
"ai_create_missing",
|
||||||
|
"ai_overwrite_existing",
|
||||||
]
|
]
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
@@ -3292,6 +3295,23 @@ class WorkflowActionSerializer(serializers.ModelSerializer[WorkflowAction]):
|
|||||||
"Passwords are required for password removal actions",
|
"Passwords are required for password removal actions",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
"type" in attrs
|
||||||
|
and attrs["type"] == WorkflowAction.WorkflowActionType.APPLY_AI_SUGGESTIONS
|
||||||
|
):
|
||||||
|
fields = attrs.get("ai_suggestion_fields")
|
||||||
|
valid_fields = set(WorkflowAction.AISuggestionField.values)
|
||||||
|
if (
|
||||||
|
fields is None
|
||||||
|
or not isinstance(fields, list)
|
||||||
|
or len(fields) == 0
|
||||||
|
or any(field not in valid_fields for field in fields)
|
||||||
|
):
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"At least one valid field is required for apply AI "
|
||||||
|
f"suggestions actions, options are: {sorted(valid_fields)}",
|
||||||
|
)
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
@@ -3320,24 +3340,43 @@ class WorkflowSerializer(serializers.ModelSerializer[Workflow]):
|
|||||||
action.get("type") == WorkflowAction.WorkflowActionType.REMOTE_OCR
|
action.get("type") == WorkflowAction.WorkflowActionType.REMOTE_OCR
|
||||||
for action in attrs["actions"]
|
for action in attrs["actions"]
|
||||||
)
|
)
|
||||||
|
has_ai_suggestions_action = any(
|
||||||
|
action.get("type")
|
||||||
|
== WorkflowAction.WorkflowActionType.APPLY_AI_SUGGESTIONS
|
||||||
|
for action in attrs["actions"]
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
has_remote_ocr_action = self.instance is not None and (
|
has_remote_ocr_action = self.instance is not None and (
|
||||||
self.instance.actions.filter(
|
self.instance.actions.filter(
|
||||||
type=WorkflowAction.WorkflowActionType.REMOTE_OCR,
|
type=WorkflowAction.WorkflowActionType.REMOTE_OCR,
|
||||||
).exists()
|
).exists()
|
||||||
)
|
)
|
||||||
|
has_ai_suggestions_action = self.instance is not None and (
|
||||||
|
self.instance.actions.filter(
|
||||||
|
type=WorkflowAction.WorkflowActionType.APPLY_AI_SUGGESTIONS,
|
||||||
|
).exists()
|
||||||
|
)
|
||||||
|
|
||||||
if "triggers" in attrs:
|
if "triggers" in attrs:
|
||||||
has_consumption_trigger = any(
|
has_consumption_trigger = any(
|
||||||
trigger.get("type") == WorkflowTrigger.WorkflowTriggerType.CONSUMPTION
|
trigger.get("type") == WorkflowTrigger.WorkflowTriggerType.CONSUMPTION
|
||||||
for trigger in attrs["triggers"]
|
for trigger in attrs["triggers"]
|
||||||
)
|
)
|
||||||
|
has_non_consumption_trigger = any(
|
||||||
|
trigger.get("type") != WorkflowTrigger.WorkflowTriggerType.CONSUMPTION
|
||||||
|
for trigger in attrs["triggers"]
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
has_consumption_trigger = self.instance is not None and (
|
has_consumption_trigger = self.instance is not None and (
|
||||||
self.instance.triggers.filter(
|
self.instance.triggers.filter(
|
||||||
type=WorkflowTrigger.WorkflowTriggerType.CONSUMPTION,
|
type=WorkflowTrigger.WorkflowTriggerType.CONSUMPTION,
|
||||||
).exists()
|
).exists()
|
||||||
)
|
)
|
||||||
|
has_non_consumption_trigger = self.instance is not None and (
|
||||||
|
self.instance.triggers.exclude(
|
||||||
|
type=WorkflowTrigger.WorkflowTriggerType.CONSUMPTION,
|
||||||
|
).exists()
|
||||||
|
)
|
||||||
|
|
||||||
# Remote OCR can only work with consumption triggers
|
# Remote OCR can only work with consumption triggers
|
||||||
if has_remote_ocr_action and not has_consumption_trigger:
|
if has_remote_ocr_action and not has_consumption_trigger:
|
||||||
@@ -3345,6 +3384,14 @@ class WorkflowSerializer(serializers.ModelSerializer[Workflow]):
|
|||||||
"Remote OCR actions require a consumption started trigger",
|
"Remote OCR actions require a consumption started trigger",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Suggestions are made from the document content, which does not exist
|
||||||
|
# until after consumption has finished
|
||||||
|
if has_ai_suggestions_action and not has_non_consumption_trigger:
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"Apply AI suggestions actions require a trigger other than "
|
||||||
|
"consumption started",
|
||||||
|
)
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def update_triggers_and_actions(
|
def update_triggers_and_actions(
|
||||||
|
|||||||
Reference in new issue
Block a user