30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
# app/api/v1/export.py
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from fastapi.responses import StreamingResponse
|
|
from sqlalchemy.orm import Session
|
|
from typing import Optional
|
|
|
|
from app.api import deps
|
|
from app.services.export_service import ExportService
|
|
|
|
router = APIRouter(prefix="/export", tags=["export"])
|
|
|
|
@router.get("/forms/{form_id}/csv")
|
|
async def export_form_csv(
|
|
form_id: int,
|
|
format: str = Query("csv", regex="^(csv|xlsx)$"),
|
|
db: Session = Depends(deps.get_db)
|
|
):
|
|
"""Экспорт данных формы в CSV или Excel"""
|
|
service = ExportService(db)
|
|
try:
|
|
file_content, filename, media_type = service.export_form_data(form_id, format)
|
|
return StreamingResponse(
|
|
iter([file_content]),
|
|
media_type=media_type,
|
|
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Export failed: {str(e)}") |