~netlandish/django-wiki

8fa48903007dd1a82df0da1d6b49508dc2010ff2 — Benjamin Bach 4 years ago 00a75c1
Notifications: Move signal plugging for other models to App.ready()
2 files changed, 37 insertions(+), 30 deletions(-)

M src/wiki/plugins/notifications/apps.py
M src/wiki/plugins/notifications/models.py
M src/wiki/plugins/notifications/apps.py => src/wiki/plugins/notifications/apps.py +37 -0
@@ 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']
                )

M src/wiki/plugins/notifications/models.py => src/wiki/plugins/notifications/models.py +0 -30
@@ 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'])