Add UI themes (Dark, Light, Midnight, Sunset)

Four preset color themes, switchable from Profile settings with instant
preview and server-side persistence (User.theme, applied via a data-theme
attribute the CSS custom-property overrides in themes.css key off). Dark
stays the existing DarkSingularity palette and default. Light is a genuine
new light-mode design; Midnight is a higher-contrast OLED-friendly dark
variant; Sunset swaps in a warm amber/coral accent family.

Custom theme building (pick-your-own-colors) is out of scope for this
pass -- presets only.

Also: fixed the PATCH /api/auth/me handler to only apply fields actually
present in the request body. It previously always overwrote display_name
unconditionally, which happened to be harmless when it was the only
field on ProfileUpdate but would have silently cleared it on any
theme-only update. And switched two hardcoded hex colors
(.btn-primary:hover, .role-badge-admin) to token-derived color-mix()
values so they adapt across themes instead of staying fixed to the
original cyan/violet palette.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-15 22:02:38 -06:00
co-authored by Claude Sonnet 5
parent d7e777cbd8
commit afdd573182
14 changed files with 316 additions and 6 deletions
@@ -0,0 +1,32 @@
"""user theme preference
Revision ID: e849b2efb79b
Revises: 880f080648de
Create Date: 2026-08-15 21:53:44.914731
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'e849b2efb79b'
down_revision: Union[str, Sequence[str], None] = '880f080648de'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('theme', sa.String(length=20), nullable=True))
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('users', 'theme')
# ### end Alembic commands ###
+1
View File
@@ -18,6 +18,7 @@ class User(Base):
is_site_admin: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
display_name: Mapped[str | None] = mapped_column(String(50))
theme: Mapped[str | None] = mapped_column(String(20))
avatar_filename: Mapped[str | None] = mapped_column(String(64))
avatar_content_type: Mapped[str | None] = mapped_column(String(50))
created_at: Mapped[datetime] = mapped_column(
+9 -2
View File
@@ -74,8 +74,15 @@ async def update_profile(
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
) -> User:
display_name = data.display_name.strip() if data.display_name else None
current_user.display_name = display_name or None
# Only apply fields actually present in the request body -- a call that
# only wants to change the theme must not clobber display_name back to
# None, and vice versa.
updates = data.model_dump(exclude_unset=True)
if "display_name" in updates:
display_name = updates["display_name"].strip() if updates["display_name"] else None
current_user.display_name = display_name or None
if "theme" in updates:
current_user.theme = updates["theme"]
await db.commit()
await db.refresh(current_user)
return current_user
+8
View File
@@ -1,5 +1,6 @@
import uuid
from datetime import datetime
from typing import Literal
from pydantic import BaseModel, ConfigDict, EmailStr, Field
@@ -19,12 +20,19 @@ class UserRead(BaseModel):
is_bot: bool
is_site_admin: bool
display_name: str | None
theme: str | None
avatar_filename: str | None
created_at: datetime
class ProfileUpdate(BaseModel):
# Both fields are independently optional-and-settable -- the router
# only applies keys actually present in the request body
# (model_dump(exclude_unset=True)), so a call that only wants to change
# the theme doesn't clobber display_name back to None, and vice versa.
display_name: str | None = Field(default=None, max_length=50)
# Kept in sync with frontend/src/styles/themes.css's theme blocks.
theme: Literal["dark", "light", "midnight", "sunset"] | None = Field(default=None)
class UserDirectoryRead(BaseModel):
+38
View File
@@ -53,6 +53,44 @@ async def test_display_name_too_long_rejected(client, db_session):
assert resp.status_code == 422
async def test_update_theme_persists(client, db_session):
await register_and_login(client, db_session, username=_unique("alice"))
resp = await client.patch("/api/auth/me", json={"theme": "midnight"})
assert resp.status_code == 200, resp.text
assert resp.json()["theme"] == "midnight"
me = await client.get("/api/auth/me")
assert me.json()["theme"] == "midnight"
async def test_invalid_theme_rejected(client, db_session):
await register_and_login(client, db_session, username=_unique("alice"))
resp = await client.patch("/api/auth/me", json={"theme": "not-a-real-theme"})
assert resp.status_code == 422
async def test_updating_theme_does_not_clobber_display_name(client, db_session):
await register_and_login(client, db_session, username=_unique("alice"))
await client.patch("/api/auth/me", json={"display_name": "Alice A."})
resp = await client.patch("/api/auth/me", json={"theme": "light"})
assert resp.status_code == 200
assert resp.json()["display_name"] == "Alice A."
assert resp.json()["theme"] == "light"
async def test_updating_display_name_does_not_clobber_theme(client, db_session):
await register_and_login(client, db_session, username=_unique("alice"))
await client.patch("/api/auth/me", json={"theme": "sunset"})
resp = await client.patch("/api/auth/me", json={"display_name": "Alice A."})
assert resp.status_code == 200
assert resp.json()["theme"] == "sunset"
assert resp.json()["display_name"] == "Alice A."
async def test_avatar_upload_succeeds_and_persists(client, db_session):
await register_and_login(client, db_session, username=_unique("alice"))