From 905b164ecbe9d21297f361e0073b3fdd4ac94f25 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 3 Apr 2022 03:47:48 +0200 Subject: [PATCH] store methods on load --- InvenTree/InvenTree/apps.py | 10 ++++++++++ InvenTree/common/notifications.py | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 6b2f2b1610..7787bbfb0c 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -31,6 +31,8 @@ class InvenTreeConfig(AppConfig): if not isInTestMode(): # pragma: no cover self.update_exchange_rates() + self.collect_notification_methods() + if canAppAccessDatabase() or settings.TESTING_ENV: self.add_user_on_startup() @@ -197,3 +199,11 @@ class InvenTreeConfig(AppConfig): # do not try again settings.USER_ADDED = True + + def collect_notification_methods(self): + """ + Collect all notification methods + """ + from common.notifications import storage + + storage.collect() diff --git a/InvenTree/common/notifications.py b/InvenTree/common/notifications.py index 6dacac3918..63cd75ec3d 100644 --- a/InvenTree/common/notifications.py +++ b/InvenTree/common/notifications.py @@ -122,10 +122,18 @@ class BulkNotificationMethod(NotificationMethod): raise NotImplementedError('The `send` method must be overriden!') +class MethodStorageClass: + liste = None + + def collect(self): + storage.liste = inheritors(NotificationMethod) - IGNORED_NOTIFICATION_CLS + + IGNORED_NOTIFICATION_CLS = set([ SingleNotificationMethod, BulkNotificationMethod, ]) +storage = MethodStorageClass() class UIMessageNotification(SingleNotificationMethod): @@ -190,9 +198,11 @@ def trigger_notifaction(obj, category=None, obj_ref='pk', **kwargs): # Collect possible methods if delivery_methods is None: - delivery_methods = inheritors(NotificationMethod) + delivery_methods = storage.liste + else: + delivery_methods = (delivery_methods - IGNORED_NOTIFICATION_CLS) - for method in (delivery_methods - IGNORED_NOTIFICATION_CLS): + for method in delivery_methods: logger.info(f"Triggering method '{method.METHOD_NAME}'") try: deliver_notification(method, obj, category, targets, context)