Files
hey-search/backend/app/settings.py
T
junvandCopilot 4bd375cb83 feat: Unsplash background image on homepage with gallery
- Fetch random nature/landscape image from Unsplash (picsum fallback)
- Auto-refresh with configurable interval (1 min to 1 day, default 30 min)
- Save every downloaded image to DATA_DIR/backgrounds/ for history
- New 'Background' tab in Settings: enable/disable toggle + refresh presets
- 'New image' button on homepage to manually fetch fresh background
- /backgrounds gallery page with masonry layout and lazy loading
- Background settings (bg_enabled, bg_refresh_minutes) persisted in SQLite
- Homepage adapts text colors when background is active
- Added backend/data/ to .gitignore, removed tracked DB file

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-02-23 18:54:44 +11:00

80 lines
2.0 KiB
Python

"""SQLite-backed application settings."""
from __future__ import annotations
import logging
from app.excluded import _get_conn
logger = logging.getLogger(__name__)
# Default values
DEFAULTS: dict[str, str] = {
"cache_ttl_hours": "6",
"redis_url": "",
"bg_enabled": "true",
"bg_refresh_minutes": "30",
}
def init_settings_table() -> None:
"""Create the settings table and populate defaults."""
conn = _get_conn()
conn.execute(
"""
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)
"""
)
# Insert defaults for any missing keys
for key, default in DEFAULTS.items():
conn.execute(
"INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)",
(key, default),
)
conn.commit()
conn.close()
logger.info("Settings table initialized")
def get_setting(key: str) -> str:
"""Get a single setting value. Returns default if not set."""
conn = _get_conn()
row = conn.execute("SELECT value FROM settings WHERE key = ?", (key,)).fetchone()
conn.close()
if row:
return row[0]
return DEFAULTS.get(key, "")
def set_setting(key: str, value: str) -> None:
"""Set a single setting value."""
conn = _get_conn()
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
(key, value),
)
conn.commit()
conn.close()
def get_all_settings() -> dict[str, str]:
"""Get all settings as a dict."""
conn = _get_conn()
rows = conn.execute("SELECT key, value FROM settings").fetchall()
conn.close()
result = dict(DEFAULTS) # start with defaults
result.update({k: v for k, v in rows})
return result
def get_cache_ttl_seconds() -> int:
"""Get cache TTL in seconds. Returns 0 if caching is disabled."""
try:
hours = float(get_setting("cache_ttl_hours"))
return max(0, int(hours * 3600))
except (ValueError, TypeError):
return 6 * 3600 # fallback: 6 hours