Prefetch actions and triggers, add some additional checks before using fields which may be None
This commit is contained in:
parent
85d2d47dc1
commit
ca40a99c6d
@ -7,6 +7,7 @@ from enum import Enum
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from subprocess import CompletedProcess
|
from subprocess import CompletedProcess
|
||||||
from subprocess import run
|
from subprocess import run
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import magic
|
import magic
|
||||||
@ -66,7 +67,12 @@ class WorkflowTriggerPlugin(
|
|||||||
"""
|
"""
|
||||||
msg = ""
|
msg = ""
|
||||||
overrides = DocumentMetadataOverrides()
|
overrides = DocumentMetadataOverrides()
|
||||||
for workflow in Workflow.objects.filter(enabled=True).order_by("order"):
|
for workflow in (
|
||||||
|
Workflow.objects.filter(enabled=True)
|
||||||
|
.prefetch_related("actions")
|
||||||
|
.prefetch_related("triggers")
|
||||||
|
.order_by("order")
|
||||||
|
):
|
||||||
action_overrides = DocumentMetadataOverrides()
|
action_overrides = DocumentMetadataOverrides()
|
||||||
|
|
||||||
if document_matches_workflow(
|
if document_matches_workflow(
|
||||||
@ -75,6 +81,8 @@ class WorkflowTriggerPlugin(
|
|||||||
WorkflowTrigger.WorkflowTriggerType.CONSUMPTION,
|
WorkflowTrigger.WorkflowTriggerType.CONSUMPTION,
|
||||||
):
|
):
|
||||||
for action in workflow.actions.all():
|
for action in workflow.actions.all():
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
assert isinstance(action, WorkflowAction)
|
||||||
msg += f"Applying {action} from {workflow}\n"
|
msg += f"Applying {action} from {workflow}\n"
|
||||||
if action.type == WorkflowAction.WorkflowActionType.ASSIGNMENT:
|
if action.type == WorkflowAction.WorkflowActionType.ASSIGNMENT:
|
||||||
if action.assign_title is not None:
|
if action.assign_title is not None:
|
||||||
@ -122,10 +130,7 @@ class WorkflowTriggerPlugin(
|
|||||||
# Removal actions overwrite the current overrides
|
# Removal actions overwrite the current overrides
|
||||||
if action.remove_all_tags:
|
if action.remove_all_tags:
|
||||||
overrides.tag_ids = []
|
overrides.tag_ids = []
|
||||||
elif (
|
elif action.remove_tags.all().count() > 0 and overrides.tag_ids:
|
||||||
action.remove_tags.all().count() > 0
|
|
||||||
and len(overrides.tag_ids) > 0
|
|
||||||
):
|
|
||||||
for tag in action.remove_tags.all():
|
for tag in action.remove_tags.all():
|
||||||
if tag.pk in overrides.tag_ids:
|
if tag.pk in overrides.tag_ids:
|
||||||
overrides.tag_ids.remove(tag.pk)
|
overrides.tag_ids.remove(tag.pk)
|
||||||
@ -164,7 +169,7 @@ class WorkflowTriggerPlugin(
|
|||||||
overrides.custom_field_ids = []
|
overrides.custom_field_ids = []
|
||||||
elif (
|
elif (
|
||||||
action.remove_custom_fields.all().count() > 0
|
action.remove_custom_fields.all().count() > 0
|
||||||
and len(overrides.custom_field_ids) > 0
|
and overrides.custom_field_ids
|
||||||
):
|
):
|
||||||
for field in action.remove_custom_fields.all():
|
for field in action.remove_custom_fields.all():
|
||||||
if field.pk in overrides.custom_field_ids:
|
if field.pk in overrides.custom_field_ids:
|
||||||
@ -186,18 +191,22 @@ class WorkflowTriggerPlugin(
|
|||||||
overrides.change_users = []
|
overrides.change_users = []
|
||||||
overrides.change_groups = []
|
overrides.change_groups = []
|
||||||
else:
|
else:
|
||||||
for user in action.remove_view_users.all():
|
if overrides.view_users:
|
||||||
if user.pk in overrides.view_users:
|
for user in action.remove_view_users.all():
|
||||||
overrides.view_users.remove(user.pk)
|
if user.pk in overrides.view_users:
|
||||||
for user in action.remove_change_users.all():
|
overrides.view_users.remove(user.pk)
|
||||||
if user.pk in overrides.change_users:
|
if overrides.change_users:
|
||||||
overrides.change_users.remove(user.pk)
|
for user in action.remove_change_users.all():
|
||||||
for group in action.remove_view_groups.all():
|
if user.pk in overrides.change_users:
|
||||||
if group.pk in overrides.view_groups:
|
overrides.change_users.remove(user.pk)
|
||||||
overrides.view_groups.remove(group.pk)
|
if overrides.view_groups:
|
||||||
for group in action.remove_change_groups.all():
|
for group in action.remove_view_groups.all():
|
||||||
if group.pk in overrides.change_groups:
|
if group.pk in overrides.view_groups:
|
||||||
overrides.change_groups.remove(group.pk)
|
overrides.view_groups.remove(group.pk)
|
||||||
|
if overrides.change_groups:
|
||||||
|
for group in action.remove_change_groups.all():
|
||||||
|
if group.pk in overrides.change_groups:
|
||||||
|
overrides.change_groups.remove(group.pk)
|
||||||
|
|
||||||
self.metadata.update(overrides)
|
self.metadata.update(overrides)
|
||||||
return msg
|
return msg
|
||||||
|
@ -531,10 +531,15 @@ def run_workflow(
|
|||||||
document: Document,
|
document: Document,
|
||||||
logging_group=None,
|
logging_group=None,
|
||||||
):
|
):
|
||||||
for workflow in Workflow.objects.filter(
|
for workflow in (
|
||||||
enabled=True,
|
Workflow.objects.filter(
|
||||||
triggers__type=trigger_type,
|
enabled=True,
|
||||||
).order_by("order"):
|
triggers__type=trigger_type,
|
||||||
|
)
|
||||||
|
.prefetch_related("actions")
|
||||||
|
.prefetch_related("triggers")
|
||||||
|
.order_by("order")
|
||||||
|
):
|
||||||
if matching.document_matches_workflow(
|
if matching.document_matches_workflow(
|
||||||
document,
|
document,
|
||||||
workflow,
|
workflow,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user