From 2a2ecb661213e0527035f83d6462a9479827ce0b Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Thu, 11 Aug 2022 17:26:45 -0300 Subject: [PATCH] Make messaging backends enum be defined explicitly --- .../api/views/user_notification_policy.py | 3 ++- engine/apps/base/messaging.py | 11 ++++++---- .../base/models/user_notification_policy.py | 21 ++++++++++--------- engine/settings/ci-test.py | 2 +- engine/settings/dev.py | 2 +- 5 files changed, 22 insertions(+), 17 deletions(-) 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 16c655b5..df0ac27c 100644 --- a/engine/settings/ci-test.py +++ b/engine/settings/ci-test.py @@ -37,4 +37,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 b5e0e2f5..9fc7e2eb 100644 --- a/engine/settings/dev.py +++ b/engine/settings/dev.py @@ -94,6 +94,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"