feat: persist map tile settings (provider/style/API keys) server-side on user record; unbake Thunderforest default key into backend config (THUNDERFOREST_DEFAULT_KEY) served via profile
This commit is contained in:
@@ -7,6 +7,7 @@ from datetime import datetime, date, timezone
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.security import get_current_user, hash_password, verify_password
|
||||
from app.core.config import settings
|
||||
from app.models.user import User, WeightLog
|
||||
|
||||
router = APIRouter()
|
||||
@@ -36,6 +37,8 @@ class ProfileOut(BaseModel):
|
||||
estimated_max_hr: Optional[int]
|
||||
is_admin: bool
|
||||
dashboard_layout: Optional[list] = None
|
||||
map_settings: Optional[dict] = None
|
||||
thunderforest_default_key: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
@@ -45,6 +48,12 @@ class DashboardLayoutIn(BaseModel):
|
||||
layout: Optional[list] = None # react-grid-layout array of {i,x,y,w,h}
|
||||
|
||||
|
||||
class MapSettingsIn(BaseModel):
|
||||
provider: Optional[str] = None
|
||||
style: Optional[str] = None
|
||||
keys: Optional[dict] = None # {thunderforest, maptiler, ...} public tile keys
|
||||
|
||||
|
||||
def _estimated_max_hr(user: User) -> Optional[int]:
|
||||
if user.birth_year:
|
||||
return 220 - (datetime.now().year - user.birth_year)
|
||||
@@ -55,7 +64,27 @@ def _estimated_max_hr(user: User) -> Optional[int]:
|
||||
async def get_profile(current_user: User = Depends(get_current_user)):
|
||||
return {**{c.name: getattr(current_user, c.name)
|
||||
for c in User.__table__.columns},
|
||||
"estimated_max_hr": _estimated_max_hr(current_user)}
|
||||
"estimated_max_hr": _estimated_max_hr(current_user),
|
||||
"thunderforest_default_key": settings.thunderforest_default_key}
|
||||
|
||||
|
||||
@router.put("/map-settings")
|
||||
async def save_map_settings(
|
||||
body: MapSettingsIn,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Persist the user's global map tile preference (provider/style/keys)."""
|
||||
keys = {}
|
||||
for k, v in (body.keys or {}).items():
|
||||
keys[str(k)] = (v or "").strip()
|
||||
current_user.map_settings = {
|
||||
"provider": body.provider,
|
||||
"style": body.style,
|
||||
"keys": keys,
|
||||
}
|
||||
await db.commit()
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.put("/dashboard-layout")
|
||||
@@ -111,7 +140,8 @@ async def update_profile(
|
||||
|
||||
return {**{c.name: getattr(current_user, c.name)
|
||||
for c in User.__table__.columns},
|
||||
"estimated_max_hr": _estimated_max_hr(current_user)}
|
||||
"estimated_max_hr": _estimated_max_hr(current_user),
|
||||
"thunderforest_default_key": settings.thunderforest_default_key}
|
||||
|
||||
|
||||
# ── Password change ────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user