53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
"""Theme export: public JSON endpoint, status check, and admin regeneration trigger."""
|
|
|
|
from typing import Any, Dict
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
from app.auth import get_current_admin_user
|
|
from app.theme_export import get_theme_status, write_theme_json
|
|
from app.user_models import User
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/status")
|
|
async def theme_status() -> Dict[str, Any]:
|
|
"""Return theme.json file status. Public endpoint."""
|
|
return get_theme_status()
|
|
|
|
|
|
@router.get("")
|
|
async def get_theme() -> Dict[str, Any]:
|
|
"""Return the current theme as a JSON dict. Public endpoint.
|
|
|
|
Also regenerates the theme.json file on disk as a side effect,
|
|
ensuring the file is always up-to-date.
|
|
"""
|
|
try:
|
|
theme = await write_theme_json()
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to generate theme: {e}",
|
|
)
|
|
return theme
|
|
|
|
|
|
@router.post("/regenerate", status_code=200)
|
|
async def regenerate_theme(
|
|
_: User = Depends(get_current_admin_user),
|
|
) -> Dict[str, Any]:
|
|
"""Force regenerate the theme.json file. Admin only.
|
|
|
|
Returns the newly generated theme dict.
|
|
"""
|
|
try:
|
|
theme = await write_theme_json()
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to regenerate theme: {e}",
|
|
)
|
|
return theme
|