diff --git a/engine/apps/api/views/user_notification_policy.py b/engine/apps/api/views/user_notification_policy.py index 03ea04dd..c36f5106 100644 --- a/engine/apps/api/views/user_notification_policy.py +++ b/engine/apps/api/views/user_notification_policy.py @@ -176,7 +176,8 @@ class UserNotificationPolicyView(UpdateSerializerMixin, ModelViewSet): continue # extra backends may be enabled per organization - if notification_channel.name not in BUILT_IN_BACKENDS: + built_in_backend_names = {b[0] for b in BUILT_IN_BACKENDS} + if notification_channel.name not in built_in_backend_names: extra_messaging_backend = get_messaging_backend_from_id(notification_channel.name) if extra_messaging_backend is None: continue diff --git a/engine/apps/base/messaging.py b/engine/apps/base/messaging.py index 2e9e1e88..64d9682d 100644 --- a/engine/apps/base/messaging.py +++ b/engine/apps/base/messaging.py @@ -9,6 +9,9 @@ class BaseMessagingBackend: available_for_use = False templater = None + def __init__(self, *args, **kwargs): + self.notification_channel_id = kwargs.get("notification_channel_id") + def get_templater_class(self): if self.templater: return import_string(self.templater) @@ -46,16 +49,16 @@ class BaseMessagingBackend: raise NotImplementedError("notify_user method missing implementation") -def load_backend(path): - return import_string(path)() +def load_backend(path, *args, **kwargs): + return import_string(path)(*args, **kwargs) def get_messaging_backends(): global _messaging_backends if _messaging_backends is None: _messaging_backends = {} - for backend_path in settings.EXTRA_MESSAGING_BACKENDS: - backend = load_backend(backend_path) + for (backend_path, notification_channel_id) in settings.EXTRA_MESSAGING_BACKENDS: + backend = load_backend(backend_path, notification_channel_id=notification_channel_id) _messaging_backends[backend.backend_id] = backend return _messaging_backends.items() diff --git a/engine/apps/base/models/user_notification_policy.py b/engine/apps/base/models/user_notification_policy.py index fd2087f6..2e76824a 100644 --- a/engine/apps/base/models/user_notification_policy.py +++ b/engine/apps/base/models/user_notification_policy.py @@ -1,3 +1,4 @@ +from enum import unique from typing import Tuple from django.conf import settings @@ -30,13 +31,13 @@ def generate_public_primary_key_for_notification_policy(): # base supported notification backends BUILT_IN_BACKENDS = ( - "SLACK", - "SMS", - "PHONE_CALL", - "TELEGRAM", - "EMAIL", - "MOBILE_PUSH_GENERAL", - "MOBILE_PUSH_CRITICAL", + ("SLACK", 0), + ("SMS", 1), + ("PHONE_CALL", 2), + ("TELEGRAM", 3), + ("EMAIL", 4), + ("MOBILE_PUSH_GENERAL", 5), + ("MOBILE_PUSH_CRITICAL", 6), ) @@ -49,10 +50,10 @@ def _notification_channel_choices(): # use NotificationChannelOptions.AVAILABLE_FOR_USE instead. supported_backends = list(BUILT_IN_BACKENDS) - for backend_id, _ in get_messaging_backends(): - supported_backends.append(backend_id) + for backend_id, backend in get_messaging_backends(): + supported_backends.append((backend_id, backend.notification_channel_id)) - channels_enum = models.IntegerChoices("NotificationChannel", supported_backends, start=0) + channels_enum = unique(models.IntegerChoices("NotificationChannel", supported_backends)) return channels_enum diff --git a/engine/settings/ci-test.py b/engine/settings/ci-test.py index f351d2c5..b3d39d4e 100644 --- a/engine/settings/ci-test.py +++ b/engine/settings/ci-test.py @@ -39,4 +39,4 @@ SENDGRID_SECRET_KEY = "dummy_sendgrid_secret_key" TWILIO_ACCOUNT_SID = "dummy_twilio_account_sid" TWILIO_AUTH_TOKEN = "dummy_twilio_auth_token" -EXTRA_MESSAGING_BACKENDS = ["apps.base.tests.messaging_backend.TestOnlyBackend"] +EXTRA_MESSAGING_BACKENDS = [("apps.base.tests.messaging_backend.TestOnlyBackend", 42)] diff --git a/engine/settings/dev.py b/engine/settings/dev.py index 9dd65948..87a82d27 100644 --- a/engine/settings/dev.py +++ b/engine/settings/dev.py @@ -91,6 +91,6 @@ SWAGGER_SETTINGS = { } if TESTING: - EXTRA_MESSAGING_BACKENDS = ["apps.base.tests.messaging_backend.TestOnlyBackend"] + EXTRA_MESSAGING_BACKENDS = [("apps.base.tests.messaging_backend.TestOnlyBackend", 42)] TELEGRAM_TOKEN = "0000000000:XXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXX" TWILIO_AUTH_TOKEN = "twilio_auth_token"