Fixes, more cleanup
This commit is contained in:
parent
2e876022a5
commit
5fe8e51f2a
@ -536,73 +536,56 @@ def run_workflows(
|
|||||||
overrides: DocumentMetadataOverrides | None = None,
|
overrides: DocumentMetadataOverrides | None = None,
|
||||||
) -> tuple[DocumentMetadataOverrides, str] | None:
|
) -> tuple[DocumentMetadataOverrides, str] | None:
|
||||||
def assignment_action():
|
def assignment_action():
|
||||||
if action.assign_tags.all().count() > 0:
|
if action.assign_tags.exists():
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
doc_tag_ids.extend(
|
doc_tag_ids.extend(action.assign_tags.values_list("pk", flat=True))
|
||||||
list(action.assign_tags.all().values_list("pk", flat=True)),
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
if overrides.tag_ids is None:
|
if overrides.tag_ids is None:
|
||||||
overrides.tag_ids = []
|
overrides.tag_ids = []
|
||||||
overrides.tag_ids.extend(
|
overrides.tag_ids.extend(
|
||||||
list(action.assign_tags.all().values_list("pk", flat=True)),
|
action.assign_tags.values_list("pk", flat=True),
|
||||||
)
|
)
|
||||||
|
|
||||||
if action.assign_correspondent is not None:
|
if action.assign_correspondent:
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
document.correspondent = action.assign_correspondent
|
document.correspondent = action.assign_correspondent
|
||||||
else:
|
else:
|
||||||
overrides.correspondent_id = action.assign_correspondent.pk
|
overrides.correspondent_id = action.assign_correspondent.pk
|
||||||
|
|
||||||
if action.assign_document_type is not None:
|
if action.assign_document_type:
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
document.document_type = action.assign_document_type
|
document.document_type = action.assign_document_type
|
||||||
else:
|
else:
|
||||||
overrides.document_type_id = action.assign_document_type.pk
|
overrides.document_type_id = action.assign_document_type.pk
|
||||||
|
|
||||||
if action.assign_storage_path is not None:
|
if action.assign_storage_path:
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
document.storage_path = action.assign_storage_path
|
document.storage_path = action.assign_storage_path
|
||||||
else:
|
else:
|
||||||
overrides.storage_path_id = action.assign_storage_path.pk
|
overrides.storage_path_id = action.assign_storage_path.pk
|
||||||
|
|
||||||
if action.assign_owner is not None:
|
if action.assign_owner:
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
document.owner = action.assign_owner
|
document.owner = action.assign_owner
|
||||||
else:
|
else:
|
||||||
overrides.owner_id = action.assign_owner.pk
|
overrides.owner_id = action.assign_owner.pk
|
||||||
|
|
||||||
if action.assign_title is not None:
|
if action.assign_title:
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
try:
|
try:
|
||||||
document.title = parse_doc_title_w_placeholders(
|
document.title = parse_doc_title_w_placeholders(
|
||||||
action.assign_title,
|
action.assign_title,
|
||||||
(
|
document.correspondent.name if document.correspondent else "",
|
||||||
document.correspondent.name
|
document.document_type.name if document.document_type else "",
|
||||||
if document.correspondent is not None
|
document.owner.username if document.owner else "",
|
||||||
else ""
|
|
||||||
),
|
|
||||||
(
|
|
||||||
document.document_type.name
|
|
||||||
if document.document_type is not None
|
|
||||||
else ""
|
|
||||||
),
|
|
||||||
(document.owner.username if document.owner is not None else ""),
|
|
||||||
timezone.localtime(document.added),
|
timezone.localtime(document.added),
|
||||||
(
|
document.original_filename or "",
|
||||||
document.original_filename
|
|
||||||
if document.original_filename is not None
|
|
||||||
else ""
|
|
||||||
),
|
|
||||||
timezone.localtime(document.created),
|
timezone.localtime(document.created),
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
message = f"Error occurred parsing title assignment '{action.assign_title}', falling back to original"
|
message = f"Error occurred parsing title assignment '{action.assign_title}', falling back to original"
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
logger.exception(
|
logger.exception(message, extra={"group": logging_group})
|
||||||
message,
|
|
||||||
extra={"group": logging_group},
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
messages.append(message)
|
messages.append(message)
|
||||||
else:
|
else:
|
||||||
@ -618,27 +601,15 @@ def run_workflows(
|
|||||||
):
|
):
|
||||||
permissions = {
|
permissions = {
|
||||||
"view": {
|
"view": {
|
||||||
"users": action.assign_view_users.all().values_list(
|
"users": action.assign_view_users.values_list("id", flat=True)
|
||||||
"id",
|
|
||||||
flat=True,
|
|
||||||
)
|
|
||||||
or [],
|
or [],
|
||||||
"groups": action.assign_view_groups.all().values_list(
|
"groups": action.assign_view_groups.values_list("id", flat=True)
|
||||||
"id",
|
|
||||||
flat=True,
|
|
||||||
)
|
|
||||||
or [],
|
or [],
|
||||||
},
|
},
|
||||||
"change": {
|
"change": {
|
||||||
"users": action.assign_change_users.all().values_list(
|
"users": action.assign_change_users.values_list("id", flat=True)
|
||||||
"id",
|
|
||||||
flat=True,
|
|
||||||
)
|
|
||||||
or [],
|
or [],
|
||||||
"groups": action.assign_change_groups.all().values_list(
|
"groups": action.assign_change_groups.values_list("id", flat=True)
|
||||||
"id",
|
|
||||||
flat=True,
|
|
||||||
)
|
|
||||||
or [],
|
or [],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -674,16 +645,13 @@ def run_workflows(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
if action.assign_custom_fields is not None:
|
if action.assign_custom_fields.exists():
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
for field in action.assign_custom_fields.all():
|
for field in action.assign_custom_fields.all():
|
||||||
if (
|
if not CustomFieldInstance.objects.filter(
|
||||||
CustomFieldInstance.objects.filter(
|
|
||||||
field=field,
|
field=field,
|
||||||
document=document,
|
document=document,
|
||||||
).count()
|
).exists():
|
||||||
== 0
|
|
||||||
):
|
|
||||||
# can be triggered on existing docs, so only add the field if it doesn't already exist
|
# can be triggered on existing docs, so only add the field if it doesn't already exist
|
||||||
CustomFieldInstance.objects.create(
|
CustomFieldInstance.objects.create(
|
||||||
field=field,
|
field=field,
|
||||||
@ -691,7 +659,7 @@ def run_workflows(
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
overrides.custom_field_ids = list(
|
overrides.custom_field_ids = list(
|
||||||
action.assign_custom_fields.all().values_list("pk", flat=True),
|
action.assign_custom_fields.values_list("pk", flat=True),
|
||||||
)
|
)
|
||||||
|
|
||||||
def removal_action():
|
def removal_action():
|
||||||
@ -703,31 +671,27 @@ def run_workflows(
|
|||||||
else:
|
else:
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
for tag in action.remove_tags.filter(
|
for tag in action.remove_tags.filter(
|
||||||
pk__in=list(document.tags.values_list("pk", flat=True)),
|
pk__in=document.tags.values_list("pk", flat=True),
|
||||||
).all():
|
):
|
||||||
doc_tag_ids.remove(tag.pk)
|
doc_tag_ids.remove(tag.pk)
|
||||||
elif overrides.tag_ids:
|
elif overrides.tag_ids:
|
||||||
for tag in action.remove_tags.filter(
|
for tag in action.remove_tags.filter(pk__in=overrides.tag_ids):
|
||||||
pk__in=overrides.tag_ids,
|
|
||||||
):
|
|
||||||
overrides.tag_ids.remove(tag.pk)
|
overrides.tag_ids.remove(tag.pk)
|
||||||
|
|
||||||
if not use_overrides and (
|
if not use_overrides and (
|
||||||
action.remove_all_correspondents
|
action.remove_all_correspondents
|
||||||
or (
|
or (
|
||||||
document.correspondent
|
document.correspondent
|
||||||
and (
|
and action.remove_correspondents.filter(
|
||||||
action.remove_correspondents.filter(
|
|
||||||
pk=document.correspondent.pk,
|
pk=document.correspondent.pk,
|
||||||
).exists()
|
).exists()
|
||||||
)
|
)
|
||||||
)
|
|
||||||
):
|
):
|
||||||
document.correspondent = None
|
document.correspondent = None
|
||||||
elif use_overrides and (
|
elif use_overrides and (
|
||||||
action.remove_all_correspondents
|
action.remove_all_correspondents
|
||||||
or (
|
or (
|
||||||
overrides.correspondent_id is not None
|
overrides.correspondent_id
|
||||||
and action.remove_correspondents.filter(
|
and action.remove_correspondents.filter(
|
||||||
pk=overrides.correspondent_id,
|
pk=overrides.correspondent_id,
|
||||||
).exists()
|
).exists()
|
||||||
@ -739,18 +703,16 @@ def run_workflows(
|
|||||||
action.remove_all_document_types
|
action.remove_all_document_types
|
||||||
or (
|
or (
|
||||||
document.document_type
|
document.document_type
|
||||||
and (
|
and action.remove_document_types.filter(
|
||||||
action.remove_document_types.filter(
|
|
||||||
pk=document.document_type.pk,
|
pk=document.document_type.pk,
|
||||||
).exists()
|
).exists()
|
||||||
)
|
)
|
||||||
)
|
|
||||||
):
|
):
|
||||||
document.document_type = None
|
document.document_type = None
|
||||||
elif use_overrides and (
|
elif use_overrides and (
|
||||||
action.remove_all_document_types
|
action.remove_all_document_types
|
||||||
or (
|
or (
|
||||||
overrides.document_type_id is not None
|
overrides.document_type_id
|
||||||
and action.remove_document_types.filter(
|
and action.remove_document_types.filter(
|
||||||
pk=overrides.document_type_id,
|
pk=overrides.document_type_id,
|
||||||
).exists()
|
).exists()
|
||||||
@ -762,18 +724,16 @@ def run_workflows(
|
|||||||
action.remove_all_storage_paths
|
action.remove_all_storage_paths
|
||||||
or (
|
or (
|
||||||
document.storage_path
|
document.storage_path
|
||||||
and (
|
and action.remove_storage_paths.filter(
|
||||||
action.remove_storage_paths.filter(
|
|
||||||
pk=document.storage_path.pk,
|
pk=document.storage_path.pk,
|
||||||
).exists()
|
).exists()
|
||||||
)
|
)
|
||||||
)
|
|
||||||
):
|
):
|
||||||
document.storage_path = None
|
document.storage_path = None
|
||||||
elif use_overrides and (
|
elif use_overrides and (
|
||||||
action.remove_all_storage_paths
|
action.remove_all_storage_paths
|
||||||
or (
|
or (
|
||||||
overrides.storage_path_id is not None
|
overrides.storage_path_id
|
||||||
and action.remove_storage_paths.filter(
|
and action.remove_storage_paths.filter(
|
||||||
pk=overrides.storage_path_id,
|
pk=overrides.storage_path_id,
|
||||||
).exists()
|
).exists()
|
||||||
@ -785,31 +745,23 @@ def run_workflows(
|
|||||||
action.remove_all_owners
|
action.remove_all_owners
|
||||||
or (
|
or (
|
||||||
document.owner
|
document.owner
|
||||||
and (action.remove_owners.filter(pk=document.owner.pk).exists())
|
and action.remove_owners.filter(pk=document.owner.pk).exists()
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
document.owner = None
|
document.owner = None
|
||||||
elif use_overrides and (
|
elif use_overrides and (
|
||||||
action.remove_all_owners
|
action.remove_all_owners
|
||||||
or (
|
or (
|
||||||
overrides.owner_id is not None
|
overrides.owner_id
|
||||||
and action.remove_owners.filter(
|
and action.remove_owners.filter(pk=overrides.owner_id).exists()
|
||||||
pk=overrides.owner_id,
|
|
||||||
).exists()
|
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
overrides.owner_id = None
|
overrides.owner_id = None
|
||||||
|
|
||||||
if action.remove_all_permissions:
|
if action.remove_all_permissions:
|
||||||
permissions = {
|
permissions = {
|
||||||
"view": {
|
"view": {"users": [], "groups": []},
|
||||||
"users": [],
|
"change": {"users": [], "groups": []},
|
||||||
"groups": [],
|
|
||||||
},
|
|
||||||
"change": {
|
|
||||||
"users": [],
|
|
||||||
"groups": [],
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
set_permissions_for_object(
|
set_permissions_for_object(
|
||||||
@ -822,11 +774,13 @@ def run_workflows(
|
|||||||
overrides.view_groups = None
|
overrides.view_groups = None
|
||||||
overrides.change_users = None
|
overrides.change_users = None
|
||||||
overrides.change_groups = None
|
overrides.change_groups = None
|
||||||
elif (
|
elif any(
|
||||||
(action.remove_view_users.all().count() > 0)
|
[
|
||||||
or (action.remove_view_groups.all().count() > 0)
|
action.remove_view_users.exists(),
|
||||||
or (action.remove_change_users.all().count() > 0)
|
action.remove_view_groups.exists(),
|
||||||
or (action.remove_change_groups.all().count() > 0)
|
action.remove_change_users.exists(),
|
||||||
|
action.remove_change_groups.exists(),
|
||||||
|
],
|
||||||
):
|
):
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
for user in action.remove_view_users.all():
|
for user in action.remove_view_users.all():
|
||||||
@ -849,22 +803,22 @@ def run_workflows(
|
|||||||
):
|
):
|
||||||
overrides.change_users.remove(user.pk)
|
overrides.change_users.remove(user.pk)
|
||||||
if overrides.view_groups:
|
if overrides.view_groups:
|
||||||
for user in action.remove_view_groups.filter(
|
for group in action.remove_view_groups.filter(
|
||||||
pk__in=overrides.view_groups,
|
pk__in=overrides.view_groups,
|
||||||
):
|
):
|
||||||
overrides.view_groups.remove(user.pk)
|
overrides.view_groups.remove(group.pk)
|
||||||
if overrides.change_groups:
|
if overrides.change_groups:
|
||||||
for user in action.remove_change_groups.filter(
|
for group in action.remove_change_groups.filter(
|
||||||
pk__in=overrides.change_groups,
|
pk__in=overrides.change_groups,
|
||||||
):
|
):
|
||||||
overrides.change_groups.remove(user.pk)
|
overrides.change_groups.remove(group.pk)
|
||||||
|
|
||||||
if action.remove_all_custom_fields:
|
if action.remove_all_custom_fields:
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
CustomFieldInstance.objects.filter(document=document).delete()
|
CustomFieldInstance.objects.filter(document=document).delete()
|
||||||
else:
|
else:
|
||||||
overrides.custom_field_ids = None
|
overrides.custom_field_ids = None
|
||||||
elif action.remove_custom_fields.all().count() > 0:
|
elif action.remove_custom_fields.exists():
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
CustomFieldInstance.objects.filter(
|
CustomFieldInstance.objects.filter(
|
||||||
field__in=action.remove_custom_fields.all(),
|
field__in=action.remove_custom_fields.all(),
|
||||||
@ -879,53 +833,46 @@ def run_workflows(
|
|||||||
use_overrides = overrides is not None
|
use_overrides = overrides is not None
|
||||||
messages = []
|
messages = []
|
||||||
|
|
||||||
for workflow in (
|
workflows = (
|
||||||
Workflow.objects.filter(
|
Workflow.objects.filter(enabled=True, triggers__type=trigger_type)
|
||||||
enabled=True,
|
.prefetch_related(
|
||||||
triggers__type=trigger_type,
|
"actions",
|
||||||
|
"actions__assign_view_users",
|
||||||
|
"actions__assign_view_groups",
|
||||||
|
"actions__assign_change_users",
|
||||||
|
"actions__assign_change_groups",
|
||||||
|
"actions__assign_custom_fields",
|
||||||
|
"actions__remove_tags",
|
||||||
|
"actions__remove_correspondents",
|
||||||
|
"actions__remove_document_types",
|
||||||
|
"actions__remove_storage_paths",
|
||||||
|
"actions__remove_custom_fields",
|
||||||
|
"actions__remove_owners",
|
||||||
|
"triggers",
|
||||||
)
|
)
|
||||||
.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")
|
.order_by("order")
|
||||||
):
|
)
|
||||||
|
|
||||||
|
for workflow in workflows:
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
# This can be called from bulk_update_documents, which may be running multiple times
|
# This can be called from bulk_update_documents, which may be running multiple times
|
||||||
# Refresh this so the matching data is fresh and instance fields are re-freshed
|
# Refresh this so the matching data is fresh and instance fields are re-freshed
|
||||||
# Otherwise, this instance might be behind and overwrite the work another process did
|
# Otherwise, this instance might be behind and overwrite the work another process did
|
||||||
document.refresh_from_db()
|
document.refresh_from_db()
|
||||||
doc_tag_ids = list(document.tags.all().values_list("pk", flat=True))
|
doc_tag_ids = list(document.tags.values_list("pk", flat=True))
|
||||||
else:
|
else:
|
||||||
doc_tag_ids = overrides.tag_ids or []
|
doc_tag_ids = overrides.tag_ids or []
|
||||||
if matching.document_matches_workflow(
|
|
||||||
document,
|
if matching.document_matches_workflow(document, workflow, trigger_type):
|
||||||
workflow,
|
|
||||||
trigger_type,
|
|
||||||
):
|
|
||||||
action: WorkflowAction
|
|
||||||
for action in workflow.actions.all():
|
for action in workflow.actions.all():
|
||||||
message = f"Applying {action} from {workflow}"
|
message = f"Applying {action} from {workflow}"
|
||||||
if not use_overrides:
|
if not use_overrides:
|
||||||
logger.info(
|
logger.info(message, extra={"group": logging_group})
|
||||||
message,
|
|
||||||
extra={"group": logging_group},
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
messages.append(message)
|
messages.append(message)
|
||||||
|
|
||||||
if action.type == WorkflowAction.WorkflowActionType.ASSIGNMENT:
|
if action.type == WorkflowAction.WorkflowActionType.ASSIGNMENT:
|
||||||
assignment_action()
|
assignment_action()
|
||||||
|
|
||||||
elif action.type == WorkflowAction.WorkflowActionType.REMOVAL:
|
elif action.type == WorkflowAction.WorkflowActionType.REMOVAL:
|
||||||
removal_action()
|
removal_action()
|
||||||
|
|
||||||
@ -933,6 +880,7 @@ def run_workflows(
|
|||||||
# save first before setting tags
|
# save first before setting tags
|
||||||
document.save()
|
document.save()
|
||||||
document.tags.set(doc_tag_ids)
|
document.tags.set(doc_tag_ids)
|
||||||
|
|
||||||
if use_overrides:
|
if use_overrides:
|
||||||
return overrides, "\n".join(messages)
|
return overrides, "\n".join(messages)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user