Files
celtic-life-science/app/pdf/router.py
Shubhankar45 0d30c58bf0 Server backup 2026-11-07 of uncommitted work in projects/pharma-web-celtic
Snapshot of the working tree on srv1071960 (branch main), taken during
server cleanup. .env files, node_modules, builds, logs and media were excluded.
2026-11-07 05:43:12 +00:00

32 lines
1.3 KiB
Python

"""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"'},
)