Source code for superdjango.contrib.notifications.views

# Imports
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views import View
from superdjango.patterns import PATTERN_PK
from superdjango.views import BreadcrumbsMixin, DetailView, JSONMixin, ListView, LoginRequiredMixin, ModelViewSet, \
    ViewSet
from .models import NotificationUser
from .shortcuts import get_unread_notifications

# Exports

__all__ = (
    "AjaxMarkNotificationRead",
    "AjaxNotificationsViewSet",
    "AjaxUnreadNotifications",
    "NotificationDetail",
    "NotificationList",
    "NotificationsViewSet",
)

# Views


[docs]class AjaxMarkNotificationRead(LoginRequiredMixin, JSONMixin, View): """Mark a notification as viewed.""" pattern_name = "ajax_mark_notification_read" pattern_value = "notifications/mark/read/%s/" % PATTERN_PK # noinspection PyUnusedLocal
[docs] def get(self, request, *args, **kwargs): """Process the request.""" link_id = self.kwargs['pk'] criteria = { 'pk': link_id, 'user': request.user, } try: link = NotificationUser.objects.get(**criteria) link.mark_viewed() return self.get_json_response({'success': "Notification has been viewed."}) except NotificationUser.DoesNotExist: return self.get_json_response({'error': "Notification does not exist."})
[docs]class AjaxUnreadNotifications(LoginRequiredMixin, JSONMixin, View): """Get unread notifications in real time.""" pattern_name = "ajax_unread_notifications" pattern_value = "notifications/unread/" # noinspection PyUnusedLocal
[docs] def get(self, request, *args, **kwargs): """Process the request.""" data = list() for notification in get_unread_notifications(request.user): data.append({ 'added_dt': notification.added_dt, 'action_text': notification.action_text, 'action_url': notification.action_url, 'icon': notification.icon, 'id': notification.pk, 'level': notification.level, 'subject': notification.subject, }) return self.get_json_response(data)
[docs]class NotificationDetail(LoginRequiredMixin, BreadcrumbsMixin, DetailView): """Display the detail of a notification.""" model = NotificationUser context_object_name = "notification" template_name = "notifications/notification_detail.html"
[docs] def get_breadcrumbs(self): """Get breadcrumbs for notification detail""" crumbs = super().get_breadcrumbs() crumbs.add(_("Notifications"), reverse("notification_list")) crumbs.add(self.object.subject, "") return crumbs
[docs] def get_context_data(self, **kwargs): """Mark the notification as viewed.""" context = super().get_context_data(**kwargs) self.object.mark_viewed() return context
[docs] def get_queryset(self): """Also filter the object for the current user.""" qs = super().get_queryset() qs = qs.filter(user=self.request.user) return qs
[docs]class NotificationList(LoginRequiredMixin, BreadcrumbsMixin, ListView): """Display a list of notifications.""" model = NotificationUser context_object_name = "notifications" template_name = "notifications/notification_list.html"
[docs] def get_queryset(self): """Also filter the objects for the current user.""" qs = super().get_queryset() qs = qs.filter(user=self.request.user) return qs
# View Sets
[docs]class AjaxNotificationsViewSet(ViewSet): """Collect AJAX notification views.""" views = [ AjaxMarkNotificationRead, AjaxUnreadNotifications, ]
[docs]class NotificationsViewSet(ModelViewSet): """Collect standard notification views for end-users.""" views = [ NotificationDetail, NotificationList, ]