Redirect to notifications list when marking as read notifications for deleted objects

This commit is contained in:
Jeremy Stretch 2024-08-15 10:43:13 -04:00
parent 855a07c2ec
commit ec25b06f58

View File

@ -424,12 +424,17 @@ class NotificationReadView(LoginRequiredMixin, View):
Mark the Notification read and redirect the user to its attached object. Mark the Notification read and redirect the user to its attached object.
""" """
def get(self, request, pk): def get(self, request, pk):
# Mark the Notification as read
notification = get_object_or_404(request.user.notifications, pk=pk) notification = get_object_or_404(request.user.notifications, pk=pk)
notification.read = timezone.now() notification.read = timezone.now()
notification.save() notification.save()
# 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(notification.object.get_absolute_url())
return redirect('account:notifications')
@register_model_view(Notification, 'dismiss') @register_model_view(Notification, 'dismiss')
class NotificationDismissView(LoginRequiredMixin, View): class NotificationDismissView(LoginRequiredMixin, View):