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 (
|
||||
Workflow.objects.filter(enabled=True)
|
||||
.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")
|
||||
.order_by("order")
|
||||
):
|
||||
@ -88,9 +99,10 @@ class WorkflowTriggerPlugin(
|
||||
if action.assign_title is not None:
|
||||
action_overrides.title = action.assign_title
|
||||
if action.assign_tags is not None:
|
||||
action_overrides.tag_ids = [
|
||||
tag.pk for tag in action.assign_tags.all()
|
||||
]
|
||||
action_overrides.tag_ids = list(
|
||||
action.assign_tags.values_list("pk", flat=True),
|
||||
)
|
||||
|
||||
if action.assign_correspondent is not None:
|
||||
action_overrides.correspondent_id = (
|
||||
action.assign_correspondent.pk
|
||||
@ -106,84 +118,81 @@ class WorkflowTriggerPlugin(
|
||||
if action.assign_owner is not None:
|
||||
action_overrides.owner_id = action.assign_owner.pk
|
||||
if action.assign_view_users is not None:
|
||||
action_overrides.view_users = [
|
||||
user.pk for user in action.assign_view_users.all()
|
||||
]
|
||||
action_overrides.view_users = list(
|
||||
action.assign_view_users.values_list("pk", flat=True),
|
||||
)
|
||||
if action.assign_view_groups is not None:
|
||||
action_overrides.view_groups = [
|
||||
group.pk for group in action.assign_view_groups.all()
|
||||
]
|
||||
action_overrides.view_groups = list(
|
||||
action.assign_view_groups.values_list("pk", flat=True),
|
||||
)
|
||||
if action.assign_change_users is not None:
|
||||
action_overrides.change_users = [
|
||||
user.pk for user in action.assign_change_users.all()
|
||||
]
|
||||
action_overrides.change_users = list(
|
||||
action.assign_change_users.values_list("pk", flat=True),
|
||||
)
|
||||
if action.assign_change_groups is not None:
|
||||
action_overrides.change_groups = [
|
||||
group.pk for group in action.assign_change_groups.all()
|
||||
]
|
||||
action_overrides.change_groups = list(
|
||||
action.assign_change_groups.values_list(
|
||||
"pk",
|
||||
flat=True,
|
||||
),
|
||||
)
|
||||
if action.assign_custom_fields is not None:
|
||||
action_overrides.custom_field_ids = [
|
||||
field.pk for field in action.assign_custom_fields.all()
|
||||
]
|
||||
action_overrides.custom_field_ids = list(
|
||||
action.assign_custom_fields.values_list(
|
||||
"pk",
|
||||
flat=True,
|
||||
),
|
||||
)
|
||||
overrides.update(action_overrides)
|
||||
elif action.type == WorkflowAction.WorkflowActionType.REMOVAL:
|
||||
# Removal actions overwrite the current overrides
|
||||
if action.remove_all_tags:
|
||||
overrides.tag_ids = []
|
||||
elif action.remove_tags.all().count() > 0 and overrides.tag_ids:
|
||||
for tag in action.remove_tags.all():
|
||||
if tag.pk in overrides.tag_ids:
|
||||
overrides.tag_ids.remove(tag.pk)
|
||||
elif overrides.tag_ids:
|
||||
for tag in action.remove_custom_fields.filter(
|
||||
pk__in=overrides.tag_ids,
|
||||
):
|
||||
overrides.tag_ids.remove(tag.pk)
|
||||
|
||||
if action.remove_all_correspondents:
|
||||
if action.remove_all_correspondents or (
|
||||
overrides.correspondent_id is not None
|
||||
and action.remove_correspondents.filter(
|
||||
pk=overrides.correspondent_id,
|
||||
).exists()
|
||||
):
|
||||
overrides.correspondent_id = None
|
||||
elif (
|
||||
action.remove_correspondents.all().count() > 0
|
||||
and overrides.correspondent_id is not None
|
||||
):
|
||||
for correspondent in action.remove_correspondents.all():
|
||||
if overrides.correspondent_id == correspondent.pk:
|
||||
overrides.correspondent_id = None
|
||||
|
||||
if action.remove_all_document_types:
|
||||
if action.remove_all_document_types or (
|
||||
overrides.document_type_id is not None
|
||||
and action.remove_document_types.filter(
|
||||
pk=overrides.document_type_id,
|
||||
).exists()
|
||||
):
|
||||
overrides.document_type_id = None
|
||||
elif (
|
||||
action.remove_document_types.all().count() > 0
|
||||
and overrides.document_type_id is not None
|
||||
):
|
||||
for doc_type in action.remove_document_types.all():
|
||||
if overrides.document_type_id == doc_type.pk:
|
||||
overrides.document_type_id = None
|
||||
|
||||
if action.remove_all_storage_paths:
|
||||
overrides.storage_path_id = None
|
||||
elif (
|
||||
action.remove_storage_paths.all().count() > 0
|
||||
and overrides.storage_path_id is not None
|
||||
if action.remove_all_storage_paths or (
|
||||
overrides.storage_path_id is not None
|
||||
and action.remove_storage_paths.filter(
|
||||
pk=overrides.storage_path_id,
|
||||
).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:
|
||||
overrides.custom_field_ids = []
|
||||
elif (
|
||||
action.remove_custom_fields.all().count() > 0
|
||||
and 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)
|
||||
elif overrides.custom_field_ids:
|
||||
for field in action.remove_custom_fields.filter(
|
||||
pk__in=overrides.custom_field_ids,
|
||||
):
|
||||
overrides.custom_field_ids.remove(field.pk)
|
||||
|
||||
if action.remove_all_owners:
|
||||
overrides.owner_id = None
|
||||
elif (
|
||||
action.remove_owners.all().count() > 0
|
||||
and overrides.owner_id is not None
|
||||
if action.remove_all_owners or (
|
||||
overrides.owner_id is not None
|
||||
and action.remove_owners.filter(
|
||||
pk=overrides.owner_id,
|
||||
).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:
|
||||
overrides.view_users = []
|
||||
@ -192,21 +201,25 @@ class WorkflowTriggerPlugin(
|
||||
overrides.change_groups = []
|
||||
else:
|
||||
if overrides.view_users:
|
||||
for user in action.remove_view_users.all():
|
||||
if user.pk in overrides.view_users:
|
||||
overrides.view_users.remove(user.pk)
|
||||
for user in action.remove_view_users.filter(
|
||||
pk__in=overrides.view_users,
|
||||
):
|
||||
overrides.view_users.remove(user.pk)
|
||||
if overrides.change_users:
|
||||
for user in action.remove_change_users.all():
|
||||
if user.pk in overrides.change_users:
|
||||
overrides.change_users.remove(user.pk)
|
||||
for user in action.remove_change_users.filter(
|
||||
pk__in=overrides.change_users,
|
||||
):
|
||||
overrides.change_users.remove(user.pk)
|
||||
if overrides.view_groups:
|
||||
for group in action.remove_view_groups.all():
|
||||
if group.pk in overrides.view_groups:
|
||||
overrides.view_groups.remove(group.pk)
|
||||
for user in action.remove_view_groups.filter(
|
||||
pk__in=overrides.view_groups,
|
||||
):
|
||||
overrides.view_groups.remove(user.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)
|
||||
for user in action.remove_change_groups.filter(
|
||||
pk__in=overrides.change_groups,
|
||||
):
|
||||
overrides.change_groups.remove(user.pk)
|
||||
|
||||
self.metadata.update(overrides)
|
||||
return msg
|
||||
|
@ -537,6 +537,17 @@ def run_workflow(
|
||||
triggers__type=trigger_type,
|
||||
)
|
||||
.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")
|
||||
.order_by("order")
|
||||
):
|
||||
@ -665,28 +676,45 @@ def run_workflow(
|
||||
elif action.type == WorkflowAction.WorkflowActionType.REMOVAL:
|
||||
if action.remove_all_tags:
|
||||
document.tags.clear()
|
||||
elif action.remove_tags.all().count() > 0:
|
||||
for tag in action.remove_tags.all():
|
||||
if tag in document.tags.all():
|
||||
document.tags.remove(tag)
|
||||
else:
|
||||
for tag in action.remove_tags.filter(
|
||||
pk__in=list(document.tags.values_list("pk", flat=True)),
|
||||
).all():
|
||||
document.tags.remove(tag.pk)
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user