Backend: - PdfTemplate model with locale field + UNIQUE(name, locale) constraint - Migration 007: pdf_templates table + template_id FK on generated_pdfs - Template service: CRUD, Jinja2 validation, render preview with sample data - Admin endpoints: CRUD /admin/pdf-templates + POST preview - User endpoint: GET /pdf/templates (active templates list) - pdf_service: resolves template from DB by ID or falls back to default for the appropriate locale - AI generate_pdf tool accepts optional template_id - Seed script + 4 HTML template files: - Basic Report (en/ru) — general-purpose report - Medical Report (en/ru) — health-focused with disclaimers Frontend: - Admin PDF templates page with editor, locale selector, live preview (iframe), template variables reference panel - PDF page: template selector dropdown in generation form - API clients for admin CRUD + user template listing - Sidebar: admin templates link - English + Russian translations Also added Phase 9 (OAuth) and Phase 10 (Rate Limits) placeholders to GeneralPlan. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2.1 KiB
2.1 KiB
Phase 8: Customizable PDF Templates — Subplan
Goal
Allow admins to manage Jinja2 HTML PDF templates in the database, and let users (or AI) select a template when generating PDFs, with a live preview editor.
Prerequisites
- Phase 6-7 completed (PDF generation with WeasyPrint, admin patterns)
Tasks
- 8.1 Create
backend/app/models/pdf_template.py: PdfTemplate model (name, description, html_content, is_default, is_active). Register inmodels/__init__.py. - 8.2 Create migration
007_create_pdf_templates.py: table + seed default template from current report.html. Addtemplate_idFK togenerated_pdfs. - 8.3 Create
backend/app/services/pdf_template_service.py: CRUD +validate_jinja2()helper. - 8.4 Create
backend/app/schemas/pdf_template.py. Addtemplate_idtoGeneratePdfRequestandPdfResponse. - 8.5 Add admin endpoints to
admin.py: CRUD/admin/pdf-templates+ POST/admin/pdf-templates/preview. - 8.6 Add
GET /pdf/templatesuser endpoint (active templates list). - 8.7 Update
pdf_service.generate_pdf_report: accept template_id, load from DB, render viafrom_string(). - 8.8 Update AI tool
generate_pdfwith optional template_id. - 8.9 Frontend API:
admin-pdf-templates.tsCRUD + updatepdf.tswith template support. - 8.10 Update
frontend/src/pages/pdf.tsx: template selector dropdown. - 8.11 Create
frontend/src/pages/admin/pdf-templates.tsx: editor with live preview. - 8.12 Template variables documentation panel in editor.
- 8.13 Update routes, sidebar, en/ru translations.
- 8.14 Tests + verification.
Acceptance Criteria
- Default template seeded by migration, identical output to current
- Admin CRUD for templates; cannot delete default
- Invalid Jinja2 rejected with clear error
- Users select template when generating PDF; default used if omitted
- AI tool works with/without template_id
- Live preview in admin editor
- generated_pdfs.template_id records which template was used
- All UI text in en/ru
- Frontend builds clean, backend tests pass
Status
COMPLETED