fix: expose per-file cover identities in import scans

This commit is contained in:
2026-10-14 14:48:16 +08:00
parent c2dc9015ea
commit 0d6dc4b342
2 changed files with 29 additions and 2 deletions
+7 -2
View File
@@ -11,6 +11,7 @@ from .config import get_settings
CATEGORIES = ("stage1", "stage2", "stage3", "final_report", "presentation")
SUPPORTED = {".pdf", ".docx", ".pptx"}
NAMES = {"stage1": "阶段报告1", "stage2": "阶段报告2", "stage3": "阶段报告3", "final_report": "最终报告", "presentation": "汇报PPT"}
IDENTITY_LABELS = {"student_no": "学号", "name": "姓名", "topic": "选题"}
def secure_path(relative: str, *, root: Path | None = None, must_exist=True) -> Path:
@@ -114,6 +115,7 @@ def scan(relative: str, *, root: Path | None = None):
if item.is_dir() and not item.is_symlink() and has_materials(item)]
for folder in folders:
info = parse_student_folder(folder.name)
folder_identity = {key: info[key] for key in IDENTITY_LABELS}
files = []
extracted = {}
identity_values = {}
@@ -124,6 +126,7 @@ def scan(relative: str, *, root: Path | None = None):
if candidate.suffix.lower() not in SUPPORTED:
continue
error = ""
identity = {}
try:
text = first_text(candidate)
category, status = classify(candidate, text)
@@ -137,6 +140,7 @@ def scan(relative: str, *, root: Path | None = None):
files.append({"relative_path": candidate.relative_to(base).as_posix(), "category": category, "status": status,
"size": stat.st_size, "mtime": stat.st_mtime, "sha256": sha256(candidate),
"mime_type": mimetypes.guess_type(candidate.name)[0] or "application/octet-stream", "error": error,
"detected_identity": identity,
"first_text": text[:500] if not error else ""})
for category in CATEGORIES:
matches = [file for file in files if file["category"] == category]
@@ -145,17 +149,18 @@ def scan(relative: str, *, root: Path | None = None):
file["status"] = "duplicate"
for key, values in identity_values.items():
if len(values) > 1:
warnings.append(f"不同材料封面的{key}不一致,请核对后填写")
warnings.append(f"不同材料封面的{IDENTITY_LABELS[key]}不一致,请核对后填写")
else:
extracted[key] = next(iter(values))
for key, value in extracted.items():
if info.get(key) and value != info[key]:
warnings.append(f"封面{key}“{value}”与目录名“{info[key]}”不一致")
warnings.append(f"封面{IDENTITY_LABELS[key]}“{value}”与目录名“{info[key]}”不一致")
elif not info.get(key):
info[key] = value
if warnings:
info["status"] = "needs_confirmation"
info.update({"relative_dir": folder.relative_to(base).as_posix(), "files": files,
"folder_identity": folder_identity,
"detected_identity": extracted, "warnings": warnings,
"missing": [category for category in CATEGORIES if not any(file["category"] == category for file in files)]})
projects.append(info)