fix(file-summary): 修复报告导出下载

This commit is contained in:
2026-06-06 16:37:29 +08:00
parent 311eb1b129
commit b1a336d019
5 changed files with 21 additions and 3 deletions

View File

@@ -92,6 +92,8 @@ USE_TZ = True
STATIC_URL = "static/"
STATICFILES_DIRS = [BASE_DIR / "static"]
MEDIA_ROOT = BASE_DIR / "media"
MEDIA_URL = "media/"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

View File

@@ -2,13 +2,14 @@ from __future__ import annotations
from pathlib import Path
from django.conf import settings
from openpyxl import Workbook
from review_agent.models import ExportedSummaryFile, FileSummaryBatch
def _exports_dir(batch: FileSummaryBatch) -> Path:
root = Path(batch.work_dir or Path("media") / "file_summary" / batch.batch_no)
root = Path(batch.work_dir) if batch.work_dir else Path(settings.MEDIA_ROOT) / "file_summary" / batch.batch_no
export_dir = root / "exports"
export_dir.mkdir(parents=True, exist_ok=True)
return export_dir

View File

@@ -2,11 +2,13 @@ from __future__ import annotations
from pathlib import Path
from django.conf import settings
from review_agent.models import ExportedSummaryFile, FileSummaryBatch
def _exports_dir(batch: FileSummaryBatch) -> Path:
root = Path(batch.work_dir or Path("media") / "file_summary" / batch.batch_no)
root = Path(batch.work_dir) if batch.work_dir else Path(settings.MEDIA_ROOT) / "file_summary" / batch.batch_no
export_dir = root / "exports"
export_dir.mkdir(parents=True, exist_ok=True)
return export_dir

View File

@@ -121,4 +121,14 @@ def export_download(request, export_id: int):
path = Path(exported.storage_path)
if not path.exists():
return JsonResponse({"error": "文件不存在。"}, status=404)
return FileResponse(path.open("rb"), as_attachment=True, filename=exported.file_name)
content_type = (
"text/markdown; charset=utf-8"
if exported.export_type == ExportedSummaryFile.ExportType.MARKDOWN
else "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
return FileResponse(
path.open("rb"),
as_attachment=True,
filename=exported.file_name,
content_type=content_type,
)

View File

@@ -96,3 +96,6 @@ def test_export_download_requires_batch_owner(client, tmp_path, django_user_mode
client.force_login(owner)
allowed = client.get(reverse("file_summary_export_download", args=[exported.pk]))
assert allowed.status_code == 200
assert "attachment" in allowed["Content-Disposition"]
assert "summary.md" in allowed["Content-Disposition"]
assert allowed["Content-Type"].startswith("text/markdown")