Enable one-click dismissals from notifications list

This commit is contained in:
Jeremy Stretch 2024-07-04 12:22:13 -04:00
parent 9c875716f7
commit 29675d6e45
3 changed files with 25 additions and 8 deletions

View File

@ -0,0 +1,13 @@
from django.utils.translation import gettext as _
from netbox.tables.columns import ActionsColumn, ActionsItem
__all__ = (
'NotificationActionsColumn',
)
class NotificationActionsColumn(ActionsColumn):
actions = {
'dismiss': ActionsItem(_('Dismiss'), 'trash-can-outline', 'delete', 'danger'),
}

View File

@ -7,6 +7,7 @@ from django.utils.translation import gettext_lazy as _
from extras.models import * from extras.models import *
from netbox.constants import EMPTY_TABLE_TEXT from netbox.constants import EMPTY_TABLE_TEXT
from netbox.tables import BaseTable, NetBoxTable, columns from netbox.tables import BaseTable, NetBoxTable, columns
from .columns import NotificationActionsColumn
__all__ = ( __all__ = (
'BookmarkTable', 'BookmarkTable',
@ -303,8 +304,8 @@ class NotificationTable(NetBoxTable):
timespec='minutes', timespec='minutes',
verbose_name=_('Read'), verbose_name=_('Read'),
) )
actions = columns.ActionsColumn( actions = NotificationActionsColumn(
actions=('delete',) actions=('dismiss',)
) )
class Meta(NetBoxTable.Meta): class Meta(NetBoxTable.Meta):

View File

@ -432,7 +432,7 @@ 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):
notification = get_object_or_404(Notification, pk=pk) notification = get_object_or_404(request.user.notifications, pk=pk)
notification.read = timezone.now() notification.read = timezone.now()
notification.save() notification.save()
@ -445,12 +445,15 @@ class NotificationDismissView(LoginRequiredMixin, View):
A convenience view which allows deleting notifications with one click. A convenience view which allows deleting notifications with one click.
""" """
def get(self, request, pk): def get(self, request, pk):
request.user.notifications.filter(pk=pk).delete() notification = get_object_or_404(request.user.notifications, pk=pk)
notification.delete()
notifications = request.user.notifications.unread()[:10] if htmx_partial(request):
return render(request, 'htmx/notifications.html', { return render(request, 'htmx/notifications.html', {
'notifications': notifications, 'notifications': request.user.notifications.unread()[:10],
}) })
return redirect('account:notifications')
@register_model_view(Notification, 'delete') @register_model_view(Notification, 'delete')