refactor(); project refactor
This commit is contained in:
@@ -5,13 +5,12 @@ from typing import Any
|
||||
import requests
|
||||
from django.core.mail import send_mail
|
||||
from django.db import transaction
|
||||
from django.db.models import QuerySet
|
||||
from django.db.models import Q, QuerySet
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.notifications.models import (
|
||||
ChannelType,
|
||||
NotificationChannel,
|
||||
NotificationEventType,
|
||||
NotificationLog,
|
||||
NotificationRule,
|
||||
NotificationStatus,
|
||||
@@ -30,11 +29,6 @@ class NotificationPayload:
|
||||
extra: dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Channel CRUD
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def channel_create(
|
||||
*,
|
||||
@@ -82,11 +76,6 @@ def channel_get(channel_id: Any) -> NotificationChannel | None:
|
||||
return None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Rule CRUD
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def rule_create(
|
||||
*,
|
||||
@@ -130,11 +119,6 @@ def rule_list(channel_id: Any | None = None) -> QuerySet[NotificationRule]:
|
||||
return qs
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Log selectors
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def log_list(
|
||||
*,
|
||||
status: str | None = None,
|
||||
@@ -146,11 +130,6 @@ def log_list(
|
||||
return qs[:limit]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Notification enqueue (called from integration points)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def notification_enqueue(
|
||||
event_type: str,
|
||||
payload: NotificationPayload,
|
||||
@@ -163,8 +142,7 @@ def notification_enqueue(
|
||||
|
||||
if payload.experiment_id:
|
||||
rules = rules.filter(
|
||||
models_Q(experiment__isnull=True)
|
||||
| models_Q(experiment_id=payload.experiment_id)
|
||||
Q(experiment__isnull=True) | Q(experiment_id=payload.experiment_id)
|
||||
)
|
||||
else:
|
||||
rules = rules.filter(experiment__isnull=True)
|
||||
@@ -203,17 +181,6 @@ def _build_event_key(event_type: str, payload: NotificationPayload) -> str:
|
||||
return f"{event_type}:{payload.experiment_id}:{bucket}"
|
||||
|
||||
|
||||
def models_Q(**kwargs):
|
||||
from django.db.models import Q
|
||||
|
||||
return Q(**kwargs)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Senders
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _send_telegram(config: dict[str, Any], payload: dict[str, Any]) -> None:
|
||||
bot_token = config.get("bot_token", "")
|
||||
chat_id = config.get("chat_id", "")
|
||||
@@ -260,11 +227,6 @@ def _send_smtp(config: dict[str, Any], payload: dict[str, Any]) -> None:
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Flush pending (called from Celery task)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def flush_pending_notifications() -> dict[str, int]:
|
||||
pending = NotificationLog.objects.filter(
|
||||
status=NotificationStatus.PENDING,
|
||||
@@ -288,7 +250,9 @@ def flush_pending_notifications() -> dict[str, int]:
|
||||
sender = senders.get(log.channel.channel_type)
|
||||
if not sender:
|
||||
log.status = NotificationStatus.FAILED
|
||||
log.error = f"No sender for channel type '{log.channel.channel_type}'."
|
||||
log.error = (
|
||||
f"No sender for channel type '{log.channel.channel_type}'."
|
||||
)
|
||||
log.save(update_fields=["status", "error"])
|
||||
results["failed"] += 1
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user