fix/carga-csv-ergonomia
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
Rutas para descargar plantillas CSV generadas desde código (sin archivos XLS/XLSX).
|
||||
"""
|
||||
from typing import Any, Dict
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi.responses import Response
|
||||
|
||||
from core.security import get_current_user
|
||||
@@ -12,17 +12,23 @@ from .registry import generate_csv_content, get_template_filename
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
_LOCALE_ALLOWED = frozenset({"es", "en"})
|
||||
|
||||
|
||||
@router.get("/{template_id}", response_class=Response)
|
||||
async def download_csv_template(
|
||||
template_id: str,
|
||||
locale: Optional[str] = Query("es", description="es | en: idioma de las cabeceras del CSV"),
|
||||
current_user: Dict[str, Any] = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
Devuelve un CSV con solo la fila de cabeceras para la plantilla indicada.
|
||||
Las cabeceras son los nombres canónicos definidos en cada template_config.
|
||||
Query `locale`: es (defecto) o en — cabeceras localizadas cuando estén definidas.
|
||||
"""
|
||||
content = generate_csv_content(template_id, include_bom=True)
|
||||
loc = (locale or "es").lower().strip()
|
||||
if loc not in _LOCALE_ALLOWED:
|
||||
raise HTTPException(status_code=400, detail="locale must be 'es' or 'en'")
|
||||
content = generate_csv_content(template_id, locale=loc, include_bom=True)
|
||||
if content is None:
|
||||
raise HTTPException(status_code=404, detail=f"Plantilla desconocida: {template_id}")
|
||||
filename = get_template_filename(template_id)
|
||||
@@ -31,5 +37,7 @@ async def download_csv_template(
|
||||
media_type="text/csv; charset=utf-8",
|
||||
headers={
|
||||
"Content-Disposition": f'attachment; filename="{filename}"',
|
||||
"Cache-Control": "private, no-store, max-age=0, must-revalidate",
|
||||
"Pragma": "no-cache",
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user