From 13b543c99d81f2c243f993832b0e9127cebd98e7 Mon Sep 17 00:00:00 2001 From: bruce Date: Sun, 7 Jun 2026 20:14:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(application-form-fill):=20=E6=B8=85?= =?UTF-8?q?=E6=B4=97=E5=A1=AB=E8=A1=A8Word=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../services/word_fill.py | 3 +- tests/test_application_form_fill_word_fill.py | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/review_agent/application_form_fill/services/word_fill.py b/review_agent/application_form_fill/services/word_fill.py index 801f56a..195b918 100644 --- a/review_agent/application_form_fill/services/word_fill.py +++ b/review_agent/application_form_fill/services/word_fill.py @@ -107,5 +107,6 @@ def _normalize_label(value: str) -> str: def _safe_filename(value: str) -> str: - text = re.sub(r'[\\/:*?"<>|]+', "_", value or "") + text = re.sub(r"[\x00-\x1f\x7f]+", "", value or "") + text = re.sub(r'[\\/:*?"<>|]+', "_", text) return text.strip()[:80] or "output" diff --git a/tests/test_application_form_fill_word_fill.py b/tests/test_application_form_fill_word_fill.py index 264b716..04c918f 100644 --- a/tests/test_application_form_fill_word_fill.py +++ b/tests/test_application_form_fill_word_fill.py @@ -1,4 +1,5 @@ import zipfile +from pathlib import Path import pytest from docx import Document @@ -119,3 +120,31 @@ def test_create_word_export_records_artifact_and_export(settings, tmp_path, djan batch=batch, artifact_type=ApplicationFormFillArtifact.ArtifactType.FILLED_TEMPLATE, ).exists() + + +def test_create_word_export_sanitizes_product_name_newlines(settings, tmp_path, django_user_model): + settings.MEDIA_ROOT = tmp_path + user = django_user_model.objects.create_user(username="owner", password="pass") + conversation = Conversation.objects.create(user=user, title="会话") + summary = FileSummaryBatch.objects.create(conversation=conversation, user=user, batch_no="FS-WORD-NL") + batch = ApplicationFormFillBatch.objects.create( + conversation=conversation, + user=user, + source_summary_batch=summary, + batch_no="AFF-WORD-NL", + product_name="原体核酸检测试剂盒(荧\n光PCR法)", + work_dir=str(tmp_path / "aff" / "AFF-WORD-NL"), + ) + template_path = tmp_path / "template.docx" + _template(template_path) + + exported = create_word_export( + batch, + _spec(), + template_path, + {"product_name": MergedField("product_name", "产品名称", "原体核酸检测试剂盒", "说明书.txt", "证据", 0.8)}, + ) + + assert "\n" not in exported.file_name + assert "\r" not in exported.file_name + assert Path(exported.storage_path).exists()