Backend for manual tempalte choice

This commit is contained in:
shamoon committed 2026-10-20 13:51:54 -07:00
1 parent eae392469f
commit bbdc016a32
8 files changed
+265 -17

No files matched your search

+60
View File
@@ -12,6 +12,7 @@ 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 StoragePath
from documents.models import Tag
from paperless_testing.dirs import DirectoriesMixin
@@ -605,6 +606,65 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
self.assertEqual(args[0], [self.doc1.id])
self.assertEqual(kwargs, {})
@mock.patch("documents.views.bulk_edit.run_zone_ocr")
def test_run_zone_ocr_documents_endpoint_with_template(self, m) -> None:
"""
GIVEN:
- API data to run a chosen OCR template on a document
WHEN:
- API is called
THEN:
- run_zone_ocr is called with the template
"""
self.setup_mock(m, "run_zone_ocr")
template = OcrTemplate.objects.create(
name="Bills",
source_width=100,
source_height=100,
)
response = self.client.post(
"/api/documents/run_zone_ocr/",
json.dumps({"documents": [self.doc1.id], "template": template.pk}),
content_type="application/json",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
m.assert_called_once()
args, kwargs = m.call_args
self.assertEqual(args[0], [self.doc1.id])
self.assertEqual(kwargs, {"template": template})
@mock.patch("documents.views.bulk_edit.run_zone_ocr")
def test_run_zone_ocr_documents_endpoint_rejects_private_template(
self,
m,
) -> None:
"""
GIVEN:
- A user who can change the documents but not view the template
WHEN:
- API is called with that template
THEN:
- The whole request is refused
"""
user = UserFactory(username="editor")
grant_global(user, "view_document", "change_document")
template = OcrTemplate.objects.create(
name="Private",
owner=UserFactory(username="other"),
source_width=100,
source_height=100,
)
self.client.force_authenticate(user=user)
response = self.client.post(
"/api/documents/run_zone_ocr/",
json.dumps({"documents": [self.doc1.id], "template": template.pk}),
content_type="application/json",
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
m.assert_not_called()
@mock.patch("documents.serialisers.bulk_edit.set_storage_path")
def test_api_set_storage_path(self, m) -> None:
"""
@@ -561,6 +561,73 @@ class TestOcrTemplatesAPI(DirectoriesMixin, APITestCase):
)
extract.assert_not_called()
@patch("documents.zone_ocr.run_zone_extraction")
def test_run_zone_ocr_applies_a_chosen_template(self, extract):
"""
GIVEN:
- A disabled template that does not match the document
WHEN:
- Zone OCR is run on the document with that template chosen
THEN:
- The template is applied anyway, since it was chosen explicitly
"""
document = DocumentFactory(content=CONTENT, mime_type="application/pdf")
document.source_path.write_bytes(b"test PDF")
template = OcrTemplate.objects.create(
name="Manual only",
match="Contoso",
enabled=False,
source_width=1000,
source_height=1000,
)
response = self.client.post(
f"/api/documents/{document.pk}/run-zone-ocr/",
{"template": template.pk},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["status"], "completed")
extract.assert_called_once_with(document, None, templates=[template])
@patch("documents.zone_ocr.run_zone_extraction")
def test_run_zone_ocr_rejects_a_template_the_user_cannot_view(self, extract):
user = User.objects.create_user(username="editor")
user.user_permissions.add(
*Permission.objects.filter(
codename__in=["view_document", "change_document"],
),
)
document = DocumentFactory(content=CONTENT, owner=user)
template = OcrTemplate.objects.create(
name="Private",
owner=User.objects.create_user(username="other"),
source_width=1000,
source_height=1000,
)
self.client.force_authenticate(user=user)
response = self.client.post(
f"/api/documents/{document.pk}/run-zone-ocr/",
{"template": template.pk},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
extract.assert_not_called()
def test_run_zone_ocr_rejects_an_unknown_template(self):
document = DocumentFactory(content=CONTENT)
response = self.client.post(
f"/api/documents/{document.pk}/run-zone-ocr/",
{"template": 999},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_document_page_image_requires_view_permission_on_the_document(self):
owner = User.objects.create_user(username="owner")
user = User.objects.create_user(username="template_editor")
+63 -3
View File
@@ -21,6 +21,7 @@ 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 StoragePath
from documents.models import Tag
from documents.permissions import set_permissions_for_objects
@@ -1999,9 +2000,31 @@ class TestBulkEditRunZoneOcr:
assert result == "OK"
assert [
call.kwargs["kwargs"]["document_id"]
for call in mock_task.apply_async.call_args_list
] == [doc.id for doc in docs]
call.kwargs["kwargs"] for call in mock_task.apply_async.call_args_list
] == [{"document_id": doc.id, "template_id": None} for doc in docs]
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
"""
doc = DocumentFactory()
template = OcrTemplate.objects.create(
name="Bills",
source_width=100,
source_height=100,
)
bulk_edit.run_zone_ocr([doc.id], template=template)
assert mock_task.apply_async.call_args.kwargs["kwargs"] == {
"document_id": doc.id,
"template_id": template.pk,
}
def test_task_runs_extraction(self, mocker: MockerFixture) -> None:
"""
@@ -2019,6 +2042,43 @@ class TestBulkEditRunZoneOcr:
extract.assert_called_once_with(doc, None)
def test_task_runs_the_chosen_template(self, mocker: MockerFixture) -> None:
"""
GIVEN:
- A queued zone OCR task with a chosen template
WHEN:
- The task runs
THEN:
- Zone extraction runs with only that template
"""
extract = mocker.patch("documents.tasks.run_zone_extraction")
doc = DocumentFactory()
template = OcrTemplate.objects.create(
name="Bills",
source_width=100,
source_height=100,
)
extract_zones(doc.id, template.pk)
extract.assert_called_once_with(doc, None, templates=[template])
def test_task_skips_a_deleted_template(self, mocker: MockerFixture) -> None:
"""
GIVEN:
- A template deleted between queueing and running
WHEN:
- The task runs
THEN:
- Nothing is extracted
"""
extract = mocker.patch("documents.tasks.run_zone_extraction")
doc = DocumentFactory()
extract_zones(doc.id, 999)
extract.assert_not_called()
@pytest.mark.django_db
class TestBulkEditReprocess: