Rewrites some additional for looping to use filters and prefetch additional many to many fields
This commit is contained in:
parent
ca40a99c6d
commit
9a6ce6267e
@ -70,6 +70,17 @@ class WorkflowTriggerPlugin(
|
|||||||
for workflow in (
|
for workflow in (
|
||||||
Workflow.objects.filter(enabled=True)
|
Workflow.objects.filter(enabled=True)
|
||||||
.prefetch_related("actions")
|
.prefetch_related("actions")
|
||||||
|
.prefetch_related("actions__assign_view_users")
|
||||||
|
.prefetch_related("actions__assign_view_groups")
|
||||||
|
.prefetch_related("actions__assign_change_users")
|
||||||
|
.prefetch_related("actions__assign_change_groups")
|
||||||
|
.prefetch_related("actions__assign_custom_fields")
|
||||||
|
.prefetch_related("actions__remove_tags")
|
||||||
|
.prefetch_related("actions__remove_correspondents")
|
||||||
|
.prefetch_related("actions__remove_document_types")
|
||||||
|
.prefetch_related("actions__remove_storage_paths")
|
||||||
|
.prefetch_related("actions__remove_custom_fields")
|
||||||
|
.prefetch_related("actions__remove_owners")
|
||||||
.prefetch_related("triggers")
|
.prefetch_related("triggers")
|
||||||
.order_by("order")
|
.order_by("order")
|
||||||
):
|
):
|
||||||
@ -88,9 +99,10 @@ class WorkflowTriggerPlugin(
|
|||||||
if action.assign_title is not None:
|
if action.assign_title is not None:
|
||||||
action_overrides.title = action.assign_title
|
action_overrides.title = action.assign_title
|
||||||
if action.assign_tags is not None:
|
if action.assign_tags is not None:
|
||||||
action_overrides.tag_ids = [
|
action_overrides.tag_ids = list(
|
||||||
tag.pk for tag in action.assign_tags.all()
|
action.assign_tags.values_list("pk", flat=True),
|
||||||
]
|
)
|
||||||
|
|
||||||
if action.assign_correspondent is not None:
|
if action.assign_correspondent is not None:
|
||||||
action_overrides.correspondent_id = (
|
action_overrides.correspondent_id = (
|
||||||
action.assign_correspondent.pk
|
action.assign_correspondent.pk
|
||||||
@ -106,83 +118,80 @@ class WorkflowTriggerPlugin(
|
|||||||
if action.assign_owner is not None:
|
if action.assign_owner is not None:
|
||||||
action_overrides.owner_id = action.assign_owner.pk
|
action_overrides.owner_id = action.assign_owner.pk
|
||||||
if action.assign_view_users is not None:
|
if action.assign_view_users is not None:
|
||||||
action_overrides.view_users = [
|
action_overrides.view_users = list(
|
||||||
user.pk for user in action.assign_view_users.all()
|
action.assign_view_users.values_list("pk", flat=True),
|
||||||
]
|
)
|
||||||
if action.assign_view_groups is not None:
|
if action.assign_view_groups is not None:
|
||||||
action_overrides.view_groups = [
|
action_overrides.view_groups = list(
|
||||||
group.pk for group in action.assign_view_groups.all()
|
action.assign_view_groups.values_list("pk", flat=True),
|
||||||
]
|
)
|
||||||
if action.assign_change_users is not None:
|
if action.assign_change_users is not None:
|
||||||
action_overrides.change_users = [
|
action_overrides.change_users = list(
|
||||||
user.pk for user in action.assign_change_users.all()
|
action.assign_change_users.values_list("pk", flat=True),
|
||||||
]
|
)
|
||||||
if action.assign_change_groups is not None:
|
if action.assign_change_groups is not None:
|
||||||
action_overrides.change_groups = [
|
action_overrides.change_groups = list(
|
||||||
group.pk for group in action.assign_change_groups.all()
|
action.assign_change_groups.values_list(
|
||||||
]
|
"pk",
|
||||||
|
flat=True,
|
||||||
|
),
|
||||||
|
)
|
||||||
if action.assign_custom_fields is not None:
|
if action.assign_custom_fields is not None:
|
||||||
action_overrides.custom_field_ids = [
|
action_overrides.custom_field_ids = list(
|
||||||
field.pk for field in action.assign_custom_fields.all()
|
action.assign_custom_fields.values_list(
|
||||||
]
|
"pk",
|
||||||
|
flat=True,
|
||||||
|
),
|
||||||
|
)
|
||||||
overrides.update(action_overrides)
|
overrides.update(action_overrides)
|
||||||
elif action.type == WorkflowAction.WorkflowActionType.REMOVAL:
|
elif action.type == WorkflowAction.WorkflowActionType.REMOVAL:
|
||||||
# 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 action.remove_tags.all().count() > 0 and overrides.tag_ids:
|
elif overrides.tag_ids:
|
||||||
for tag in action.remove_tags.all():
|
for tag in action.remove_custom_fields.filter(
|
||||||
if tag.pk in overrides.tag_ids:
|
pk__in=overrides.tag_ids,
|
||||||
|
):
|
||||||
overrides.tag_ids.remove(tag.pk)
|
overrides.tag_ids.remove(tag.pk)
|
||||||
|
|
||||||
if action.remove_all_correspondents:
|
if action.remove_all_correspondents or (
|
||||||
overrides.correspondent_id = None
|
overrides.correspondent_id is not None
|
||||||
elif (
|
and action.remove_correspondents.filter(
|
||||||
action.remove_correspondents.all().count() > 0
|
pk=overrides.correspondent_id,
|
||||||
and overrides.correspondent_id is not None
|
).exists()
|
||||||
):
|
):
|
||||||
for correspondent in action.remove_correspondents.all():
|
|
||||||
if overrides.correspondent_id == correspondent.pk:
|
|
||||||
overrides.correspondent_id = None
|
overrides.correspondent_id = None
|
||||||
|
|
||||||
if action.remove_all_document_types:
|
if action.remove_all_document_types or (
|
||||||
overrides.document_type_id = None
|
overrides.document_type_id is not None
|
||||||
elif (
|
and action.remove_document_types.filter(
|
||||||
action.remove_document_types.all().count() > 0
|
pk=overrides.document_type_id,
|
||||||
and overrides.document_type_id is not None
|
).exists()
|
||||||
):
|
):
|
||||||
for doc_type in action.remove_document_types.all():
|
|
||||||
if overrides.document_type_id == doc_type.pk:
|
|
||||||
overrides.document_type_id = None
|
overrides.document_type_id = None
|
||||||
|
|
||||||
if action.remove_all_storage_paths:
|
if action.remove_all_storage_paths or (
|
||||||
overrides.storage_path_id = None
|
overrides.storage_path_id is not None
|
||||||
elif (
|
and action.remove_storage_paths.filter(
|
||||||
action.remove_storage_paths.all().count() > 0
|
pk=overrides.storage_path_id,
|
||||||
and overrides.storage_path_id is not None
|
).exists()
|
||||||
):
|
):
|
||||||
for storage_path in action.remove_storage_paths.all():
|
|
||||||
if overrides.storage_path_id == storage_path.pk:
|
|
||||||
overrides.storage_path_id = None
|
overrides.storage_path_id = None
|
||||||
|
|
||||||
if action.remove_all_custom_fields:
|
if action.remove_all_custom_fields:
|
||||||
overrides.custom_field_ids = []
|
overrides.custom_field_ids = []
|
||||||
elif (
|
elif overrides.custom_field_ids:
|
||||||
action.remove_custom_fields.all().count() > 0
|
for field in action.remove_custom_fields.filter(
|
||||||
and overrides.custom_field_ids
|
pk__in=overrides.custom_field_ids,
|
||||||
):
|
):
|
||||||
for field in action.remove_custom_fields.all():
|
|
||||||
if field.pk in overrides.custom_field_ids:
|
|
||||||
overrides.custom_field_ids.remove(field.pk)
|
overrides.custom_field_ids.remove(field.pk)
|
||||||
|
|
||||||
if action.remove_all_owners:
|
if action.remove_all_owners or (
|
||||||
overrides.owner_id = None
|
overrides.owner_id is not None
|
||||||
elif (
|
and action.remove_owners.filter(
|
||||||
action.remove_owners.all().count() > 0
|
pk=overrides.owner_id,
|
||||||
and overrides.owner_id is not None
|
).exists()
|
||||||
):
|
):
|
||||||
for owner in action.remove_owners.all():
|
|
||||||
if overrides.owner_id == owner.pk:
|
|
||||||
overrides.owner_id = None
|
overrides.owner_id = None
|
||||||
|
|
||||||
if action.remove_all_permissions:
|
if action.remove_all_permissions:
|
||||||
@ -192,21 +201,25 @@ class WorkflowTriggerPlugin(
|
|||||||
overrides.change_groups = []
|
overrides.change_groups = []
|
||||||
else:
|
else:
|
||||||
if overrides.view_users:
|
if overrides.view_users:
|
||||||
for user in action.remove_view_users.all():
|
for user in action.remove_view_users.filter(
|
||||||
if user.pk in overrides.view_users:
|
pk__in=overrides.view_users,
|
||||||
|
):
|
||||||
overrides.view_users.remove(user.pk)
|
overrides.view_users.remove(user.pk)
|
||||||
if overrides.change_users:
|
if overrides.change_users:
|
||||||
for user in action.remove_change_users.all():
|
for user in action.remove_change_users.filter(
|
||||||
if user.pk in overrides.change_users:
|
pk__in=overrides.change_users,
|
||||||
|
):
|
||||||
overrides.change_users.remove(user.pk)
|
overrides.change_users.remove(user.pk)
|
||||||
if overrides.view_groups:
|
if overrides.view_groups:
|
||||||
for group in action.remove_view_groups.all():
|
for user in action.remove_view_groups.filter(
|
||||||
if group.pk in overrides.view_groups:
|
pk__in=overrides.view_groups,
|
||||||
overrides.view_groups.remove(group.pk)
|
):
|
||||||
|
overrides.view_groups.remove(user.pk)
|
||||||
if overrides.change_groups:
|
if overrides.change_groups:
|
||||||
for group in action.remove_change_groups.all():
|
for user in action.remove_change_groups.filter(
|
||||||
if group.pk in overrides.change_groups:
|
pk__in=overrides.change_groups,
|
||||||
overrides.change_groups.remove(group.pk)
|
):
|
||||||
|
overrides.change_groups.remove(user.pk)
|
||||||
|
|
||||||
self.metadata.update(overrides)
|
self.metadata.update(overrides)
|
||||||
return msg
|
return msg
|
||||||
|
@ -537,6 +537,17 @@ def run_workflow(
|
|||||||
triggers__type=trigger_type,
|
triggers__type=trigger_type,
|
||||||
)
|
)
|
||||||
.prefetch_related("actions")
|
.prefetch_related("actions")
|
||||||
|
.prefetch_related("actions__assign_view_users")
|
||||||
|
.prefetch_related("actions__assign_view_groups")
|
||||||
|
.prefetch_related("actions__assign_change_users")
|
||||||
|
.prefetch_related("actions__assign_change_groups")
|
||||||
|
.prefetch_related("actions__assign_custom_fields")
|
||||||
|
.prefetch_related("actions__remove_tags")
|
||||||
|
.prefetch_related("actions__remove_correspondents")
|
||||||
|
.prefetch_related("actions__remove_document_types")
|
||||||
|
.prefetch_related("actions__remove_storage_paths")
|
||||||
|
.prefetch_related("actions__remove_custom_fields")
|
||||||
|
.prefetch_related("actions__remove_owners")
|
||||||
.prefetch_related("triggers")
|
.prefetch_related("triggers")
|
||||||
.order_by("order")
|
.order_by("order")
|
||||||
):
|
):
|
||||||
@ -665,28 +676,45 @@ def run_workflow(
|
|||||||
elif action.type == WorkflowAction.WorkflowActionType.REMOVAL:
|
elif action.type == WorkflowAction.WorkflowActionType.REMOVAL:
|
||||||
if action.remove_all_tags:
|
if action.remove_all_tags:
|
||||||
document.tags.clear()
|
document.tags.clear()
|
||||||
elif action.remove_tags.all().count() > 0:
|
else:
|
||||||
for tag in action.remove_tags.all():
|
for tag in action.remove_tags.filter(
|
||||||
if tag in document.tags.all():
|
pk__in=list(document.tags.values_list("pk", flat=True)),
|
||||||
document.tags.remove(tag)
|
).all():
|
||||||
|
document.tags.remove(tag.pk)
|
||||||
|
|
||||||
if action.remove_all_correspondents or (
|
if action.remove_all_correspondents or (
|
||||||
document.correspondent in action.remove_correspondents.all()
|
document.correspondent
|
||||||
|
and (
|
||||||
|
action.remove_correspondents.filter(
|
||||||
|
pk=document.correspondent.pk,
|
||||||
|
).exists()
|
||||||
|
)
|
||||||
):
|
):
|
||||||
document.correspondent = None
|
document.correspondent = None
|
||||||
|
|
||||||
if action.remove_all_document_types or (
|
if action.remove_all_document_types or (
|
||||||
document.document_type in action.remove_document_types.all()
|
document.document_type
|
||||||
|
and (
|
||||||
|
action.remove_document_types.filter(
|
||||||
|
pk=document.document_type.pk,
|
||||||
|
).exists()
|
||||||
|
)
|
||||||
):
|
):
|
||||||
document.document_type = None
|
document.document_type = None
|
||||||
|
|
||||||
if action.remove_all_storage_paths or (
|
if action.remove_all_storage_paths or (
|
||||||
document.storage_path in action.remove_storage_paths.all()
|
document.storage_path
|
||||||
|
and (
|
||||||
|
action.remove_storage_paths.filter(
|
||||||
|
pk=document.storage_path.pk,
|
||||||
|
).exists()
|
||||||
|
)
|
||||||
):
|
):
|
||||||
document.storage_path = None
|
document.storage_path = None
|
||||||
|
|
||||||
if action.remove_all_owners or (
|
if action.remove_all_owners or (
|
||||||
document.owner in action.remove_owners.all()
|
document.owner
|
||||||
|
and (action.remove_owners.filter(pk=document.owner.pk).exists())
|
||||||
):
|
):
|
||||||
document.owner = None
|
document.owner = None
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user