chore: make whoosh imports lazy to unblock test collection during Tantivy migration

Module-level whoosh imports in tasks.py and paperless/views.py prevented
test collection after removing whoosh-reloaded. Move to lazy imports inside
the functions that use them; will be removed entirely in Task 14.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Trenton H
2026-03-29 14:37:28 -07:00
parent 494d17e7ac
commit 2cf85d9b58
2 changed files with 16 additions and 3 deletions
+14 -2
View File
@@ -20,9 +20,7 @@ from django.db import transaction
from django.db.models.signals import post_save
from django.utils import timezone
from filelock import FileLock
from whoosh.writing import AsyncWriter
from documents import index
from documents import sanity_checker
from documents.barcodes import BarcodePlugin
from documents.bulk_download import ArchiveOnlyStrategy
@@ -84,12 +82,20 @@ def _identity(iterable: Iterable[_T]) -> Iterable[_T]:
@shared_task
def index_optimize() -> None:
from whoosh.writing import AsyncWriter
from documents import index
ix = index.open_index()
writer = AsyncWriter(ix)
writer.commit(optimize=True)
def index_reindex(*, iter_wrapper: IterWrapper[Document] = _identity) -> None:
from whoosh.writing import AsyncWriter
from documents import index
documents = Document.objects.all()
ix = index.open_index(recreate=True)
@@ -270,6 +276,10 @@ def sanity_check(*, scheduled=True, raise_on_error=True):
@shared_task
def bulk_update_documents(document_ids) -> None:
from whoosh.writing import AsyncWriter
from documents import index
documents = Document.objects.filter(id__in=document_ids)
ix = index.open_index()
@@ -300,6 +310,8 @@ def update_document_content_maybe_archive_file(document_id) -> None:
Re-creates OCR content and thumbnail for a document, and archive file if
it exists.
"""
from documents import index
document = Document.objects.get(id=document_id)
mime_type = document.mime_type
+2 -1
View File
@@ -35,7 +35,6 @@ from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from documents.index import DelayedQuery
from documents.permissions import PaperlessObjectPermissions
from documents.tasks import llmindex_index
from paperless.filters import GroupFilterSet
@@ -72,6 +71,8 @@ class StandardPagination(PageNumberPagination):
)
def get_all_result_ids(self):
from documents.index import DelayedQuery # removed with Whoosh in Task 14
query = self.page.paginator.object_list
if isinstance(query, DelayedQuery):
try: