mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-17 19:31:32 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c242b66a0 |
No files matched your search
@@ -1,5 +1,6 @@
|
||||
"""Queued document bulk operations."""
|
||||
|
||||
from contextlib import nullcontext
|
||||
from typing import Any
|
||||
|
||||
from auditlog.context import set_actor
|
||||
@@ -112,6 +113,8 @@ def execute_bulk_operation(
|
||||
if operation == "run_workflow" and not user.has_perm("documents.view_workflow"):
|
||||
raise PermissionDenied("Insufficient permissions to run this workflow")
|
||||
|
||||
actor_context = set_actor(user) if settings.AUDIT_LOG_ENABLED else nullcontext()
|
||||
|
||||
if operation not in MODIFIED_FIELDS:
|
||||
execution_parameters = parameters.copy()
|
||||
if operation in USER_OPERATIONS:
|
||||
@@ -119,7 +122,7 @@ def execute_bulk_operation(
|
||||
if operation in TRIGGER_SOURCE_OPERATIONS:
|
||||
execution_parameters["trigger_source"] = trigger_source
|
||||
modified_field = LEGACY_MODIFIED_FIELDS.get(operation)
|
||||
with set_actor(user):
|
||||
with actor_context:
|
||||
old_values = (
|
||||
snapshot_field(document_ids, modified_field)
|
||||
if settings.AUDIT_LOG_ENABLED and modified_field
|
||||
@@ -146,7 +149,7 @@ def execute_bulk_operation(
|
||||
modified_field = MODIFIED_FIELDS[operation]
|
||||
# Keep metadata writes and their audit entries together. Follow-up processing
|
||||
# runs after commit, within this same tracked task.
|
||||
with set_actor(user), transaction.atomic():
|
||||
with actor_context, transaction.atomic():
|
||||
old_values = (
|
||||
snapshot_field(document_ids, modified_field)
|
||||
if settings.AUDIT_LOG_ENABLED and modified_field
|
||||
|
||||
@@ -123,6 +123,41 @@ def test_document_action_runs_with_user_in_worker(bulk_setup):
|
||||
assert result == {"operation": "rotate", "document_count": 1}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_delete_task_without_audit_log(bulk_setup, settings):
|
||||
user, _, doc, _ = bulk_setup
|
||||
settings.AUDIT_LOG_ENABLED = False
|
||||
with mock.patch(
|
||||
"documents.bulk_operations.set_actor",
|
||||
side_effect=AssertionError("audit context must not be entered"),
|
||||
):
|
||||
result = execute_bulk_operation("delete", [doc.pk], {}, user.pk)
|
||||
assert result == {"operation": "delete", "document_count": 1}
|
||||
assert not Document.objects.filter(pk=doc.pk).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_metadata_task_without_audit_log(bulk_setup, settings):
|
||||
user, _, doc, correspondent = bulk_setup
|
||||
settings.AUDIT_LOG_ENABLED = False
|
||||
with (
|
||||
mock.patch(
|
||||
"documents.bulk_operations.set_actor",
|
||||
side_effect=AssertionError("audit context must not be entered"),
|
||||
),
|
||||
mock.patch("documents.bulk_operations.bulk_update_documents"),
|
||||
):
|
||||
result = execute_bulk_operation(
|
||||
"set_correspondent",
|
||||
[doc.pk],
|
||||
{"correspondent": correspondent.pk},
|
||||
user.pk,
|
||||
)
|
||||
doc.refresh_from_db()
|
||||
assert doc.correspondent_id == correspondent.pk
|
||||
assert result == {"operation": "set_correspondent", "document_count": 1}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_document_action_queue_contains_only_serializable_parameters(bulk_setup):
|
||||
user, client, doc, _ = bulk_setup
|
||||
|
||||
Reference in new issue
Block a user