From 926f56bb41f1e62b3bfbcbe3a51cb562cedbb41b Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 2 Apr 2022 04:11:29 +0200 Subject: [PATCH] move plugins checks to method --- InvenTree/common/notifications.py | 31 +++++++++++++++++++ .../builtin/integration/core_notifications.py | 14 +++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/InvenTree/common/notifications.py b/InvenTree/common/notifications.py index 103a070542..6dacac3918 100644 --- a/InvenTree/common/notifications.py +++ b/InvenTree/common/notifications.py @@ -4,6 +4,7 @@ from datetime import timedelta from InvenTree.helpers import inheritors from InvenTree.ready import isImportingData from common.models import NotificationEntry, NotificationMessage +from plugin import registry logger = logging.getLogger('inventree') @@ -17,6 +18,7 @@ class NotificationMethod: METHOD_NAME = '' CONTEXT_BUILTIN = ['name', 'message', ] CONTEXT_EXTRA = [] + GLOBAL_SETTING = None def __init__(self, obj, category, targets, context) -> None: # Check if a sending fnc is defined @@ -27,6 +29,11 @@ class NotificationMethod: if self.METHOD_NAME in ('', None): raise NotImplementedError(f'The NotificationMethod {self.__class__} did not provide a METHOD_NAME') + # Check if plugin is disabled - if so do not gather targets etc. + if self.global_setting_disable(): + self.targets = None + return + # Define arguments self.obj = obj self.category = category @@ -80,6 +87,30 @@ class NotificationMethod: def cleanup(self): return True + # region plugins + def get_plugin(self): + """Returns plugin class""" + return False + + def global_setting_disable(self): + """Check if the method is defined in a plugin and has a global setting""" + # Check if plugin has a setting + if not self.GLOBAL_SETTING: + return False + + # Check if plugin is set + plg_cls = self.get_plugin() + if not plg_cls: + return False + + # Check if method globally enabled + plg_instance = registry.plugins.get(plg_cls.PLUGIN_NAME.lower()) + if plg_instance and not plg_instance.get_setting(self.GLOBAL_SETTING): + return True + + # Lets go! + return False + # endregion class SingleNotificationMethod(NotificationMethod): def send(self, target): diff --git a/InvenTree/plugin/builtin/integration/core_notifications.py b/InvenTree/plugin/builtin/integration/core_notifications.py index ba4ba138cf..dc8b36114f 100644 --- a/InvenTree/plugin/builtin/integration/core_notifications.py +++ b/InvenTree/plugin/builtin/integration/core_notifications.py @@ -11,6 +11,11 @@ from common.models import InvenTreeUserSetting import InvenTree.tasks +class PlgMixin: + def get_plugin(self): + return CoreNotificationsPlugin + + class CoreNotificationsPlugin(SettingsMixin, IntegrationPluginBase): """ Core notification methods for InvenTree @@ -29,13 +34,15 @@ class CoreNotificationsPlugin(SettingsMixin, IntegrationPluginBase): }, } - class EmailNotification(BulkNotificationMethod): + class EmailNotification(PlgMixin, BulkNotificationMethod): METHOD_NAME = 'mail' CONTEXT_EXTRA = [ ('template', ), ('template', 'html', ), ('template', 'subject', ), ] + GLOBAL_SETTING = 'ENABLE_NOTIFICATION_EMAILS' + } def get_targets(self): """ @@ -43,11 +50,6 @@ class CoreNotificationsPlugin(SettingsMixin, IntegrationPluginBase): only for users which allow email notifications """ - # Check if method globally enabled - plg = registry.plugins.get(CoreNotificationsPlugin.PLUGIN_NAME.lower()) - if plg and not plg.get_setting('ENABLE_NOTIFICATION_EMAILS'): - return - allowed_users = [] for user in self.targets: