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>
58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
import uuid
|
|
|
|
from fastapi import HTTPException, status
|
|
from sqlalchemy import func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.security import hash_password
|
|
from app.models.user import User
|
|
|
|
|
|
async def list_users(db: AsyncSession, limit: int = 50, offset: int = 0) -> tuple[list[User], int]:
|
|
total = await db.scalar(select(func.count()).select_from(User))
|
|
result = await db.execute(
|
|
select(User).order_by(User.created_at.desc()).limit(limit).offset(offset)
|
|
)
|
|
return list(result.scalars().all()), total or 0
|
|
|
|
|
|
async def get_user(db: AsyncSession, user_id: uuid.UUID) -> User:
|
|
result = await db.execute(select(User).where(User.id == user_id))
|
|
user = result.scalar_one_or_none()
|
|
if not user:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
|
|
return user
|
|
|
|
|
|
async def create_user(
|
|
db: AsyncSession, email: str, username: str, password: str,
|
|
full_name: str | None = None, role: str = "user", max_chats: int = 10,
|
|
) -> User:
|
|
existing = await db.execute(
|
|
select(User).where((User.email == email) | (User.username == username))
|
|
)
|
|
if existing.scalar_one_or_none():
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="User already exists")
|
|
|
|
user = User(
|
|
email=email,
|
|
username=username,
|
|
hashed_password=hash_password(password),
|
|
full_name=full_name,
|
|
role=role,
|
|
max_chats=max_chats,
|
|
)
|
|
db.add(user)
|
|
await db.flush()
|
|
return user
|
|
|
|
|
|
async def update_user(db: AsyncSession, user_id: uuid.UUID, **kwargs) -> User:
|
|
user = await get_user(db, user_id)
|
|
allowed = {"role", "is_active", "max_chats", "full_name"}
|
|
for key, value in kwargs.items():
|
|
if key in allowed and value is not None:
|
|
setattr(user, key, value)
|
|
await db.flush()
|
|
return user
|