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
@@ -518,8 +518,9 @@ class NotificationsView(LoginRequiredMixin, View):
518518 """
519519 def get (self , request ):
520520 return render (request , 'htmx/notifications.html' , {
521- 'notifications' : request .user .notifications .unread (),
521+ 'notifications' : request .user .notifications .unread ()[: 10 ] ,
522522 'total_count' : request .user .notifications .count (),
523+ 'unread_count' : request .user .notifications .unread ().count (),
523524 })
524525
525526
@@ -528,6 +529,7 @@ class NotificationReadView(LoginRequiredMixin, View):
528529 """
529530 Mark the Notification read and redirect the user to its attached object.
530531 """
532+
531533 def get (self , request , pk ):
532534 # Mark the Notification as read
533535 notification = get_object_or_404 (request .user .notifications , pk = pk )
@@ -541,18 +543,48 @@ def get(self, request, pk):
541543 return redirect ('account:notifications' )
542544
543545
546+ @register_model_view (Notification , name = 'dismiss_all' , path = 'dismiss-all' , detail = False )
547+ class NotificationDismissAllView (LoginRequiredMixin , View ):
548+ """
549+ Convenience view to clear all *unread* notifications for the current user.
550+ """
551+
552+ def get (self , request ):
553+ request .user .notifications .unread ().delete ()
554+ if htmx_partial (request ):
555+ # If a user is currently on the notification page, redirect there (full repaint)
556+ redirect_resp = htmx_maybe_redirect_current_page (request , 'account:notifications' , preserve_query = True )
557+ if redirect_resp :
558+ return redirect_resp
559+
560+ return render (request , 'htmx/notifications.html' , {
561+ 'notifications' : request .user .notifications .unread ()[:10 ],
562+ 'total_count' : request .user .notifications .count (),
563+ 'unread_count' : request .user .notifications .unread ().count (),
564+ })
565+ return redirect ('account:notifications' )
566+
567+
544568@register_model_view (Notification , 'dismiss' )
545569class NotificationDismissView (LoginRequiredMixin , View ):
546570 """
547571 A convenience view which allows deleting notifications with one click.
548572 """
573+
549574 def get (self , request , pk ):
550575 notification = get_object_or_404 (request .user .notifications , pk = pk )
551576 notification .delete ()
552577
553578 if htmx_partial (request ):
579+ # If a user is currently on the notification page, redirect there (full repaint)
580+ redirect_resp = htmx_maybe_redirect_current_page (request , 'account:notifications' , preserve_query = True )
581+ if redirect_resp :
582+ return redirect_resp
583+
554584 return render (request , 'htmx/notifications.html' , {
555585 'notifications' : request .user .notifications .unread ()[:10 ],
586+ 'total_count' : request .user .notifications .count (),
587+ 'unread_count' : request .user .notifications .unread ().count (),
556588 })
557589
558590 return redirect ('account:notifications' )
0 commit comments