From 8fa48903007dd1a82df0da1d6b49508dc2010ff2 Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Mon, 30 Dec 2019 21:20:04 +0100 Subject: [PATCH] Notifications: Move signal plugging for other models to App.ready() --- src/wiki/plugins/notifications/apps.py | 37 ++++++++++++++++++++++++ src/wiki/plugins/notifications/models.py | 30 ------------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/wiki/plugins/notifications/apps.py b/src/wiki/plugins/notifications/apps.py index 51c92c68..f4705ba5 100644 --- a/src/wiki/plugins/notifications/apps.py +++ b/src/wiki/plugins/notifications/apps.py @@ -1,4 +1,5 @@ from django.apps import AppConfig +from django.db.models import signals from django.utils.translation import gettext_lazy as _ @@ -6,3 +7,39 @@ class NotificationsConfig(AppConfig): name = 'wiki.plugins.notifications' verbose_name = _("Wiki notifications") label = 'wiki_notifications' + + def ready(self): + """ + NOTIFICATIONS FOR PLUGINS + """ + from django_nyt.utils import notify + from wiki.core.plugins import registry + from wiki.decorators import disable_signal_for_loaddata + from . import models + + for plugin in registry.get_plugins(): + + notifications = getattr(plugin, 'notifications', []) + for notification_dict in notifications: + @disable_signal_for_loaddata + def plugin_notification(instance, **kwargs): + if notification_dict.get('ignore', lambda x: False)(instance): + return + if kwargs.get('created', False) == notification_dict.get('created', True): + if 'get_url' in notification_dict: + url = notification_dict['get_url'](instance) + else: + url = models.default_url(notification_dict['get_article'](instance)) + + message = notification_dict['message'](instance) + notify( + message, + notification_dict['key'], + target_object=notification_dict['get_article'](instance), + url=url, + ) + + signals.post_save.connect( + plugin_notification, + sender=notification_dict['model'] + ) diff --git a/src/wiki/plugins/notifications/models.py b/src/wiki/plugins/notifications/models.py index 3c5bf985..ac5590a6 100644 --- a/src/wiki/plugins/notifications/models.py +++ b/src/wiki/plugins/notifications/models.py @@ -5,7 +5,6 @@ from django.utils.translation import gettext_lazy as _ from django_nyt.models import Subscription from django_nyt.utils import notify from wiki import models as wiki_models -from wiki.core.plugins import registry from wiki.decorators import disable_signal_for_loaddata from wiki.models.pluginbase import ArticlePlugin from wiki.plugins.notifications import settings @@ -76,32 +75,3 @@ signals.post_save.connect( # TODO: We should notify users when the current_revision of an article is # changed... - -################################################## -# NOTIFICATIONS FOR PLUGINS -################################################## -for plugin in registry.get_plugins(): - - notifications = getattr(plugin, 'notifications', []) - for notification_dict in notifications: - @disable_signal_for_loaddata - def plugin_notification(instance, **kwargs): - if notification_dict.get('ignore', lambda x: False)(instance): - return - if kwargs.get('created', False) == notification_dict.get('created', True): - if 'get_url' in notification_dict: - url = notification_dict['get_url'](instance) - else: - url = default_url(notification_dict['get_article'](instance)) - - message = notification_dict['message'](instance) - notify( - message, - notification_dict['key'], - target_object=notification_dict['get_article'](instance), - url=url, - ) - - signals.post_save.connect( - plugin_notification, - sender=notification_dict['model']) -- 2.45.2