Files
ds-chat/backend/app/schemas/custom_theme.py
T
ksmithandClaude Sonnet 5 bd3e621e9f Support multiple named, saved custom themes per user
Replaces the single custom_theme_colors blob (one palette per user) with
a proper CustomTheme table -- users can now save, name, and switch
between as many custom palettes as they like, not just one.

Data model: users.active_custom_theme_id references whichever saved
CustomTheme (if any) is currently active; theme='custom' + that id
together determine what's rendered. The migration data-migrates any
already-saved single palette into a named CustomTheme row on upgrade,
and best-effort backfills the active one back into the old column shape
on downgrade.

New endpoints under /api/custom-themes: list, create, rename/recolor,
delete (falls back the user to a preset if the deleted theme was
active, so the two theme columns can never disagree), and activate.
UserRead.active_custom_theme is only populated when theme == 'custom'
even though the DB deliberately keeps the id set while a preset is
active, so switching to a preset and back doesn't lose the saved
palette.

ProfileModal now lists saved themes as swatches (click to activate,
pencil to edit -- active or not, trash to delete with a confirm), plus
a "+ New" button that creates, activates, and opens the editor for a
fresh theme immediately.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-17 07:53:05 -06:00

52 lines
1.6 KiB
Python

import uuid
from datetime import datetime
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
# Matches exactly the CSS custom properties frontend/src/styles/themes.css
# overrides per built-in preset -- a saved custom theme is applied the same
# way, just as inline styles on :root instead of a static stylesheet block
# (see frontend/src/lib/theme.ts). Hex-only (`#rrggbb`) since that's exactly
# what a native <input type="color"> always produces -- no alpha, no
# shorthand -- so the pattern constraint can't reject anything the picker UI
# itself sends.
_HEX_COLOR = Field(pattern=r"^#[0-9a-fA-F]{6}$")
class CustomThemeColors(BaseModel):
void: str = _HEX_COLOR
void_2: str = _HEX_COLOR
surface: str = _HEX_COLOR
surface_2: str = _HEX_COLOR
border: str = _HEX_COLOR
text: str = _HEX_COLOR
muted: str = _HEX_COLOR
accent: str = _HEX_COLOR
accent_2: str = _HEX_COLOR
accent_3: str = _HEX_COLOR
highlight: str = _HEX_COLOR
danger: str = _HEX_COLOR
color_scheme: Literal["light", "dark"]
class CustomThemeCreate(BaseModel):
name: str = Field(min_length=1, max_length=50)
colors: CustomThemeColors
class CustomThemeUpdate(BaseModel):
# Each field independently optional-and-settable, same convention as
# ProfileUpdate -- a rename shouldn't require resending all 12 colors.
name: str | None = Field(default=None, min_length=1, max_length=50)
colors: CustomThemeColors | None = Field(default=None)
class CustomThemeRead(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: uuid.UUID
name: str
colors: CustomThemeColors
created_at: datetime