Hide keyboard shortcuts

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 

10 

11# Exports 

12 

13__all__ = ( 

14 "AjaxMarkNotificationRead", 

15 "AjaxNotificationsViewSet", 

16 "AjaxUnreadNotifications", 

17 "NotificationDetail", 

18 "NotificationList", 

19 "NotificationsViewSet", 

20) 

21 

22# Views 

23 

24 

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 

29 

30 # noinspection PyUnusedLocal 

31 def get(self, request, *args, **kwargs): 

32 link_id = self.kwargs['pk'] 

33 

34 criteria = { 

35 'pk': link_id, 

36 'user': request.user, 

37 } 

38 try: 

39 link = NotificationUser.objects.get(**criteria) 

40 link.mark_viewed() 

41 

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."}) 

45 

46 

47class AjaxUnreadNotifications(LoginRequiredMixin, JSONMixin, View): 

48 """Get unread notifications in real time.""" 

49 pattern_name = "ajax_unread_notifications" 

50 pattern_value = "notifications/unread/" 

51 

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 }) 

65 

66 return self.get_json_response(data) 

67 

68 

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" 

74 

75 def get_breadcrumbs(self): 

76 crumbs = super().get_breadcrumbs() 

77 

78 crumbs.add(_("Notifications"), reverse("notification_list")) 

79 crumbs.add(self.object.subject, "") 

80 

81 return crumbs 

82 

83 def get_context_data(self, **kwargs): 

84 """Mark the notification as viewed.""" 

85 context = super().get_context_data(**kwargs) 

86 

87 self.object.mark_viewed() 

88 

89 return context 

90 

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 

96 

97 

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" 

103 

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 

109 

110# View Sets 

111 

112 

113class AjaxNotificationsViewSet(ViewSet): 

114 """Collect AJAX notification views.""" 

115 views = [ 

116 AjaxMarkNotificationRead, 

117 AjaxUnreadNotifications, 

118 ] 

119 

120 

121class NotificationsViewSet(ModelViewSet): 

122 """Collect standard notification views for end-users.""" 

123 views = [ 

124 NotificationDetail, 

125 NotificationList, 

126 ]