@router.get("/{template_id}") async def get_template(template_id: int, user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)): t = await db.get(EmailTemplate, template_id) if not t or t.user_id != user.id: raise HTTPException(404, "Not found") analysis = analyze_template_variables(t.body_html) return {"id": t.id, "name": t.name, "subject_hint": t.subject_hint, "body_html": t.body_html, "body_text": t.body_text, "variables": analysis["variables"], "unknown_variables": analysis["unknown"]} @router.put("/{template_id}") async def edit_template(template_id: int, payload: TemplateIn, user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)): t = await db.get(EmailTemplate, template_id) if not t or t.user_id != user.id: raise HTTPException(404, "Not found") if re.search(r"PDF conversion test; returns the PDF inline.""" t = await db.get(EmailTemplate, template_id) if not t or t.user_id != user.id: raise HTTPException(404, "Not found") from app.services.variable_engine import GeneratedValues, VariableEngine engine = VariableEngine(sample.get("config", {})) g = GeneratedValues(invoice_number=engine.generate_invoice_number(set()), amount=engine.generate_amount(), shipping=engine.generate_shipping(), invoice_date=sample.get("invoice_date", "2026-01-15"), due_date=sample.get("due_date", "2026-01-30")) res = engine.substitute(t.body_html, sample.get("recipient", {}), g, extra={"unsubscribe_link": "https://example.com/unsubscribe"}) pdf = message_builder.html_to_pdf(res.text) import base64 return {"pdf_base64": base64.b64encode(pdf).decode(), "size_bytes": len(pdf), "engine": "WeasyPrint"}