diff --git a/notifications/admin.py b/notifications/admin.py index 72b9e5d..ce34924 100644 --- a/notifications/admin.py +++ b/notifications/admin.py @@ -10,5 +10,9 @@ class NotificationAdmin(admin.ModelAdmin): 'level', 'target', 'unread', 'public') list_filter = ('level', 'unread', 'public', 'timestamp',) + def get_queryset(self, request): + qs = super(NotificationAdmin, self).get_queryset(request) + return qs.prefetch_related('actor') + admin.site.register(Notification, NotificationAdmin) diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index 546d058..416c96c 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -11,8 +11,10 @@ import pytz from django.conf import settings from django.contrib.auth.models import Group, User from django.core.exceptions import ImproperlyConfigured +from django.db import connection from django.template import Context, Template from django.test import RequestFactory, TestCase +from django.test.utils import CaptureQueriesContext from django.utils import timezone from django.utils.timezone import localtime, utc from notifications.models import Notification, notify_handler @@ -510,3 +512,29 @@ class TagTest(TestCase): context = {"user":self.to_user} output = u"True" self.tag_test(template, context, output) + + +class AdminTest(TestCase): + def setUp(self): + self.message_count = 10 + self.from_user = User.objects.create_user(username="from", password="pwd", email="example@example.com") + self.to_user = User.objects.create_user(username="to", password="pwd", email="example@example.com") + self.to_user.is_staff = True + self.to_user.is_superuser = True + self.to_user.save() + for _ in range(self.message_count): + notify.send( + self.from_user, + recipient=self.to_user, + verb='commented', + action_object=self.from_user, + ) + + def test_list(self): + self.client.login(username='to', password='pwd') + + with CaptureQueriesContext(connection=connection) as context: + response = self.client.get(reverse('admin:notifications_notification_changelist')) + self.assertLessEqual(len(context), 6) + + self.assertEqual(response.status_code, 200, response.content)