settings.py 871 B

123456789101112131415161718192021222324252627282930
  1. from typing import Optional
  2. from .core import isoformat, utcnow
  3. from .db import ctl_execute, ctl_fetch_one
  4. def get_setting_value(key: str) -> Optional[str]:
  5. row = ctl_fetch_one("SELECT value FROM app_settings WHERE key = ?", (key,))
  6. if row is None:
  7. return None
  8. return row["value"]
  9. def set_setting_value(key: str, value: str, category: str = "GENERAL") -> None:
  10. now = isoformat(utcnow())
  11. ctl_execute(
  12. """
  13. INSERT INTO app_settings (key, category, value, updated_at)
  14. VALUES (?, ?, ?, ?)
  15. ON CONFLICT(key) DO UPDATE SET
  16. category = excluded.category,
  17. value = excluded.value,
  18. updated_at = excluded.updated_at
  19. """,
  20. (key, category, value, now),
  21. )
  22. def delete_setting_value(key: str) -> None:
  23. ctl_execute("DELETE FROM app_settings WHERE key = ?", (key,))