diff --git a/src/backend/InvenTree/InvenTree/apps.py b/src/backend/InvenTree/InvenTree/apps.py index 0cd448361ff3..8e2ceeb4b885 100644 --- a/src/backend/InvenTree/InvenTree/apps.py +++ b/src/backend/InvenTree/InvenTree/apps.py @@ -40,9 +40,14 @@ def ready(self): - Adding users set in the current environment """ # skip loading if plugin registry is not loaded or we run in a background thread + + if not InvenTree.ready.isPluginRegistryLoaded(): + return + + # Skip if not in worker or main thread if ( - not InvenTree.ready.isPluginRegistryLoaded() - or not InvenTree.ready.isInMainThread() + not InvenTree.ready.isInMainThread() + and not InvenTree.ready.isInWorkerThread() ): return @@ -52,7 +57,6 @@ def ready(self): if InvenTree.ready.canAppAccessDatabase() or settings.TESTING_ENV: self.remove_obsolete_tasks() - self.collect_tasks() self.start_background_tasks() diff --git a/src/backend/InvenTree/common/notifications.py b/src/backend/InvenTree/common/notifications.py index 7e3c4016f1fd..23eb0ae5dfcc 100644 --- a/src/backend/InvenTree/common/notifications.py +++ b/src/backend/InvenTree/common/notifications.py @@ -10,7 +10,7 @@ import common.models import InvenTree.helpers -from InvenTree.ready import isImportingData +from InvenTree.ready import isImportingData, isRebuildingData from plugin import registry from plugin.models import NotificationUserSetting, PluginConfig from users.models import Owner @@ -185,9 +185,20 @@ class MethodStorageClass: Is initialized on startup as one instance named `storage` in this file. """ - liste = None + methods_list = None user_settings = {} + @property + def methods(self): + """Return all available methods. + + This is cached, and stored internally. + """ + if self.methods_list is None: + self.collect() + + return self.methods_list + def collect(self, selected_classes=None): """Collect all classes in the environment that are notification methods. @@ -196,7 +207,8 @@ def collect(self, selected_classes=None): Args: selected_classes (class, optional): References to the classes that should be registered. Defaults to None. """ - logger.debug('Collecting notification methods') + logger.debug('Collecting notification methods...') + current_method = ( InvenTree.helpers.inheritors(NotificationMethod) - IGNORED_NOTIFICATION_CLS ) @@ -219,8 +231,12 @@ def collect(self, selected_classes=None): item.plugin = plugin() if plugin else None filtered_list[ref] = item - storage.liste = list(filtered_list.values()) - logger.info('Found %s notification methods', len(storage.liste)) + storage.methods_list = list(filtered_list.values()) + + logger.info('Found %s notification methods', len(storage.methods_list)) + + for item in storage.methods_list: + logger.debug(' - %s', str(item)) def get_usersettings(self, user) -> list: """Returns all user settings for a specific user. @@ -234,7 +250,8 @@ def get_usersettings(self, user) -> list: list: All applicablae notification settings. """ methods = [] - for item in storage.liste: + + for item in storage.methods: if item.USER_SETTING: new_key = f'NOTIFICATION_METHOD_{item.METHOD_NAME.upper()}' @@ -250,6 +267,7 @@ def get_usersettings(self, user) -> list: 'icon': getattr(item, 'METHOD_ICON', ''), 'method': item.METHOD_NAME, }) + return methods @@ -352,7 +370,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs): delivery_methods = kwargs.get('delivery_methods', None) # Check if data is importing currently - if isImportingData(): + if isImportingData() or isRebuildingData(): return # Resolve object reference @@ -422,7 +440,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs): # Collect possible methods if delivery_methods is None: - delivery_methods = storage.liste or [] + delivery_methods = storage.methods or [] else: delivery_methods = delivery_methods - IGNORED_NOTIFICATION_CLS @@ -439,7 +457,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs): # Set delivery flag common.models.NotificationEntry.notify(category, obj_ref_value) else: - logger.debug("No possible users for notification '%s'", category) + logger.info("No possible users for notification '%s'", category) def trigger_superuser_notification(plugin: PluginConfig, msg: str): diff --git a/src/backend/InvenTree/plugin/templatetags/plugin_extras.py b/src/backend/InvenTree/plugin/templatetags/plugin_extras.py index c7d15e95a85b..c3e7885ecb05 100644 --- a/src/backend/InvenTree/plugin/templatetags/plugin_extras.py +++ b/src/backend/InvenTree/plugin/templatetags/plugin_extras.py @@ -95,7 +95,7 @@ def notification_list(context, *args, **kwargs): 'description': a.__doc__, 'name': a.__name__, } - for a in storage.liste + for a in storage.methods ]