mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-19 20:25:21 +00:00
Move run_zone_ocr to the new bulk operations pathway
This commit is contained in:
13 files changed
+193
-47
No files matched your search
@@ -33,7 +33,6 @@ from documents.models import CustomField
|
||||
from documents.models import CustomFieldInstance
|
||||
from documents.models import Document
|
||||
from documents.models import DocumentType
|
||||
from documents.models import OcrTemplate
|
||||
from documents.models import PaperlessTask
|
||||
from documents.models import StoragePath
|
||||
from documents.models import Tag
|
||||
@@ -1262,17 +1261,10 @@ def remove_doclink(
|
||||
def run_zone_ocr(
|
||||
doc_ids: list[int],
|
||||
*,
|
||||
template: OcrTemplate | None = None,
|
||||
template: int | None = None,
|
||||
) -> Literal["OK"]:
|
||||
"""
|
||||
Queue zone OCR for the given documents, with the given template or with the
|
||||
templates that match each document's content when its task runs.
|
||||
"""
|
||||
template_id = template.pk if template is not None else None
|
||||
"""Run zone OCR within the tracked bulk operation task."""
|
||||
for document_id in doc_ids:
|
||||
extract_zones.apply_async(
|
||||
kwargs={"document_id": document_id, "template_id": template_id},
|
||||
headers={"trigger_source": PaperlessTask.TriggerSource.MANUAL},
|
||||
)
|
||||
extract_zones(document_id=document_id, template_id=template)
|
||||
|
||||
return "OK"
|
||||
@@ -13,8 +13,10 @@ from rest_framework.exceptions import PermissionDenied
|
||||
from documents import bulk_edit
|
||||
from documents.models import CustomFieldInstance
|
||||
from documents.models import Document
|
||||
from documents.models import OcrTemplate
|
||||
from documents.models import PaperlessTask
|
||||
from documents.permissions import has_document_operation_permissions
|
||||
from documents.permissions import has_perms_owner_aware
|
||||
from documents.tasks import bulk_update_documents
|
||||
|
||||
MODIFIED_FIELDS = {
|
||||
@@ -34,6 +36,7 @@ QUEUED_OPERATIONS = frozenset(
|
||||
"delete",
|
||||
"reprocess",
|
||||
"run_workflow",
|
||||
"run_zone_ocr",
|
||||
"rotate",
|
||||
"merge",
|
||||
"merge_as_versions",
|
||||
@@ -113,6 +116,11 @@ def execute_bulk_operation(
|
||||
if operation == "run_workflow" and not user.has_perm("documents.view_workflow"):
|
||||
raise PermissionDenied("Insufficient permissions to run this workflow")
|
||||
|
||||
if operation == "run_zone_ocr" and parameters.get("template") is not None:
|
||||
template = OcrTemplate.objects.get(pk=parameters["template"])
|
||||
if not has_perms_owner_aware(user, "view_ocrtemplate", template):
|
||||
raise PermissionDenied("Insufficient permissions to run this OCR template")
|
||||
|
||||
actor_context = set_actor(user) if settings.AUDIT_LOG_ENABLED else nullcontext()
|
||||
|
||||
if operation not in MODIFIED_FIELDS:
|
||||
|
||||
@@ -1821,7 +1821,7 @@ class RunZoneOcrSerializer(serializers.Serializer):
|
||||
|
||||
|
||||
class RunZoneOcrDocumentsSerializer(DocumentSelectionSerializer, RunZoneOcrSerializer):
|
||||
pass
|
||||
from_webui = serializers.BooleanField(required=False, default=False)
|
||||
|
||||
|
||||
class BulkEditSerializer(
|
||||
|
||||
@@ -780,7 +780,7 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
|
||||
m.assert_called_once()
|
||||
args, kwargs = m.call_args
|
||||
self.assertEqual(args[0], [self.doc1.id])
|
||||
self.assertEqual(kwargs, {"template": template})
|
||||
self.assertEqual(kwargs, {"template": template.pk})
|
||||
|
||||
@mock.patch("documents.views.bulk_edit.run_zone_ocr")
|
||||
def test_run_zone_ocr_documents_endpoint_rejects_private_template(
|
||||
|
||||
@@ -1942,37 +1942,21 @@ class TestBulkEditRunZoneOcr:
|
||||
def mock_task(self, mocker: MockerFixture) -> mock.MagicMock:
|
||||
return mocker.patch("documents.bulk_edit.extract_zones")
|
||||
|
||||
def test_queues_every_selected_document(
|
||||
def test_runs_every_selected_document_in_the_current_task(
|
||||
self,
|
||||
mock_task: mock.MagicMock,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- Selected documents
|
||||
WHEN:
|
||||
- run_zone_ocr is called with them
|
||||
THEN:
|
||||
- Each document is queued once, templates are matched when its
|
||||
task runs
|
||||
"""
|
||||
docs = DocumentFactory.create_batch(2)
|
||||
|
||||
result = bulk_edit.run_zone_ocr([doc.id for doc in docs])
|
||||
|
||||
assert result == "OK"
|
||||
assert [
|
||||
call.kwargs["kwargs"] for call in mock_task.apply_async.call_args_list
|
||||
] == [{"document_id": doc.id, "template_id": None} for doc in docs]
|
||||
assert mock_task.call_args_list == [
|
||||
mock.call(document_id=doc.id, template_id=None) for doc in docs
|
||||
]
|
||||
mock_task.apply_async.assert_not_called()
|
||||
|
||||
def test_queues_the_chosen_template(self, mock_task: mock.MagicMock) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A chosen OCR template
|
||||
WHEN:
|
||||
- run_zone_ocr is called with it
|
||||
THEN:
|
||||
- The template is queued with the document
|
||||
"""
|
||||
def test_runs_the_chosen_template(self, mock_task: mock.MagicMock) -> None:
|
||||
doc = DocumentFactory()
|
||||
template = OcrTemplate.objects.create(
|
||||
name="Bills",
|
||||
@@ -1980,12 +1964,10 @@ class TestBulkEditRunZoneOcr:
|
||||
source_height=100,
|
||||
)
|
||||
|
||||
bulk_edit.run_zone_ocr([doc.id], template=template)
|
||||
bulk_edit.run_zone_ocr([doc.id], template=template.pk)
|
||||
|
||||
assert mock_task.apply_async.call_args.kwargs["kwargs"] == {
|
||||
"document_id": doc.id,
|
||||
"template_id": template.pk,
|
||||
}
|
||||
mock_task.assert_called_once_with(document_id=doc.id, template_id=template.pk)
|
||||
mock_task.apply_async.assert_not_called()
|
||||
|
||||
def test_task_runs_extraction(self, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Request acceptance, worker execution, and task tracking."""
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from unittest import mock
|
||||
|
||||
@@ -12,12 +13,14 @@ from rest_framework.test import APIClient
|
||||
from documents.bulk_operations import execute_bulk_operation
|
||||
from documents.models import Correspondent
|
||||
from documents.models import Document
|
||||
from documents.models import OcrTemplate
|
||||
from documents.models import PaperlessTask
|
||||
from documents.signals.handlers import before_task_publish_handler
|
||||
from documents.signals.handlers import task_failure_handler
|
||||
from documents.signals.handlers import task_postrun_handler
|
||||
from documents.signals.handlers import task_revoked_handler
|
||||
from paperless_testing.factories import UserFactory
|
||||
from paperless_testing.permissions import grant_global
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -35,6 +38,7 @@ def bulk_setup(paperless_dirs):
|
||||
"endpoint,operation,parameters",
|
||||
[
|
||||
("bulk_edit", "set_correspondent", {"correspondent": None}),
|
||||
("run_zone_ocr", "run_zone_ocr", {}),
|
||||
],
|
||||
)
|
||||
def test_request_only_queues(bulk_setup, endpoint, operation, parameters):
|
||||
@@ -43,6 +47,8 @@ def test_request_only_queues(bulk_setup, endpoint, operation, parameters):
|
||||
data = {"documents": [doc.pk], "from_webui": True}
|
||||
if endpoint == "bulk_edit":
|
||||
data.update(method=operation, parameters=parameters)
|
||||
else:
|
||||
data.update(parameters)
|
||||
with mock.patch("documents.views.execute_bulk_operation.apply_async") as enqueue:
|
||||
enqueue.return_value.id = task_id
|
||||
response = client.post(f"/api/documents/{endpoint}/", data, format="json")
|
||||
@@ -59,6 +65,110 @@ def test_request_only_queues(bulk_setup, endpoint, operation, parameters):
|
||||
assert enqueue.call_args.kwargs["headers"]["trigger_source"] == "web_ui"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_zone_ocr_queues_only_template_id_and_returns_tracked_task(bulk_setup):
|
||||
user, client, doc, _ = bulk_setup
|
||||
template = OcrTemplate.objects.create(
|
||||
name="Template",
|
||||
source_width=100,
|
||||
source_height=100,
|
||||
)
|
||||
with (
|
||||
mock.patch("documents.views.execute_bulk_operation.apply_async") as enqueue,
|
||||
mock.patch("documents.bulk_edit.extract_zones") as extract,
|
||||
):
|
||||
enqueue.return_value.id = "zone-ocr-task"
|
||||
response = client.post(
|
||||
"/api/documents/run_zone_ocr/",
|
||||
{
|
||||
"documents": [doc.pk],
|
||||
"template": template.pk,
|
||||
"from_webui": True,
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.data == {"result": "OK", "task_id": "zone-ocr-task"}
|
||||
kwargs = enqueue.call_args.kwargs["kwargs"]
|
||||
assert json.loads(json.dumps(kwargs)) == {
|
||||
"operation": "run_zone_ocr",
|
||||
"document_ids": [doc.pk],
|
||||
"parameters": {"template": template.pk},
|
||||
"user_id": user.pk,
|
||||
"trigger_source": "web_ui",
|
||||
}
|
||||
extract.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_zone_ocr_finishes_extraction_within_the_tracked_task(bulk_setup):
|
||||
user, _, doc, _ = bulk_setup
|
||||
template = OcrTemplate.objects.create(
|
||||
name="Template",
|
||||
source_width=100,
|
||||
source_height=100,
|
||||
)
|
||||
with mock.patch("documents.bulk_edit.extract_zones") as extract:
|
||||
result = execute_bulk_operation(
|
||||
"run_zone_ocr",
|
||||
[doc.pk],
|
||||
{"template": template.pk},
|
||||
user.pk,
|
||||
)
|
||||
extract.assert_called_once_with(document_id=doc.pk, template_id=template.pk)
|
||||
extract.apply_async.assert_not_called()
|
||||
assert result == {"operation": "run_zone_ocr", "document_count": 1}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.parametrize("revoked", ["document", "template", "user"])
|
||||
def test_zone_ocr_rechecks_access_before_extraction(bulk_setup, revoked):
|
||||
_, client, doc, _ = bulk_setup
|
||||
user = UserFactory()
|
||||
grant_global(user, "change_document")
|
||||
template = OcrTemplate.objects.create(
|
||||
name="Template",
|
||||
owner=user,
|
||||
source_width=100,
|
||||
source_height=100,
|
||||
)
|
||||
client.force_authenticate(user)
|
||||
with mock.patch("documents.views.execute_bulk_operation.apply_async") as enqueue:
|
||||
enqueue.return_value.id = "zone-ocr-task"
|
||||
response = client.post(
|
||||
"/api/documents/run_zone_ocr/",
|
||||
{
|
||||
"documents": [doc.pk],
|
||||
"template": template.pk,
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
assert response.status_code == 200
|
||||
if revoked == "document":
|
||||
doc.owner = UserFactory()
|
||||
doc.save(update_fields=["owner"])
|
||||
elif revoked == "template":
|
||||
template.owner = UserFactory()
|
||||
template.save(update_fields=["owner"])
|
||||
else:
|
||||
User.objects.filter(pk=user.pk).update(is_active=False)
|
||||
with mock.patch("documents.bulk_edit.extract_zones") as extract:
|
||||
with pytest.raises(PermissionDenied):
|
||||
execute_bulk_operation(**enqueue.call_args.kwargs["kwargs"])
|
||||
extract.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_zone_ocr_cannot_fall_back_to_matching_if_chosen_template_is_deleted(
|
||||
bulk_setup,
|
||||
):
|
||||
user, _, doc, _ = bulk_setup
|
||||
with mock.patch("documents.bulk_edit.extract_zones") as extract:
|
||||
with pytest.raises(OcrTemplate.DoesNotExist):
|
||||
execute_bulk_operation("run_zone_ocr", [doc.pk], {"template": 999}, user.pk)
|
||||
extract.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_metadata_task_includes_audit_and_followup(bulk_setup, settings):
|
||||
user, _, doc, correspondent = bulk_setup
|
||||
|
||||
@@ -3481,6 +3481,7 @@ class ReprocessDocumentsView(DocumentOperationPermissionMixin):
|
||||
name="RunZoneOcrDocumentsResult",
|
||||
fields={
|
||||
"result": serializers.CharField(),
|
||||
"task_id": serializers.CharField(),
|
||||
},
|
||||
),
|
||||
},
|
||||
@@ -3499,6 +3500,8 @@ class RunZoneOcrDocumentsView(DocumentOperationPermissionMixin):
|
||||
template,
|
||||
):
|
||||
return HttpResponseForbidden("Insufficient permissions")
|
||||
if template is not None:
|
||||
serializer.validated_data["template"] = template.pk
|
||||
return self._execute_document_action(
|
||||
method=bulk_edit.run_zone_ocr,
|
||||
validated_data=serializer.validated_data,
|
||||
|
||||
Reference in new issue
Block a user