Chore: annotate the zone OCR views, signal and helpers

The OCR views, signal handler and regex helpers were untyped, which hid
dead path checks in the views. Annotate them and drop the dead checks.
This commit is contained in:
stumpylog committed 2026-11-19 13:46:52 -08:00
1 parent d4f435de7d
commit 8f1a6d21ce
5 files changed
+43 -23

No files matched your search

+1 -1
View File
@@ -165,7 +165,7 @@ def match_storage_paths(document: Document, classifier: DocumentClassifier, user
def matches(
matching_model: MatchingModel | WorkflowTrigger | OcrTemplate,
document: Document,
):
) -> bool:
search_flags = 0
document_content = document.get_effective_content() or ""
+7 -2
View File
@@ -39,7 +39,7 @@ def _compile_or_none(
flags: int,
*,
log: Callable[..., None],
) -> regex.Pattern | None:
) -> regex.Pattern[str] | None:
"""Validate and compile a pattern, logging and returning None on failure."""
try:
validate_regex_pattern(pattern)
@@ -93,7 +93,12 @@ def safe_regex_match(pattern: str, text: str, *, flags: int = 0):
return _run_with_timeout(pattern, "matching", compiled.match, text)
def safe_regex_fullmatch(pattern: str, text: str, *, flags: int = 0):
def safe_regex_fullmatch(
pattern: str,
text: str,
*,
flags: int = 0,
) -> regex.Match[str] | None:
"""
Run a regex full match with a timeout. Returns a match object or None.
Validation errors and timeouts are logged and treated as no match.
+6 -1
View File
@@ -1466,7 +1466,12 @@ def close_connection_pool_on_worker_init(**kwargs) -> None:
conn.close_pool()
def run_zone_ocr_extraction(sender, document, original_file=None, **kwargs):
def run_zone_ocr_extraction(
sender: Any,
document: Document,
original_file: Path | None = None,
**kwargs: Any,
) -> None:
"""Apply matching templates without failing consumption if extraction fails."""
try:
from documents.zone_ocr import run_zone_extraction
+22 -14
View File
@@ -10,6 +10,7 @@ from collections import deque
from collections.abc import AsyncIterator
from collections.abc import Callable
from collections.abc import Generator
from collections.abc import Sequence
from datetime import datetime
from datetime import timedelta
from http import HTTPStatus
@@ -22,6 +23,7 @@ from typing import TYPE_CHECKING
from typing import Any
from typing import Final
from typing import NamedTuple
from typing import cast
from unicodedata import normalize
from urllib.parse import quote
from urllib.parse import urlparse
@@ -2300,7 +2302,7 @@ class DocumentViewSet(
url_path="run-zone-ocr",
permission_classes=[IsAuthenticated],
)
def run_zone_ocr(self, request, pk=None):
def run_zone_ocr(self, request: Request, pk: str | None = None) -> HttpResponse:
"""
Run zone-based OCR extraction on this document, with the given template
or with every template that matches it.
@@ -2312,13 +2314,14 @@ class DocumentViewSet(
serializer.is_valid(raise_exception=True)
template = serializer.validated_data.get("template")
document = get_object_or_404(
document: Document = get_object_or_404(
Document.objects.select_related("root_document__owner"),
pk=pk,
)
if not request.user.has_perm(
user = cast("User", request.user)
if not user.has_perm(
"documents.change_document",
) or not has_perms_owner_aware(request.user, "change_document", document):
) or not has_perms_owner_aware(user, "change_document", document):
return HttpResponseForbidden("Insufficient permissions")
from documents.zone_ocr import get_templates_for_document
@@ -2334,7 +2337,7 @@ class DocumentViewSet(
templates = (
[template]
if template is not None
else get_templates_for_document(file_document, requester=request.user)
else get_templates_for_document(file_document, requester=user)
)
if not templates:
return Response(
@@ -2342,7 +2345,7 @@ class DocumentViewSet(
)
doc_path = file_document.archive_path or file_document.source_path
if not doc_path or not Path(doc_path).is_file():
if not doc_path.is_file():
return Response(
{"error": "Document file not found"},
status=status.HTTP_404_NOT_FOUND,
@@ -3491,7 +3494,7 @@ class ReprocessDocumentsView(DocumentOperationPermissionMixin):
class RunZoneOcrDocumentsView(DocumentOperationPermissionMixin):
serializer_class = RunZoneOcrDocumentsSerializer
def post(self, request, *args, **kwargs):
def post(self, request: Request, *args: Any, **kwargs: Any) -> HttpResponse:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
template = serializer.validated_data.get("template")
@@ -5829,7 +5832,7 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem
)
ordering_fields = ("name",)
def get_permissions(self):
def get_permissions(self) -> Sequence[Any]:
if self.action in {"document_page_image", "test_zone"}:
# Read-only previews: viewing templates is enough, even for the POST.
return [
@@ -5844,9 +5847,14 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem
methods=["get"],
url_path=r"document-page-image/(?P<doc_id>[0-9]+)/(?P<page>[0-9]+)",
)
def document_page_image(self, request, doc_id=None, page=None):
def document_page_image(
self,
request: Request,
doc_id: str,
page: str,
) -> HttpResponse:
"""Render a page for the template editor's zone overlay."""
document = get_object_or_404(
document: Document = get_object_or_404(
Document.objects.select_related("root_document__owner"),
pk=doc_id,
)
@@ -5897,7 +5905,7 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem
@extend_schema(request=OcrTemplateTestSerializer)
@action(detail=False, methods=["post"], url_path="test-zone")
def test_zone(self, request):
def test_zone(self, request: Request) -> HttpResponse:
"""Extract an ad-hoc zone without changing the document."""
from documents.zone_ocr import extract_zone_preview
@@ -5906,10 +5914,10 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem
context={"request": request},
)
serializer.is_valid(raise_exception=True)
document = serializer.validated_data["document"]
document: Document = serializer.validated_data["document"]
doc_path = document.archive_path or document.source_path
if not doc_path or not Path(doc_path).is_file():
if not doc_path.is_file():
return Response(
{"error": "Document file not found"},
status=status.HTTP_404_NOT_FOUND,
@@ -5918,7 +5926,7 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem
zone = OcrTemplateZone(**serializer.validated_data["zone"])
result = extract_zone_preview(
Path(doc_path),
doc_path,
zone,
zone.zone_source_width or 0,
zone.zone_source_height or 0,
+7 -5
View File
@@ -189,16 +189,18 @@ def get_templates_for_document(
"zones",
"zones__custom_field",
)
match_document = document
if document.root_document_id is None and templates:
# The original file can be selected even when newer versions exist.
document = (
annotated = (
Document.objects.filter(pk=document.pk)
.annotate(effective_content=F("content"))
.first()
)
if document is None:
if annotated is None:
return []
return [template for template in templates if matches(template, document)]
match_document = annotated
return [template for template in templates if matches(template, match_document)]
def run_zone_extraction(
@@ -277,7 +279,7 @@ def _resolve_doc_path(
original_file: Path | None,
) -> Path | None:
"""Prefer the archive, then the source, then the ingestion signal's file."""
candidates = []
candidates: list[Path | None] = []
if document.has_archive_version:
candidates.append(document.archive_path)
candidates.append(document.source_path)
@@ -924,7 +926,7 @@ def _parse_number(value: str, locale: Locale) -> Decimal | None:
else None
)
negative = sign is not None
if negative:
if sign is not None:
text = text[: sign.start()] + text[sign.end() :]
if re.search(r"\d\D*-|(?<!\w)-", text):
return None