Files
PacketStation/server/packetstation/notify/seeding.py
T

39 lines
1.4 KiB
Python
Raw Normal View History

"""Seed the routing rules: every missing event type enabled, immediate, digest 07:00, to PS-Admins.
Idempotent. An existing rule is never touched, so the Admin's choices stand.
"""
from __future__ import annotations
from typing import Any
from ..auth.groups import ADMIN_GROUP
from ..settings import Settings
from .catalog import EVENT_TYPES
def admins_dn(settings: Settings) -> str:
base = settings.ldap_group_base_dn.strip()
return f"CN={ADMIN_GROUP},{base}" if base else f"CN={ADMIN_GROUP}"
def seed(conn: Any, settings: Settings) -> list[str]:
"""Insert the missing rules. Returns the event types it inserted."""
added: list[str] = []
with conn.transaction():
for key, et in EVENT_TYPES.items():
row = conn.execute(
"INSERT INTO notification_rules (event_type, enabled, delivery, digest_time, param)"
" VALUES (%s, true, 'immediate', '07:00', %s) ON CONFLICT (event_type) DO NOTHING RETURNING event_type",
(key, et.param_default),
).fetchone()
if row is None:
continue
conn.execute(
"INSERT INTO notification_recipients (event_type, kind, group_dn, group_name)"
" VALUES (%s, 'group', %s, %s)",
(key, admins_dn(settings), ADMIN_GROUP),
)
added.append(key)
return added