from django.contrib import admin from openwisp_notifications.base.admin import NotificationSettingAdminMixin from openwisp_notifications.swapper import load_model from openwisp_notifications.widgets import _add_object_notification_widget from openwisp_users.admin import UserAdmin from openwisp_utils.admin import AlwaysHasChangedMixin Notification = load_model('Notification') NotificationSetting = load_model('NotificationSetting') class NotificationSettingInline(NotificationSettingAdminMixin, AlwaysHasChangedMixin, admin.TabularInline): model = NotificationSetting extra = 0 def has_change_permission(self, request, obj=None): return request.user.is_superuser or request.user == obj UserAdmin.inlines = [NotificationSettingInline] + UserAdmin.inlines _add_object_notification_widget()
from django.db.models.signals import post_delete, post_save, pre_delete from django.dispatch import receiver from django.template.loader import render_to_string from django.utils import timezone from django.utils.html import strip_tags from openwisp_notifications import settings as app_settings from openwisp_notifications.swapper import load_model from openwisp_notifications.tasks import delete_obsolete_notifications from openwisp_notifications.types import get_notification_configuration from openwisp_notifications.utils import NotificationException, _get_object_link User = get_user_model() EXTRA_DATA = app_settings.get_config()['USE_JSONFIELD'] Notification = load_model('Notification') NotificationUser = load_model('NotificationUser') def notify_handler(**kwargs): """ Handler function to create Notification instance upon action signal call. """ # Pull the options out of kwargs kwargs.pop('signal', None) actor = kwargs.pop('sender') public = bool(kwargs.pop('public', True)) description = kwargs.pop('description', None) timestamp = kwargs.pop('timestamp', timezone.now()) recipient = kwargs.pop('recipient', None) notification_type = kwargs.pop('type', None)
import pytest from channels.db import database_sync_to_async from channels.testing import WebsocketCommunicator from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.contenttypes.models import ContentType from django.utils.module_loading import import_string from openwisp_notifications.api.serializers import NotificationListSerializer from openwisp_notifications.signals import notify from openwisp_notifications.swapper import load_model from openwisp_notifications.tests.test_helpers import TEST_DATETIME User = get_user_model() Notification = load_model('Notification') IgnoreObjectNotification = load_model('IgnoreObjectNotification') @database_sync_to_async def create_notification(admin_user): n = notify.send(sender=admin_user, type='default').pop() return n[1].pop() @database_sync_to_async def notification_operation(notification, **kwargs): if kwargs.get('mark_read', False): notification.mark_as_read() if kwargs.get('delete', False): notification.delete()
from django.utils.html import strip_tags from openwisp_notifications import settings as app_settings from openwisp_notifications import tasks from openwisp_notifications.exceptions import NotificationRenderException from openwisp_notifications.swapper import load_model, swapper_load_model from openwisp_notifications.types import get_notification_configuration from openwisp_notifications.websockets import handlers as ws_handlers logger = logging.getLogger(__name__) EXTRA_DATA = app_settings.get_config()['USE_JSONFIELD'] User = get_user_model() Notification = load_model('Notification') NotificationSetting = load_model('NotificationSetting') IgnoreObjectNotification = load_model('IgnoreObjectNotification') NotificationsAppConfig = apps.get_app_config(NotificationSetting._meta.app_label) Group = swapper_load_model('openwisp_users', 'Group') OrganizationUser = swapper_load_model('openwisp_users', 'OrganizationUser') Organization = swapper_load_model('openwisp_users', 'Organization') def notify_handler(**kwargs): """ Handler function to create Notification instance upon action signal call. """ # Pull the options out of kwargs kwargs.pop('signal', None)
from io import StringIO from unittest.mock import patch import swapper from django.core import management from django.test import TransactionTestCase from openwisp_notifications import checks from openwisp_notifications import settings as app_settings from openwisp_notifications.swapper import load_model from openwisp_users.tests.utils import TestOrganizationMixin Notification = load_model('Notification') Organization = swapper.load_model('openwisp_users', 'Organization') class TestManagementCommands(TransactionTestCase, TestOrganizationMixin): def test_create_notification_command(self): admin = self._get_admin() management.call_command('create_notification') default_org = Organization.objects.first() self.assertEqual(Notification.objects.count(), 1) n = Notification.objects.first() self.assertEqual(n.type, 'default') self.assertEqual(n.actor, default_org) self.assertEqual(n.recipient, admin) @patch( 'openwisp_notifications.tasks.ns_register_unregister_notification_type.delay' )