44from django .contrib .contenttypes .models import ContentType
55from django .core .paginator import EmptyPage
66from django .db .models import Count , Q
7- from django .http import HttpResponseBadRequest , HttpResponseForbidden , HttpResponse
7+ from django .http import HttpResponseBadRequest , HttpResponseForbidden , HttpResponse , Http404
88from django .shortcuts import get_object_or_404 , redirect , render
99from django .urls import reverse
1010from django .utils import timezone
2525from netbox .views import generic
2626from netbox .views .generic .mixins import TableMixin
2727from utilities .forms import ConfirmationForm , get_field_value
28- from utilities .htmx import htmx_partial
28+ from utilities .htmx import htmx_partial , htmx_maybe_redirect_current_page
2929from utilities .paginator import EnhancedPaginator , get_paginate_count
3030from utilities .query import count_related
3131from utilities .querydict import normalize_querydict
@@ -525,8 +525,9 @@ class NotificationsView(LoginRequiredMixin, View):
525525 """
526526 def get (self , request ):
527527 return render (request , 'htmx/notifications.html' , {
528- 'notifications' : request .user .notifications .unread (),
528+ 'notifications' : request .user .notifications .unread ()[: 10 ] ,
529529 'total_count' : request .user .notifications .count (),
530+ 'unread_count' : request .user .notifications .unread ().count (),
530531 })
531532
532533
@@ -535,6 +536,7 @@ class NotificationReadView(LoginRequiredMixin, View):
535536 """
536537 Mark the Notification read and redirect the user to its attached object.
537538 """
539+
538540 def get (self , request , pk ):
539541 # Mark the Notification as read
540542 notification = get_object_or_404 (request .user .notifications , pk = pk )
@@ -548,18 +550,48 @@ def get(self, request, pk):
548550 return redirect ('account:notifications' )
549551
550552
553+ @register_model_view (Notification , name = 'dismiss_all' , path = 'dismiss-all' , detail = False )
554+ class NotificationDismissAllView (LoginRequiredMixin , View ):
555+ """
556+ Convenience view to clear all *unread* notifications for the current user.
557+ """
558+
559+ def get (self , request ):
560+ request .user .notifications .unread ().delete ()
561+ if htmx_partial (request ):
562+ # If a user is currently on the notification page, redirect there (full repaint)
563+ redirect_resp = htmx_maybe_redirect_current_page (request , 'account:notifications' , preserve_query = True )
564+ if redirect_resp :
565+ return redirect_resp
566+
567+ return render (request , 'htmx/notifications.html' , {
568+ 'notifications' : request .user .notifications .unread ()[:10 ],
569+ 'total_count' : request .user .notifications .count (),
570+ 'unread_count' : request .user .notifications .unread ().count (),
571+ })
572+ return redirect ('account:notifications' )
573+
574+
551575@register_model_view (Notification , 'dismiss' )
552576class NotificationDismissView (LoginRequiredMixin , View ):
553577 """
554578 A convenience view which allows deleting notifications with one click.
555579 """
580+
556581 def get (self , request , pk ):
557582 notification = get_object_or_404 (request .user .notifications , pk = pk )
558583 notification .delete ()
559584
560585 if htmx_partial (request ):
586+ # If a user is currently on the notification page, redirect there (full repaint)
587+ redirect_resp = htmx_maybe_redirect_current_page (request , 'account:notifications' , preserve_query = True )
588+ if redirect_resp :
589+ return redirect_resp
590+
561591 return render (request , 'htmx/notifications.html' , {
562592 'notifications' : request .user .notifications .unread ()[:10 ],
593+ 'total_count' : request .user .notifications .count (),
594+ 'unread_count' : request .user .notifications .unread ().count (),
563595 })
564596
565597 return redirect ('account:notifications' )
0 commit comments