65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
from typing import Any
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from database.models import Setting
|
|
|
|
from database.settings_cache import settings_cache
|
|
from ..defaults import DEFAULT_NOTIFICATIONS_CONFIG
|
|
from .runtime_sync import publish_runtime_config, register_runtime_config
|
|
|
|
|
|
NOTIFICATIONS_CONFIG: dict[str, Any] = DEFAULT_NOTIFICATIONS_CONFIG.copy()
|
|
register_runtime_config("NOTIFICATIONS_CONFIG", NOTIFICATIONS_CONFIG)
|
|
|
|
|
|
async def load_notifications_config(session: AsyncSession) -> None:
|
|
stmt = select(Setting).where(Setting.key == "NOTIFICATIONS_CONFIG")
|
|
result = await session.execute(stmt)
|
|
setting = result.scalar_one_or_none()
|
|
|
|
if setting is None:
|
|
notifications_config = DEFAULT_NOTIFICATIONS_CONFIG.copy()
|
|
setting = Setting(
|
|
key="NOTIFICATIONS_CONFIG",
|
|
value=notifications_config,
|
|
description="Конфигурация уведомлений",
|
|
)
|
|
session.add(setting)
|
|
else:
|
|
stored = setting.value or {}
|
|
notifications_config = DEFAULT_NOTIFICATIONS_CONFIG.copy()
|
|
notifications_config.update(stored)
|
|
setting.value = notifications_config
|
|
|
|
NOTIFICATIONS_CONFIG.clear()
|
|
NOTIFICATIONS_CONFIG.update(notifications_config)
|
|
await session.flush()
|
|
|
|
|
|
async def update_notifications_config(session: AsyncSession, new_values: dict[str, Any]) -> None:
|
|
stmt = select(Setting).where(Setting.key == "NOTIFICATIONS_CONFIG")
|
|
result = await session.execute(stmt)
|
|
setting = result.scalar_one_or_none()
|
|
|
|
if setting is None:
|
|
setting = Setting(
|
|
key="NOTIFICATIONS_CONFIG",
|
|
value=new_values,
|
|
description="Конфигурация уведомлений",
|
|
)
|
|
session.add(setting)
|
|
else:
|
|
setting.value = new_values
|
|
|
|
await session.commit()
|
|
|
|
notifications_config = DEFAULT_NOTIFICATIONS_CONFIG.copy()
|
|
notifications_config.update(new_values)
|
|
|
|
NOTIFICATIONS_CONFIG.clear()
|
|
NOTIFICATIONS_CONFIG.update(notifications_config)
|
|
settings_cache.update("NOTIFICATIONS_CONFIG", notifications_config)
|
|
await publish_runtime_config("NOTIFICATIONS_CONFIG", notifications_config)
|