feat: refactor to FastAPI architecture v2.0
Some checks failed
Build & Push Docker / build (push) Has been cancelled
Some checks failed
Build & Push Docker / build (push) Has been cancelled
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
56
app/api/pdf.py
Normal file
56
app/api/pdf.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""PDF generation API endpoint."""
|
||||
|
||||
from io import BytesIO
|
||||
|
||||
import pandas as pd
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from app.models.event import ScenarioDocument
|
||||
from app.core.validator import validate_inputs, ValidationError, ScenarsError
|
||||
from app.core.pdf_generator import generate_pdf
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/generate-pdf")
|
||||
async def generate_pdf_endpoint(doc: ScenarioDocument):
|
||||
"""Generate PDF timetable from ScenarioDocument."""
|
||||
try:
|
||||
validate_inputs(doc.event.title, doc.event.detail, 0)
|
||||
except ValidationError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
|
||||
# Convert to DataFrame
|
||||
rows = []
|
||||
for block in doc.blocks:
|
||||
rows.append({
|
||||
'Datum': block.datum,
|
||||
'Zacatek': block.zacatek,
|
||||
'Konec': block.konec,
|
||||
'Program': block.program,
|
||||
'Typ': block.typ,
|
||||
'Garant': block.garant,
|
||||
'Poznamka': block.poznamka,
|
||||
})
|
||||
|
||||
df = pd.DataFrame(rows)
|
||||
|
||||
if df.empty:
|
||||
raise HTTPException(status_code=422, detail="No blocks provided")
|
||||
|
||||
# Build program descriptions and colors
|
||||
program_descriptions = {pt.code: pt.description for pt in doc.program_types}
|
||||
program_colors = {pt.code: 'FF' + pt.color.lstrip('#') for pt in doc.program_types}
|
||||
|
||||
try:
|
||||
pdf_bytes = generate_pdf(df, doc.event.title, doc.event.detail,
|
||||
program_descriptions, program_colors)
|
||||
except ScenarsError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
|
||||
return StreamingResponse(
|
||||
BytesIO(pdf_bytes),
|
||||
media_type="application/pdf",
|
||||
headers={"Content-Disposition": "attachment; filename=scenar_timetable.pdf"}
|
||||
)
|
||||
Reference in New Issue
Block a user