first commit
This commit is contained in:
32
backend/app/routers/report.py
Normal file
32
backend/app/routers/report.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from app.models import get_db
|
||||
from app.services.report_generator import ReportGenerator
|
||||
|
||||
router = APIRouter(prefix="/report", tags=["Report"])
|
||||
|
||||
@router.get("/export")
|
||||
def export_report(format: str = "xlsx", db: Session = Depends(get_db)):
|
||||
"""匯出報表"""
|
||||
if format not in ['xlsx', 'pdf']:
|
||||
raise HTTPException(status_code=400, detail="Invalid format. Use 'xlsx' or 'pdf'")
|
||||
|
||||
generator = ReportGenerator(db)
|
||||
|
||||
if format == 'xlsx':
|
||||
output = generator.generate_excel()
|
||||
media_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
filename = "dit_attribution_report.xlsx"
|
||||
else:
|
||||
output = generator.generate_pdf()
|
||||
media_type = "application/pdf"
|
||||
filename = "dit_attribution_report.pdf"
|
||||
|
||||
return StreamingResponse(
|
||||
output,
|
||||
media_type=media_type,
|
||||
headers={
|
||||
"Content-Disposition": f"attachment; filename={filename}"
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user