settings: PATCH /api/settings/language with ACTIVE_LANGUAGES gate

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Giorgio Gilestro 2026-05-27 17:16:17 +02:00
parent 1ea71bc160
commit f4025e3cbb
2 changed files with 97 additions and 0 deletions

View file

@ -21,6 +21,7 @@ import httpx
from pydantic import BaseModel, Field
from app.auth import require_token, maybe_current_user, CurrentUser
from app.services.i18n import ACTIVE_LANGUAGES
from app.config import get_settings
from app.db import get_session, utcnow
from app.services.openrouter import (
@ -895,3 +896,38 @@ async def patch_digest_prefs(
user.digest_tone = payload.tone
await session.commit()
return DigestPrefsOut(opt_in=payload.opt_in, tone=payload.tone)
# ---------------------------------------------------------------------------
# Settings — language preference
# ---------------------------------------------------------------------------
class LanguagePrefsIn(BaseModel):
lang: str
class LanguagePrefsOut(BaseModel):
lang: str
@router.patch("/settings/language", response_model=LanguagePrefsOut)
async def patch_language_prefs(
payload: LanguagePrefsIn,
principal: CurrentUser = Depends(require_token),
session: AsyncSession = Depends(get_session),
) -> LanguagePrefsOut:
if principal.user is None:
raise HTTPException(status_code=400, detail="no_user_context")
lang = (payload.lang or "").strip().lower()
if lang not in ACTIVE_LANGUAGES:
raise HTTPException(
status_code=400,
detail=f"unsupported language: {payload.lang!r}",
)
user = await session.get(User, principal.user.id)
if user is None:
raise HTTPException(status_code=404, detail="user_not_found")
user.lang = lang
await session.commit()
return LanguagePrefsOut(lang=lang)