Closes #15621: User notifications (#16800)

* Initial work on #15621

* Signal receiver should ignore models which don't support notifications

* Flesh out NotificationGroup functionality

* Add NotificationGroup filters for users & groups

* Separate read & dimiss actions

* Enable one-click dismissals from notifications list

* Include total notification count in dropdown

* Drop 'kind' field from Notification model

* Register event types in the registry; add colors & icons

* Enable event rules to target notification groups

* Define dynamic choices for Notification.event_name

* Move event registration to core

* Add more job events

* Misc cleanup

* Misc cleanup

* Correct absolute URLs for notifications & subscriptions

* Optimize subscriber notifications

* Use core event types when queuing events

* Standardize queued event attribute to event_type; change content_type to object_type

* Rename Notification.event_name to event_type

* Restore NotificationGroupBulkEditView

* Add API tests

* Add view & filterset tests

* Add model documentation

* Fix tests

* Update notification bell when notifications have been cleared

* Ensure subscribe button appears only on relevant models

* Notifications/subscriptions cannot be ordered by object

* Misc cleanup

* Add event icon & type to notifications table

* Adjust icon sizing

* Mute color of read notifications

* Misc cleanup
This commit is contained in:
Jeremy Stretch
2024-07-15 14:24:11 -04:00
committed by GitHub
parent 1c2336be60
commit b0e7294bc1
59 changed files with 1913 additions and 90 deletions

View File

@@ -1,4 +1,3 @@
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
@@ -6,10 +5,9 @@ from core.models import ObjectType
from dcim.models import DeviceType, Manufacturer, Site
from extras.choices import *
from extras.models import *
from users.models import Group, User
from utilities.testing import ViewTestCases, TestCase
User = get_user_model()
class CustomFieldTestCase(ViewTestCases.PrimaryObjectViewTestCase):
model = CustomField
@@ -620,3 +618,166 @@ class CustomLinkTest(TestCase):
response = self.client.get(site.get_absolute_url(), follow=True)
self.assertEqual(response.status_code, 200)
self.assertIn(f'FOO {site.name} BAR', str(response.content))
class SubscriptionTestCase(
ViewTestCases.CreateObjectViewTestCase,
ViewTestCases.DeleteObjectViewTestCase,
ViewTestCases.ListObjectsViewTestCase,
ViewTestCases.BulkDeleteObjectsViewTestCase
):
model = Subscription
@classmethod
def setUpTestData(cls):
site_ct = ContentType.objects.get_for_model(Site)
sites = (
Site(name='Site 1', slug='site-1'),
Site(name='Site 2', slug='site-2'),
Site(name='Site 3', slug='site-3'),
Site(name='Site 4', slug='site-4'),
)
Site.objects.bulk_create(sites)
cls.form_data = {
'object_type': site_ct.pk,
'object_id': sites[3].pk,
}
def setUp(self):
super().setUp()
sites = Site.objects.all()
user = self.user
subscriptions = (
Subscription(object=sites[0], user=user),
Subscription(object=sites[1], user=user),
Subscription(object=sites[2], user=user),
)
Subscription.objects.bulk_create(subscriptions)
def _get_url(self, action, instance=None):
if action == 'list':
return reverse('account:subscriptions')
return super()._get_url(action, instance)
def test_list_objects_anonymous(self):
self.client.logout()
url = reverse('account:subscriptions')
login_url = reverse('login')
self.assertRedirects(self.client.get(url), f'{login_url}?next={url}')
def test_list_objects_with_permission(self):
return
def test_list_objects_with_constrained_permission(self):
return
class NotificationGroupTestCase(ViewTestCases.PrimaryObjectViewTestCase):
model = NotificationGroup
@classmethod
def setUpTestData(cls):
users = (
User(username='User 1'),
User(username='User 2'),
User(username='User 3'),
)
User.objects.bulk_create(users)
groups = (
Group(name='Group 1'),
Group(name='Group 2'),
Group(name='Group 3'),
)
Group.objects.bulk_create(groups)
notification_groups = (
NotificationGroup(name='Notification Group 1'),
NotificationGroup(name='Notification Group 2'),
NotificationGroup(name='Notification Group 3'),
)
NotificationGroup.objects.bulk_create(notification_groups)
for i, notification_group in enumerate(notification_groups):
notification_group.users.add(users[i])
notification_group.groups.add(groups[i])
cls.form_data = {
'name': 'Notification Group X',
'description': 'Blah',
'users': [users[0].pk, users[1].pk],
'groups': [groups[0].pk, groups[1].pk],
}
cls.csv_data = (
'name,description,users,groups',
'Notification Group 4,Foo,"User 1,User 2","Group 1,Group 2"',
'Notification Group 5,Bar,"User 1,User 2","Group 1,Group 2"',
'Notification Group 6,Baz,"User 1,User 2","Group 1,Group 2"',
)
cls.csv_update_data = (
"id,name",
f"{notification_groups[0].pk},Notification Group 7",
f"{notification_groups[1].pk},Notification Group 8",
f"{notification_groups[2].pk},Notification Group 9",
)
cls.bulk_edit_data = {
'description': 'New description',
}
class NotificationTestCase(
ViewTestCases.DeleteObjectViewTestCase,
ViewTestCases.ListObjectsViewTestCase,
ViewTestCases.BulkDeleteObjectsViewTestCase
):
model = Notification
@classmethod
def setUpTestData(cls):
site_ct = ContentType.objects.get_for_model(Site)
sites = (
Site(name='Site 1', slug='site-1'),
Site(name='Site 2', slug='site-2'),
Site(name='Site 3', slug='site-3'),
Site(name='Site 4', slug='site-4'),
)
Site.objects.bulk_create(sites)
cls.form_data = {
'object_type': site_ct.pk,
'object_id': sites[3].pk,
}
def setUp(self):
super().setUp()
sites = Site.objects.all()
user = self.user
notifications = (
Notification(object=sites[0], user=user),
Notification(object=sites[1], user=user),
Notification(object=sites[2], user=user),
)
Notification.objects.bulk_create(notifications)
def _get_url(self, action, instance=None):
if action == 'list':
return reverse('account:notifications')
return super()._get_url(action, instance)
def test_list_objects_anonymous(self):
self.client.logout()
url = reverse('account:notifications')
login_url = reverse('login')
self.assertRedirects(self.client.get(url), f'{login_url}?next={url}')
def test_list_objects_with_permission(self):
return
def test_list_objects_with_constrained_permission(self):
return