From dd3166a4ed4e93d2f62e5115aaec2fe3fc179120 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 15 Aug 2024 12:04:19 -0400 Subject: [PATCH] Fixes #17097: Record static object representation when calling `NotificationGroup.notify()` (#17161) * Fixes #17097: Record static object representation when calling NotificationGroup.notify() * Redirect to notifications list when marking as read notifications for deleted objects * Link to object in notifications dropdown only for non-destructive events --- netbox/extras/events.py | 1 + netbox/extras/views.py | 7 ++++++- netbox/templates/htmx/notifications.html | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/netbox/extras/events.py b/netbox/extras/events.py index ad603d11a..f13a3b48f 100644 --- a/netbox/extras/events.py +++ b/netbox/extras/events.py @@ -139,6 +139,7 @@ def process_event_rules(event_rules, object_type, event_type, data, username=Non event_rule.action_object.notify( object_type=object_type, object_id=data['id'], + object_repr=data.get('display'), event_type=event_type ) diff --git a/netbox/extras/views.py b/netbox/extras/views.py index 8eb9f412d..aaf8ff393 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -424,11 +424,16 @@ class NotificationReadView(LoginRequiredMixin, View): Mark the Notification read and redirect the user to its attached object. """ def get(self, request, pk): + # Mark the Notification as read notification = get_object_or_404(request.user.notifications, pk=pk) notification.read = timezone.now() notification.save() - return redirect(notification.object.get_absolute_url()) + # Redirect to the object if it has a URL (deleted objects will not) + if hasattr(notification.object, 'get_absolute_url'): + return redirect(notification.object.get_absolute_url()) + + return redirect('account:notifications') @register_model_view(Notification, 'dismiss') diff --git a/netbox/templates/htmx/notifications.html b/netbox/templates/htmx/notifications.html index 23660e4c5..e03e5afc9 100644 --- a/netbox/templates/htmx/notifications.html +++ b/netbox/templates/htmx/notifications.html @@ -7,7 +7,11 @@
- {{ notification.object_repr }} + {% if not notification.event.destructive %} + {{ notification.object_repr }} + {% else %} + {{ notification.object_repr }} + {% endif %}
{{ notification.event }} {{ notification.created|timesince }} {% trans "ago" %}