"""Printable list of the Doctors & Chemists directory. The browser sends the rows it already shows (the user's own scope, filters and search), so this prints nothing the user cannot already see; it only lays them out on the company letterhead.""" from fastapi import APIRouter, Depends, Response from pydantic import BaseModel, Field from app.auth.dependencies import get_current_user from app.pdf.service import csv_table_pdf_bytes from app.users.models import User router = APIRouter(prefix="/directory", tags=["directory"]) class DirectoryPdf(BaseModel): title: str = Field(min_length=1, max_length=60) columns: list[str] = Field(min_length=1, max_length=12) rows: list[dict[str, str]] = Field(max_length=5000) @router.post("/pdf") async def directory_pdf(payload: DirectoryPdf, _: User = Depends(get_current_user)) -> Response: columns = tuple(c for c in payload.columns if c.isidentifier()) rows = [{c: r.get(c, "") for c in columns} for r in payload.rows] name = "".join(ch for ch in payload.title.lower() if ch.isalnum()) or "directory" return Response( csv_table_pdf_bytes(f"directory_{name}", payload.title, columns, rows), media_type="application/pdf", headers={"Content-Disposition": f'inline; filename="{name}.pdf"'}, )