""" Rutas para descargar plantillas CSV generadas desde código (sin archivos XLS/XLSX). """ from typing import Any, Dict, Optional from fastapi import APIRouter, Depends, HTTPException, Query from fastapi.responses import Response from core.security import get_current_user 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. Query `locale`: es (defecto) o en — cabeceras localizadas cuando estén definidas. """ 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) return Response( content=content, 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", }, )