Fix: let documents without an owner use every OCR template

Unowned documents only matched unowned templates, so they never matched
templates created in the UI. Consider every template, as matching does.
This commit is contained in:
stumpylog committed 2026-11-19 08:43:29 -08:00
1 parent 0117fed721
commit 0f4a71ed88
2 files changed
+25 -11

No files matched your search

+15 -4
View File
@@ -1534,22 +1534,33 @@ class TestTemplateVisibility:
assert private in get_templates_for_document(document)
@pytest.mark.usefixtures("mine", "private")
def test_unowned_documents_only_use_unowned_templates(
def test_unowned_documents_consider_every_template(
self,
other: User,
document: Document,
mine: OcrTemplate,
unowned: OcrTemplate,
private: OcrTemplate,
) -> None:
"""
GIVEN:
- A document with no owner
- Templates owned by two users and an unowned template
WHEN:
- Templates are selected for the document
- Templates are selected for consumption, and for a manual run by
one of the users
THEN:
- Only unowned templates apply
- Consumption considers every template
- The manual run is limited to templates the requester can view
"""
document.owner = None
assert get_templates_for_document(document) == [unowned]
assert Counter(get_templates_for_document(document)) == Counter(
[mine, unowned, private],
)
assert Counter(
get_templates_for_document(document, requester=other),
) == Counter([unowned, private])
@pytest.mark.usefixtures("private")
def test_versions_use_the_root_documents_owner(
+10 -7
View File
@@ -128,15 +128,18 @@ def get_templates_for_document(
) -> list[OcrTemplate]:
"""
The enabled templates that match the selected file's content and that the
root document's owner can view. For a manual run, the templates are further
limited to those the requester can view, so a user editing someone else's
document never runs or sees that owner's private templates.
root document's owner can view. A document without an owner considers every
template, as matching does for tags, correspondents and the like. For a
manual run, the templates are further limited to those the requester can
view, so a user editing someone else's document never runs or sees that
owner's private templates.
"""
owner = get_root_document(document).owner
templates = OcrTemplate.objects.filter(
enabled=True,
id__in=permitted_object_ids(owner, OcrTemplate, "view_ocrtemplate"),
)
templates = OcrTemplate.objects.filter(enabled=True)
if owner is not None:
templates = templates.filter(
id__in=permitted_object_ids(owner, OcrTemplate, "view_ocrtemplate"),
)
if requester is not None:
templates = templates.filter(
id__in=permitted_object_ids(requester, OcrTemplate, "view_ocrtemplate"),