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""" 

2Abstract 

3-------- 

4 

5The notifications app is primarily meant to provide in-app (e.g. in-project) notifications, but may also be used to send 

6email and SMS when supported. 

7 

8Install 

9------- 

10 

11Add ``superdjango.contrib.notifications.apps.DefaultConfig`` to ``INSTALLED_APPS``. 

12 

13Run ``./manage.py migrate``. 

14 

15Sending Email 

16............. 

17 

18If you will be sending notifications via email: 

19 

201. Define the email address from which the message is sent using ``SUPERDJANGO_NOTIFICATIONS_FROM_EMAIL``. 

212. Define the from name of the email address using ``SUPERDJANGO_NOTIFICATIONS_FROM_NAME``. 

22 

23Sending SMS/Text 

24................ 

25 

26If you will be sending notifications via SMS, you'll need to create a callback which integrates with a text messaging 

27service such as Twilio. 

28 

29The callback must accept a body, mobile number, and optionally a from number. For example: 

30 

31.. code-block:: python 

32 

33 def send_sms(body, number, from_number=None): 

34 # ... 

35 

36Set ``SUPERDJANGO_NOTIFICATIONS_SMS_CALLBACK`` to the dotted path of the callback. Optionally set the 

37``SUPERDJANGO_NOTIFICATIONS_FROM_NUMBER``. 

38 

39Usage 

40----- 

41 

42The notifications system allows developers to queue up notifications for users. It is also possible for administrators 

43or authorized users to create notifications using the provided UI (see below). 

44 

45Creating a Notification Programmatically 

46........................................ 

47 

48To create a notification for a user: 

49 

50.. code-block:: python 

51 

52 from django.contrib.auth import get_user_model 

53 from superdjango.contrib.notifications.models import Notification 

54 

55 UserModel = get_user_model() 

56 

57 body = "This is a test notification." 

58 subject = "Testing 123" 

59 users = [ 

60 UserModel.objects.get(pk=1), 

61 UserModel.objects.get(pk=2), 

62 UserModel.objects.get(pk=3), 

63 ] 

64 notification = Notification.create_for(body, subject, users) 

65 

66You can of course pass in a list of users with only one user instance. 

67 

68Sending Notifications 

69..................... 

70 

71The notifications app makes use of a queue pattern where the actual links to a user (and optional email/SMS) must be 

72executed on a regular schedule. This is the purpose of the ``send_notifications`` management command. 

73 

74Create a shell script which loads the Python environment and executes the command: 

75 

76.. code-block:: bash 

77 

78 #! /usr/bin/env bash 

79 

80 cd /path/to/project; 

81 source python/bin/activate; 

82 cd source; 

83 ./manage.py send_notifications; 

84 

85Then add the script to a cron job: 

86 

87.. code-block:: text 

88 

89 # send notifications every 15 minutes 

90 */15 * * * * root /path/to/project/cron/send_notifications.sh 

91 

92Displaying Notifications 

93........................ 

94 

95Various template tags are provided for displaying a user's notifications in your templates. All require the current user 

96as a parameter. 

97 

98``has_notifications``: Indicates whether a user has unread notifications. 

99 

100.. code-block:: html 

101 

102 {% if has_notifications request.user %} 

103 <div class="alert alter-info">You have some notifications.</div> 

104 {% endif %} 

105 

106``notification_bar``: Utilizes the ``notifications/tags/notification_bar.html`` template to display a promoted 

107notification at the top of a page. 

108 

109.. code-block:: html 

110 

111 <head> 

112 ... 

113 <link rel="stylesheet" href="{% static "superdjango/contrib/notifications/css/notification-bar.css" %}"> 

114 </head> 

115 <body> 

116 {% notification_bar request.user %} 

117 ... 

118 

119 <script src="{% static "superdjango/contrib/notifications/js/notification-bar.js" %}"></script> 

120 </body> 

121 

122If the AJAX views are set up using the automatic method (below), you may replace ``notification-bar.js`` with 

123``notification-bar-ajax.js``. Otherwise, additional work is required to override ``notification-bar-ajax.js`` so that 

124it refers to the correct URL. 

125 

126``unread_notification_count``: Returns the number of unread notifications. 

127 

128.. code-block:: html 

129 

130 <div class="notifications"> 

131 <i class="fas fa-bell"></i> Notifications 

132 <span class="badge badge-danger">{% unread_notification_count request.user %}</span> 

133 </div> 

134 

135``unread_notifications``: Utilizes the ``notifications/tags/notification_items.html`` template to display a list of 

136notifications. 

137 

138.. code-block:: html 

139 

140 <div class="notifications"> 

141 {% unread_notifications request.user %} 

142 </div> 

143 

144Finally, the included ``notify_web_api.html`` template may be used to display in-browser messages for promoted 

145notifications. 

146 

147.. code-block:: html 

148 

149 <body> 

150 ... 

151 {% include "notifications/includes/notify_web_api.html" %} 

152 </body> 

153 

154Utilizing the Standard Views 

155............................ 

156 

157Detail and list views are provided for displaying notifications to users. 

158 

159.. code-block:: python 

160 

161 # main/urls.py 

162 from superdjango.contrib.notifications.views import NotificationsViewSet 

163 

164 urlpatterns = [ 

165 path("notifications/", include(NotificationsViewSet().get_urls())), 

166 ] 

167 

168Utilizing the AJAX Views 

169........................ 

170 

171The notification apps provides two (2) AJAX views; one for obtaining unread notifications, and the other for marking a 

172notification as read. 

173 

174To automatically include these views, simply add ``superdjango.interfaces.ajax.apps.AutoConfig`` to your 

175``INSTALLED_APPS`` and include the AJAX URLs in your main ``urls.py`` file. See :ref:`interfaces-interfaces-ajax`. 

176 

177You may also manually include the views: 

178 

179.. code-block:: python 

180 

181 # main/urls.py 

182 from superdjango.contrib.notifications.views import AjaxNotificationsViewSet 

183 

184 urlpatterns = [ 

185 # ... 

186 path("ajax/notify/", include(AjaxNotificationsViewSet().get_urls())), 

187 # ... 

188 ] 

189 

190Setting up the UI 

191................. 

192 

193The notification apps comes with pre-defined configuration for SuperDjango UI. You may use it as is by registering the 

194``NotificationsMenu`` with the UI, or customize as desired. 

195 

196.. code-block:: python 

197 

198 # glue/ui.py 

199 from superdjango import ui 

200 from superdjango.contrib.notifications.ui import NotificationsMenu 

201 

202 ui.site.register(NotificationsMenu) 

203 

204""" 

205__author__ = "Shawn Davis <shawn@superdjango.com>" 

206__maintainer__ = "Shawn Davis <shawn@superdjango.com>" 

207__version__ = "0.4.0-x" 

208 

209# https://www.w3schools.com/howto/howto_css_notification_button.asp 

210# https://www.html5rocks.com/en/tutorials/notifications/quick/ 

211# https://github.com/alexgibson/notify.js