Phase 6: PDF & Polish — PDF generation, admin users/settings, AI tool

Backend:
- Setting + GeneratedPdf models, Alembic migration with default settings seed
- PDF generation service (WeasyPrint + Jinja2 with autoescape)
- Health report HTML template with memory entries + document excerpts
- Admin user management: list, create, update (role/max_chats/is_active)
- Admin settings: self_registration_enabled, default_max_chats
- Self-registration check wired into auth register endpoint
- default_max_chats applied to new user registrations
- AI tool: generate_pdf creates health compilation PDFs
- PDF compile/list/download API endpoints
- WeasyPrint system deps added to Dockerfile

Frontend:
- PDF reports page with generate + download
- Admin users page with create/edit/activate/deactivate
- Admin settings page with self-registration toggle + max chats
- Extended sidebar with PDF reports + admin users/settings links
- English + Russian translations for all new UI

Review fixes applied:
- Jinja2 autoescape enabled (XSS prevention in PDFs)
- db.refresh after flush (created_at populated correctly)
- storage_path removed from API response (no internal path leak)
- Role field uses Literal["user", "admin"] validation
- React hooks called before conditional returns (rules of hooks)
- default_max_chats setting now applied during registration

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-19 14:37:43 +03:00
parent 04e3ae8319
commit fed6a3df1b
33 changed files with 1219 additions and 10 deletions

52
backend/app/api/v1/pdf.py Normal file
View File

@@ -0,0 +1,52 @@
import uuid
from pathlib import Path
from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import FileResponse
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.deps import get_current_user
from app.database import get_db
from app.models.user import User
from app.schemas.pdf import GeneratePdfRequest, PdfListResponse, PdfResponse
from app.services import pdf_service
router = APIRouter(prefix="/pdf", tags=["pdf"])
@router.post("/compile", response_model=PdfResponse, status_code=status.HTTP_201_CREATED)
async def compile_pdf(
data: GeneratePdfRequest,
user: Annotated[User, Depends(get_current_user)],
db: Annotated[AsyncSession, Depends(get_db)],
):
pdf = await pdf_service.generate_health_pdf(
db, user.id, data.title, data.document_ids or None, data.chat_id,
)
return PdfResponse.model_validate(pdf)
@router.get("/", response_model=PdfListResponse)
async def list_pdfs(
user: Annotated[User, Depends(get_current_user)],
db: Annotated[AsyncSession, Depends(get_db)],
):
pdfs = await pdf_service.get_user_pdfs(db, user.id)
return PdfListResponse(pdfs=[PdfResponse.model_validate(p) for p in pdfs])
@router.get("/{pdf_id}/download")
async def download_pdf(
pdf_id: uuid.UUID,
user: Annotated[User, Depends(get_current_user)],
db: Annotated[AsyncSession, Depends(get_db)],
):
pdf = await pdf_service.get_pdf(db, pdf_id, user.id)
if not pdf:
raise HTTPException(status_code=404, detail="PDF not found")
file_path = Path(pdf.storage_path)
if not file_path.exists():
raise HTTPException(status_code=404, detail="PDF file not found on disk")
media_type = "application/pdf" if file_path.suffix == ".pdf" else "text/html"
return FileResponse(path=str(file_path), filename=f"{pdf.title}.pdf", media_type=media_type)