move plugins checks to method

This commit is contained in:
Matthias 2022-04-02 04:11:29 +02:00
parent 6a300ea24a
commit 926f56bb41
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
2 changed files with 39 additions and 6 deletions

View File

@ -4,6 +4,7 @@ from datetime import timedelta
from InvenTree.helpers import inheritors from InvenTree.helpers import inheritors
from InvenTree.ready import isImportingData from InvenTree.ready import isImportingData
from common.models import NotificationEntry, NotificationMessage from common.models import NotificationEntry, NotificationMessage
from plugin import registry
logger = logging.getLogger('inventree') logger = logging.getLogger('inventree')
@ -17,6 +18,7 @@ class NotificationMethod:
METHOD_NAME = '' METHOD_NAME = ''
CONTEXT_BUILTIN = ['name', 'message', ] CONTEXT_BUILTIN = ['name', 'message', ]
CONTEXT_EXTRA = [] CONTEXT_EXTRA = []
GLOBAL_SETTING = None
def __init__(self, obj, category, targets, context) -> None: def __init__(self, obj, category, targets, context) -> None:
# Check if a sending fnc is defined # Check if a sending fnc is defined
@ -27,6 +29,11 @@ class NotificationMethod:
if self.METHOD_NAME in ('', None): if self.METHOD_NAME in ('', None):
raise NotImplementedError(f'The NotificationMethod {self.__class__} did not provide a METHOD_NAME') 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 # Define arguments
self.obj = obj self.obj = obj
self.category = category self.category = category
@ -80,6 +87,30 @@ class NotificationMethod:
def cleanup(self): def cleanup(self):
return True 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): class SingleNotificationMethod(NotificationMethod):
def send(self, target): def send(self, target):

View File

@ -11,6 +11,11 @@ from common.models import InvenTreeUserSetting
import InvenTree.tasks import InvenTree.tasks
class PlgMixin:
def get_plugin(self):
return CoreNotificationsPlugin
class CoreNotificationsPlugin(SettingsMixin, IntegrationPluginBase): class CoreNotificationsPlugin(SettingsMixin, IntegrationPluginBase):
""" """
Core notification methods for InvenTree Core notification methods for InvenTree
@ -29,13 +34,15 @@ class CoreNotificationsPlugin(SettingsMixin, IntegrationPluginBase):
}, },
} }
class EmailNotification(BulkNotificationMethod): class EmailNotification(PlgMixin, BulkNotificationMethod):
METHOD_NAME = 'mail' METHOD_NAME = 'mail'
CONTEXT_EXTRA = [ CONTEXT_EXTRA = [
('template', ), ('template', ),
('template', 'html', ), ('template', 'html', ),
('template', 'subject', ), ('template', 'subject', ),
] ]
GLOBAL_SETTING = 'ENABLE_NOTIFICATION_EMAILS'
}
def get_targets(self): def get_targets(self):
""" """
@ -43,11 +50,6 @@ class CoreNotificationsPlugin(SettingsMixin, IntegrationPluginBase):
only for users which allow email notifications 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 = [] allowed_users = []
for user in self.targets: for user in self.targets: