Move run_zone_ocr to the new bulk operations pathway

This commit is contained in:
shamoon committed 2026-11-17 13:43:13 -08:00
1 parent 3153b118bd
commit 855b161407
13 files changed
+193 -47

No files matched your search

@@ -1387,6 +1387,10 @@ describe('BulkEditorComponent', () => {
})
it('should support run zone ocr', () => {
const queued = jest
.spyOn(TestBed.inject(TaskNotificationService), 'showQueued')
.mockImplementation(() => {})
const reload = jest.spyOn(documentListViewService, 'reload')
let modal: NgbModalRef
modalService.activeInstances.subscribe((m) => (modal = m[0]))
jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(true)
@@ -1406,10 +1410,17 @@ describe('BulkEditorComponent', () => {
let req = httpTestingController.expectOne(
`${environment.apiBaseUrl}documents/run_zone_ocr/`
)
req.flush(true)
req.flush({ result: 'OK', task_id: 'zone-ocr-1' })
expect(req.request.body).toEqual({
documents: [3, 4],
from_webui: true,
})
expect(queued).toHaveBeenLastCalledWith(
'zone-ocr-1',
"Bulk operation 'Run zone OCR' queued.",
{ content: "Bulk operation 'Run zone OCR' completed." }
)
expect(reload).not.toHaveBeenCalled()
component.runZoneOcrSelected()
modal.componentInstance.template = 3
@@ -1417,11 +1428,17 @@ describe('BulkEditorComponent', () => {
req = httpTestingController.expectOne(
`${environment.apiBaseUrl}documents/run_zone_ocr/`
)
req.flush(true)
req.flush({ result: 'OK', task_id: 'zone-ocr-2' })
expect(req.request.body).toEqual({
documents: [3, 4],
template: 3,
from_webui: true,
})
expect(queued).toHaveBeenLastCalledWith(
'zone-ocr-2',
"Bulk operation 'Run zone OCR' queued.",
{ content: "Bulk operation 'Run zone OCR' completed." }
)
httpTestingController.match(
`${environment.apiBaseUrl}ocr_templates/?page=1&page_size=100000`
) // template list
@@ -1042,6 +1042,7 @@ export class BulkEditorComponent
dialog.buttonsEnabled.set(false)
this.executeDocumentAction(
modal,
'run_zone_ocr',
this.documentService.runZoneOcrDocuments(
this.getSelectionQuery(),
dialog.template
@@ -323,6 +323,27 @@ describe(`DocumentService`, () => {
})
})
it.each([null, 3])('should queue zone OCR with template %s', (template) => {
const queued = jest.fn()
subscription = service
.runZoneOcrDocuments({ documents: [1, 2] }, template)
.subscribe(queued)
const req = httpTestingController.expectOne(
`${environment.apiBaseUrl}${endpoint}/run_zone_ocr/`
)
expect(req.request.method).toBe('POST')
expect(req.request.body).toEqual({
documents: [1, 2],
...(template ? { template } : {}),
from_webui: true,
})
req.flush({ result: 'OK', task_id: 'zone-ocr-task' })
expect(queued).toHaveBeenCalledWith({
result: 'OK',
task_id: 'zone-ocr-task',
})
})
it('should call appropriate api endpoint for rotate documents', () => {
const ids = [1, 2, 3]
subscription = service.rotateDocuments({ documents: ids }, 90).subscribe()
@@ -376,10 +376,14 @@ export class DocumentService extends AbstractPaperlessService<Document> {
selection: DocumentSelectionQuery,
templateId: number | null = null
) {
return this.http.post(this.getResourceUrl(null, 'run_zone_ocr'), {
...selection,
...(templateId ? { template: templateId } : {}),
})
return this.http.post<BulkOperationResponse>(
this.getResourceUrl(null, 'run_zone_ocr'),
{
...selection,
...(templateId ? { template: templateId } : {}),
from_webui: true,
}
)
}
runZoneOcr(
@@ -1,6 +1,13 @@
import { getDocumentOperationLabel } from './document-operation'
import {
documentOperationHasFollowUpTasks,
getDocumentOperationLabel,
} from './document-operation'
describe('Document Operation Utils', () => {
it('names zone OCR and treats task completion as finished extraction', () => {
expect(getDocumentOperationLabel('run_zone_ocr')).toBe('Run zone OCR')
expect(documentOperationHasFollowUpTasks('run_zone_ocr')).toBe(false)
})
it('should retrieve the document operation label correctly for an existing operation, null otherwise', () => {
const result = getDocumentOperationLabel('modify_custom_fields')
expect(result).toBe('Update custom fields')
@@ -10,6 +10,7 @@ const OPERATION_LABELS: Record<string, string> = {
delete: $localize`Move to trash`,
reprocess: $localize`Document reprocessing`,
run_workflow: $localize`Run workflow`,
run_zone_ocr: $localize`Run zone OCR`,
rotate: $localize`Rotate documents`,
merge: $localize`Merge documents`,
merge_as_versions: $localize`Merge as versions`,
+3 -11
View File
@@ -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"
+8
View File
@@ -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:
+1 -1
View File
@@ -1821,7 +1821,7 @@ class RunZoneOcrSerializer(serializers.Serializer):
class RunZoneOcrDocumentsSerializer(DocumentSelectionSerializer, RunZoneOcrSerializer):
pass
from_webui = serializers.BooleanField(required=False, default=False)
class BulkEditSerializer(
+1 -1
View File
@@ -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(
+9 -27
View File
@@ -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:
"""
+110
View File
@@ -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
+3
View File
@@ -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,