2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 19:46:46 +00:00

Notifications fix (#8360)

* Fix for app loading

- Allow collection for background worker too

* Improved logging

* Refactor MethodStorageClass

- Cache methods more intelligently
- Re-collect if null
This commit is contained in:
Oliver 2024-10-25 18:00:32 +11:00 committed by GitHub
parent 6be6c4b42f
commit 331692bb46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 13 deletions

View File

@ -40,9 +40,14 @@ class InvenTreeConfig(AppConfig):
- Adding users set in the current environment - Adding users set in the current environment
""" """
# skip loading if plugin registry is not loaded or we run in a background thread # 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 ( if (
not InvenTree.ready.isPluginRegistryLoaded() not InvenTree.ready.isInMainThread()
or not InvenTree.ready.isInMainThread() and not InvenTree.ready.isInWorkerThread()
): ):
return return
@ -52,7 +57,6 @@ class InvenTreeConfig(AppConfig):
if InvenTree.ready.canAppAccessDatabase() or settings.TESTING_ENV: if InvenTree.ready.canAppAccessDatabase() or settings.TESTING_ENV:
self.remove_obsolete_tasks() self.remove_obsolete_tasks()
self.collect_tasks() self.collect_tasks()
self.start_background_tasks() self.start_background_tasks()

View File

@ -10,7 +10,7 @@ from django.utils.translation import gettext_lazy as _
import common.models import common.models
import InvenTree.helpers import InvenTree.helpers
from InvenTree.ready import isImportingData from InvenTree.ready import isImportingData, isRebuildingData
from plugin import registry from plugin import registry
from plugin.models import NotificationUserSetting, PluginConfig from plugin.models import NotificationUserSetting, PluginConfig
from users.models import Owner from users.models import Owner
@ -181,9 +181,20 @@ class MethodStorageClass:
Is initialized on startup as one instance named `storage` in this file. Is initialized on startup as one instance named `storage` in this file.
""" """
liste = None methods_list = None
user_settings = {} 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): def collect(self, selected_classes=None):
"""Collect all classes in the environment that are notification methods. """Collect all classes in the environment that are notification methods.
@ -192,7 +203,8 @@ class MethodStorageClass:
Args: Args:
selected_classes (class, optional): References to the classes that should be registered. Defaults to None. 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 = ( current_method = (
InvenTree.helpers.inheritors(NotificationMethod) - IGNORED_NOTIFICATION_CLS InvenTree.helpers.inheritors(NotificationMethod) - IGNORED_NOTIFICATION_CLS
) )
@ -215,8 +227,12 @@ class MethodStorageClass:
item.plugin = plugin() if plugin else None item.plugin = plugin() if plugin else None
filtered_list[ref] = item filtered_list[ref] = item
storage.liste = list(filtered_list.values()) storage.methods_list = list(filtered_list.values())
logger.info('Found %s notification methods', len(storage.liste))
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: def get_usersettings(self, user) -> list:
"""Returns all user settings for a specific user. """Returns all user settings for a specific user.
@ -230,7 +246,8 @@ class MethodStorageClass:
list: All applicablae notification settings. list: All applicablae notification settings.
""" """
methods = [] methods = []
for item in storage.liste:
for item in storage.methods:
if item.USER_SETTING: if item.USER_SETTING:
new_key = f'NOTIFICATION_METHOD_{item.METHOD_NAME.upper()}' new_key = f'NOTIFICATION_METHOD_{item.METHOD_NAME.upper()}'
@ -246,6 +263,7 @@ class MethodStorageClass:
'icon': getattr(item, 'METHOD_ICON', ''), 'icon': getattr(item, 'METHOD_ICON', ''),
'method': item.METHOD_NAME, 'method': item.METHOD_NAME,
}) })
return methods return methods
@ -348,7 +366,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs):
delivery_methods = kwargs.get('delivery_methods') delivery_methods = kwargs.get('delivery_methods')
# Check if data is importing currently # Check if data is importing currently
if isImportingData(): if isImportingData() or isRebuildingData():
return return
# Resolve object reference # Resolve object reference
@ -422,7 +440,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs):
# Collect possible methods # Collect possible methods
if delivery_methods is None: if delivery_methods is None:
delivery_methods = storage.liste or [] delivery_methods = storage.methods or []
else: else:
delivery_methods = delivery_methods - IGNORED_NOTIFICATION_CLS delivery_methods = delivery_methods - IGNORED_NOTIFICATION_CLS
@ -439,7 +457,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs):
# Set delivery flag # Set delivery flag
common.models.NotificationEntry.notify(category, obj_ref_value) common.models.NotificationEntry.notify(category, obj_ref_value)
else: 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): def trigger_superuser_notification(plugin: PluginConfig, msg: str):

View File

@ -95,7 +95,7 @@ def notification_list(context, *args, **kwargs):
'description': a.__doc__, 'description': a.__doc__,
'name': a.__name__, 'name': a.__name__,
} }
for a in storage.liste for a in storage.methods
] ]