Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
2from django.urls import reverse
3from django.utils.translation import gettext_lazy as _
4from django.views import View
5from superdjango.patterns import PATTERN_PK
6from superdjango.views import BreadcrumbsMixin, DetailView, JSONMixin, ListView, LoginRequiredMixin, ModelViewSet, \
7 ViewSet
8from .models import NotificationUser
9from .shortcuts import get_unread_notifications
11# Exports
13__all__ = (
14 "AjaxMarkNotificationRead",
15 "AjaxNotificationsViewSet",
16 "AjaxUnreadNotifications",
17 "NotificationDetail",
18 "NotificationList",
19 "NotificationsViewSet",
20)
22# Views
25class AjaxMarkNotificationRead(LoginRequiredMixin, JSONMixin, View):
26 """Mark a notification as viewed."""
27 pattern_name = "ajax_mark_notification_read"
28 pattern_value = "notifications/mark/read/%s/" % PATTERN_PK
30 # noinspection PyUnusedLocal
31 def get(self, request, *args, **kwargs):
32 link_id = self.kwargs['pk']
34 criteria = {
35 'pk': link_id,
36 'user': request.user,
37 }
38 try:
39 link = NotificationUser.objects.get(**criteria)
40 link.mark_viewed()
42 return self.get_json_response({'success': "Notification has been viewed."})
43 except NotificationUser.DoesNotExist:
44 return self.get_json_response({'error': "Notification does not exist."})
47class AjaxUnreadNotifications(LoginRequiredMixin, JSONMixin, View):
48 """Get unread notifications in real time."""
49 pattern_name = "ajax_unread_notifications"
50 pattern_value = "notifications/unread/"
52 # noinspection PyUnusedLocal
53 def get(self, request, *args, **kwargs):
54 data = list()
55 for notification in get_unread_notifications(request.user):
56 data.append({
57 'added_dt': notification.added_dt,
58 'action_text': notification.action_text,
59 'action_url': notification.action_url,
60 'icon': notification.icon,
61 'id': notification.pk,
62 'level': notification.level,
63 'subject': notification.subject,
64 })
66 return self.get_json_response(data)
69class NotificationDetail(LoginRequiredMixin, BreadcrumbsMixin, DetailView):
70 """Display the detail of a notification."""
71 model = NotificationUser
72 context_object_name = "notification"
73 template_name = "notifications/notification_detail.html"
75 def get_breadcrumbs(self):
76 crumbs = super().get_breadcrumbs()
78 crumbs.add(_("Notifications"), reverse("notification_list"))
79 crumbs.add(self.object.subject, "")
81 return crumbs
83 def get_context_data(self, **kwargs):
84 """Mark the notification as viewed."""
85 context = super().get_context_data(**kwargs)
87 self.object.mark_viewed()
89 return context
91 def get_queryset(self):
92 """Also filter the object for the current user."""
93 qs = super().get_queryset()
94 qs = qs.filter(user=self.request.user)
95 return qs
98class NotificationList(LoginRequiredMixin, BreadcrumbsMixin, ListView):
99 """Display a list of notifications."""
100 model = NotificationUser
101 context_object_name = "notifications"
102 template_name = "notifications/notification_list.html"
104 def get_queryset(self):
105 """Also filter the objects for the current user."""
106 qs = super().get_queryset()
107 qs = qs.filter(user=self.request.user)
108 return qs
110# View Sets
113class AjaxNotificationsViewSet(ViewSet):
114 """Collect AJAX notification views."""
115 views = [
116 AjaxMarkNotificationRead,
117 AjaxUnreadNotifications,
118 ]
121class NotificationsViewSet(ModelViewSet):
122 """Collect standard notification views for end-users."""
123 views = [
124 NotificationDetail,
125 NotificationList,
126 ]