refactor(); project refactor

This commit is contained in:
ITQ
2026-02-23 11:46:52 +03:00
parent 85923f11fc
commit ca0c456862
16 changed files with 198 additions and 194 deletions
+3 -1
View File
@@ -41,7 +41,9 @@ class NotificationChannel(BaseModel):
default=dict,
blank=True,
verbose_name=_("configuration"),
help_text=_("Provider-specific settings (tokens, chat IDs, SMTP host, etc.)"),
help_text=_(
"Provider-specific settings (tokens, chat IDs, SMTP host, etc.)"
),
)
is_active = models.BooleanField(
default=True,
+5 -41
View File
@@ -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
@@ -6,10 +6,8 @@ from django.test import TestCase
from apps.experiments.tests.helpers import make_experiment
from apps.notifications.models import (
ChannelType,
NotificationChannel,
NotificationEventType,
NotificationLog,
NotificationRule,
NotificationStatus,
)
from apps.notifications.services import (
@@ -85,7 +83,9 @@ class RuleCRUDTest(TestCase):
event_type=NotificationEventType.GUARDRAIL_TRIGGERED,
channel=self.channel,
)
self.assertEqual(r.event_type, NotificationEventType.GUARDRAIL_TRIGGERED)
self.assertEqual(
r.event_type, NotificationEventType.GUARDRAIL_TRIGGERED
)
self.assertIsNone(r.experiment)
self.assertTrue(r.is_active)