55from django .contrib .auth .mixins import LoginRequiredMixin
66from django .shortcuts import get_object_or_404
77from django .shortcuts import redirect
8+ from django .shortcuts import render
89from django .urls import reverse
910from django .utils import timezone
1011from django .views .generic import UpdateView
1112from django .views .generic import View
1213
14+ from apps .userdashboard .views import UserDashboardNotificationsBaseView
15+
1316from .forms import NotificationSettingsForm
1417from .models import Notification
1518from .models import NotificationSettings
@@ -53,6 +56,30 @@ def post(self, request):
5356 return redirect ("account_notification_settings" )
5457
5558
59+ class MarkAllNotificationsAsReadView (UserDashboardNotificationsBaseView ):
60+ """Mark all notifications as read with HTMX support"""
61+
62+ def post (self , request , * args , ** kwargs ):
63+ section = request .POST .get ("section" , "" )
64+ notifications = Notification .objects .filter (recipient = request .user , read = False )
65+
66+ if section :
67+ notifications = get_notifications_by_section (notifications , section )
68+ notifications .update (read = True , read_at = timezone .now ())
69+
70+ if request .headers .get ("HX-Request" ):
71+ context = self ._get_notifications_context ()
72+ response = render (
73+ request , "a4_candy_notifications/_notifications_partial.html" , context
74+ )
75+ print ("RESPOONDING WITH TRIGGER" )
76+ # Add HTMX trigger header to update the button
77+ response ["HX-Trigger" ] = "updateNotificationCount"
78+ return response
79+ else :
80+ return redirect ("userdashboard-notifications" )
81+
82+
5683class MarkNotificationAsReadView (LoginRequiredMixin , View ):
5784 def get (self , request , * args , ** kwargs ):
5885 notification = get_object_or_404 (
@@ -68,13 +95,16 @@ def get(self, request, *args, **kwargs):
6895 return redirect (request .META .get ("HTTP_REFERER" , "home" ))
6996
7097
71- class MarkAllNotificationsAsReadView (LoginRequiredMixin , View ):
72- def post (self , request ):
73- section = request .POST .get ("section" , "" )
74- notifications = Notification .objects .filter (recipient = request .user , read = False )
98+ class NotificationCountPartialView (UserDashboardNotificationsBaseView ):
99+ """HTMX partial for just the notification badge"""
75100
76- if section :
77- notifications = get_notifications_by_section (notifications , section )
78- notifications .update (read = True , read_at = timezone .now ())
79- messages .success (request , "All notifications marked as read" )
80- return redirect (request .META .get ("HTTP_REFERER" , "home" ))
101+ def get (self , request , * args , ** kwargs ):
102+ unread_count = 0
103+ if request .user .is_authenticated :
104+ unread_count = Notification .objects .unread_count_for_user (request .user )
105+
106+ return render (
107+ request ,
108+ "a4_candy_notifications/_notifications_button_partial.html" ,
109+ {"user" : request .user , "unread_count" : unread_count },
110+ )
0 commit comments