Add settings and history archive repository protocols and DI support

This commit is contained in:
2026-04-17 20:54:08 +02:00
parent 7055971163
commit db5b4cb77e
8 changed files with 137 additions and 27 deletions

View File

@@ -14,12 +14,14 @@ import bcrypt
import structlog
from app.db import init_db, open_db
from app.repositories import settings_repo
from app.repositories import settings_repo as default_settings_repo
from app.utils.async_utils import run_blocking
if TYPE_CHECKING:
import aiosqlite
from app.repositories.protocols import SettingsRepository
log: structlog.stdlib.BoundLogger = structlog.get_logger()
# Keys used in the settings table.
@@ -34,11 +36,15 @@ _KEY_MAP_COLOR_THRESHOLD_MEDIUM = "map_color_threshold_medium"
_KEY_MAP_COLOR_THRESHOLD_LOW = "map_color_threshold_low"
async def is_setup_complete(db: aiosqlite.Connection) -> bool:
async def is_setup_complete(
db: aiosqlite.Connection,
settings_repo: SettingsRepository = default_settings_repo,
) -> bool:
"""Return ``True`` if initial setup has already been performed.
Args:
db: Active aiosqlite connection.
settings_repo: Repository interface for settings persistence.
Returns:
``True`` when the ``setup_completed`` key exists in settings.
@@ -55,6 +61,7 @@ async def run_setup(
fail2ban_socket: str,
timezone: str,
session_duration_minutes: int,
settings_repo: SettingsRepository = default_settings_repo,
) -> None:
"""Persist the initial configuration and mark setup as complete.
@@ -72,7 +79,7 @@ async def run_setup(
Raises:
RuntimeError: If setup has already been completed.
"""
if await is_setup_complete(db):
if await is_setup_complete(db, settings_repo=settings_repo):
raise RuntimeError("Setup has already been completed.")
log.info("bangui_setup_started")
@@ -120,17 +127,26 @@ async def run_setup(
log.info("bangui_setup_completed")
async def get_password_hash(db: aiosqlite.Connection) -> str | None:
async def get_password_hash(
db: aiosqlite.Connection,
settings_repo: SettingsRepository = default_settings_repo,
) -> str | None:
"""Return the stored bcrypt password hash, or ``None`` if not set."""
return await settings_repo.get_setting(db, _KEY_PASSWORD_HASH)
async def get_runtime_database_path(db: aiosqlite.Connection) -> str | None:
async def get_runtime_database_path(
db: aiosqlite.Connection,
settings_repo: SettingsRepository = default_settings_repo,
) -> str | None:
"""Return the runtime database path persisted during initial setup."""
return await settings_repo.get_setting(db, _KEY_DATABASE_PATH)
async def get_persisted_runtime_settings(db: aiosqlite.Connection) -> dict[str, str | int]:
async def get_persisted_runtime_settings(
db: aiosqlite.Connection,
settings_repo: SettingsRepository = default_settings_repo,
) -> dict[str, str | int]:
"""Return runtime configuration values persisted during initial setup."""
runtime_settings: dict[str, str | int] = {}
@@ -183,7 +199,10 @@ async def _ensure_database_initialized(database_path: str) -> bool:
return True
async def get_timezone(db: aiosqlite.Connection) -> str:
async def get_timezone(
db: aiosqlite.Connection,
settings_repo: SettingsRepository = default_settings_repo,
) -> str:
"""Return the configured IANA timezone string."""
tz = await settings_repo.get_setting(db, _KEY_TIMEZONE)
return tz if tz else "UTC"