Fix: drop only the ASN when a zone's ASN is already in use

A duplicate ASN failed the save and rolled back every write of the template.
Check first and report the zone as duplicate_asn instead.
This commit is contained in:
stumpylog committed 2026-11-19 08:06:08 -08:00
1 parent 3504ad07c6
commit 7b25551e1e
2 files changed
+108 -2

No files matched your search

+86 -1
View File
@@ -920,6 +920,7 @@ class TestSavedZoneExtraction:
def test_failed_template_reports_rollback_and_next_template_still_runs(
self,
mocker: MockerFixture,
mock_zone_ocr_run: MagicMock,
page: Path,
document: Document,
@@ -927,7 +928,8 @@ class TestSavedZoneExtraction:
) -> None:
"""
GIVEN:
- A template whose ASN zone conflicts with another document's ASN
- A template whose ASN is taken by another document after the
duplicate check passed, so saving it fails
- A second template that would succeed
WHEN:
- Zone extraction runs over both templates
@@ -935,6 +937,7 @@ class TestSavedZoneExtraction:
- The first template is reported as failed and rolled back, and the
second template is still applied
"""
mocker.patch("documents.zone_ocr._asn_in_use", return_value=False)
DocumentFactory(archive_serial_number=42)
make_zone(template, "Title")
make_zone(template, "Conflict", "asn")
@@ -957,6 +960,88 @@ class TestSavedZoneExtraction:
assert document.title == "Applied"
assert document.archive_serial_number is None
@pytest.mark.parametrize(
"deleted",
[
pytest.param(False, id="active-document"),
pytest.param(True, id="trashed-document"),
],
)
def test_duplicate_asn_is_dropped_and_other_fields_still_written(
self,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
template: OcrTemplate,
deleted: bool, # noqa: FBT001
) -> None:
"""
GIVEN:
- A template with a title zone and an ASN zone whose value is the
ASN of another document, active or in the trash
WHEN:
- The template is processed
THEN:
- The ASN zone is reported as duplicate_asn and the ASN is not
written, while the title from the same template is written
"""
other = DocumentFactory(archive_serial_number=42)
if deleted:
other.delete()
make_zone(template, "Title")
make_zone(template, "ASN", "asn")
mock_zone_ocr_run.side_effect = ocr_outputs("New title", "42")
results = _process_template(
document,
{0: page},
template,
list(template.zones.all()),
paperless_dirs.scratch_dir,
)
assert [(result.zone, result.value, result.reason) for result in results] == [
("Title", "New title", None),
("ASN", None, "duplicate_asn"),
]
document.refresh_from_db()
assert document.title == "New title"
assert document.archive_serial_number is None
def test_document_keeping_its_own_asn_is_not_a_duplicate(
self,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
template: OcrTemplate,
) -> None:
"""
GIVEN:
- A document that already has ASN 42 and an ASN zone reading 42
WHEN:
- The template is processed
THEN:
- The ASN is accepted
"""
document.archive_serial_number = 42
document.save()
make_zone(template, "ASN", "asn")
mock_zone_ocr_run.side_effect = ocr_outputs("42")
results = _process_template(
document,
{0: page},
template,
list(template.zones.all()),
paperless_dirs.scratch_dir,
)
assert [(result.value, result.reason) for result in results] == [(42, None)]
document.refresh_from_db()
assert document.archive_serial_number == 42
def test_literal_format_does_not_write_when_every_zone_failed(
self,
mock_zone_ocr_run: MagicMock,
+22 -1
View File
@@ -293,6 +293,7 @@ def _process_template(
grouped.setdefault(_field_key(zone), []).append((zone, result))
updates = []
root_document = get_root_document(document)
combine_formats = template.combine_formats or {}
for key, field_zones in grouped.items():
accepted = [result for _, result in field_zones if result.reason is None]
@@ -310,6 +311,13 @@ def _process_template(
)
target_zone = field_zones[0][0]
update = _prepare_field_update(target_zone, value) if value else None
if update is not None and _asn_in_use(update, root_document):
# Saving would violate the unique ASN and roll back every other
# write of this template, so only this field is dropped.
for result in accepted:
result.value = None
result.reason = "duplicate_asn"
continue
if update is not None:
updates.append(update)
for result in accepted:
@@ -318,10 +326,23 @@ def _process_template(
result.reason = (
"unassigned" if key == "custom_field" else "invalid_value"
)
_apply_field_updates(get_root_document(document), updates)
_apply_field_updates(root_document, updates)
return results
def _asn_in_use(update: FieldUpdate, root_document: Document) -> bool:
"""Whether an ASN update collides with another document's ASN.
Trashed documents count, matching the database constraint.
"""
return (
update.field_name == "archive_serial_number"
and Document.global_objects.filter(archive_serial_number=update.value)
.exclude(pk=root_document.pk)
.exists()
)
def _field_key(zone: OcrTemplateZone) -> str:
"""Use the same target keys as the editor's combination formats."""
target = zone.target