1010
1111
1212def create_notification (event = None , * args , ** kwargs ):
13- # System notifications
14- try :
15- system_notifications = Notifications .objects .get (user = None )
16- except Exception :
17- system_notifications = Notifications ()
18-
19- logger .debug ('creating system notifications' )
20- process_notifications (event , system_notifications , * args , ** kwargs )
2113
2214 if 'recipients' in kwargs :
23- # mimic existing code so that when recipients is specified, no other personal notifications are sent.
15+ # mimic existing code so that when recipients is specified, no other system or personal notifications are sent.
2416 logger .debug ('creating notifications for recipients' )
25- for recipient_notifications in Notifications .objects .filter (user__username__in = kwargs ['recipients' ], user__is_active = True ):
17+ for recipient_notifications in Notifications .objects .filter (user__username__in = kwargs ['recipients' ], user__is_active = True , product = None ):
2618 # kwargs.update({'user': recipient_notifications.user})
2719 process_notifications (event , recipient_notifications , * args , ** kwargs )
2820 else :
21+ logger .debug ('creating system notifications' )
22+ # send system notifications to all admin users
23+
24+ # System notifications
25+ try :
26+ system_notifications = Notifications .objects .get (user = None )
27+ except Exception :
28+ system_notifications = Notifications ()
29+
30+ admin_users = Dojo_User .objects .filter (is_staff = True )
31+ for admin_user in admin_users :
32+ system_notifications .user = admin_user
33+ process_notifications (event , system_notifications , * args , ** kwargs )
34+
2935 # Personal but global notifications
3036 # only retrieve users which have at least one notification type enabled for this event type.
3137 logger .debug ('creating personal notifications' )
@@ -71,8 +77,8 @@ def create_notification_message(event, user, notification_type, *args, **kwargs)
7177 try :
7278 notification = render_to_string (template , kwargs )
7379 except Exception as e :
74- logger .exception ( e )
75- create_description (event )
80+ logger .debug ( 'template not found or not implemented yet: %s' , template )
81+ create_description (event , * args , ** kwargs )
7682 notification = render_to_string ('notifications/other.tpl' , kwargs )
7783
7884 return notification
@@ -87,9 +93,9 @@ def process_notifications(event, notifications=None, *args, **kwargs):
8793
8894 sync = 'initiator' in kwargs and hasattr (kwargs ['initiator' ], 'usercontactinfo' ) and kwargs ['initiator' ].usercontactinfo .block_execution
8995
90- logger .debug ('sync: %s' , sync )
96+ logger .debug ('sync: %s %s ' , sync , notifications . user )
9197 logger .debug ('sending notifications ' + ('synchronously' if sync else 'asynchronously' ))
92- logger .debug (vars (notifications ))
98+ # logger.debug(vars(notifications))
9399
94100 slack_enabled = get_system_setting ('enable_slack_notifications' )
95101 hipchat_enabled = get_system_setting ('enable_hipchat_notifications' )
@@ -115,7 +121,6 @@ def process_notifications(event, notifications=None, *args, **kwargs):
115121 else :
116122 send_mail_notification (event , notifications .user , * args , ** kwargs )
117123
118- print (getattr (notifications , event , None ))
119124 if 'alert' in getattr (notifications , event , None ):
120125 if not sync :
121126 send_alert_notification_task .delay (event , notifications .user , * args , ** kwargs )
@@ -125,25 +130,25 @@ def process_notifications(event, notifications=None, *args, **kwargs):
125130
126131def send_slack_notification (event , user = None , * args , ** kwargs ):
127132 from dojo .utils import get_system_setting , get_slack_user_id
128- if user is not None :
129- if hasattr (user , 'usercontactinfo' ) and user .usercontactinfo .slack_username is not None :
130- slack_user_id = user .usercontactinfo .slack_user_id
131- if user .usercontactinfo .slack_user_id is None :
132- # Lookup the slack userid
133- slack_user_id = get_slack_user_id (
134- user .usercontactinfo .slack_username )
135- slack_user_save = UserContactInfo .objects .get (user_id = user .id )
136- slack_user_save .slack_user_id = slack_user_id
137- slack_user_save .save ()
138-
139- channel = '@%s' % slack_user_id
133+ try :
134+ if user is not None :
135+ if hasattr (user , 'usercontactinfo' ) and user .usercontactinfo .slack_username is not None :
136+ slack_user_id = user .usercontactinfo .slack_user_id
137+ if user .usercontactinfo .slack_user_id is None :
138+ # Lookup the slack userid
139+ slack_user_id = get_slack_user_id (
140+ user .usercontactinfo .slack_username )
141+ slack_user_save = UserContactInfo .objects .get (user_id = user .id )
142+ slack_user_save .slack_user_id = slack_user_id
143+ slack_user_save .save ()
144+
145+ channel = '@%s' % slack_user_id
146+ else :
147+ # user has no slack username, skip
148+ return
140149 else :
141- # user has no slack username, skip
142- return
143- else :
144- channel = get_system_setting ('slack_channel' )
150+ channel = get_system_setting ('slack_channel' )
145151
146- try :
147152 res = requests .request (
148153 method = 'POST' ,
149154 url = 'https://slack.com/api/chat.postMessage' ,
@@ -161,11 +166,11 @@ def send_slack_notification(event, user=None, *args, **kwargs):
161166
162167def send_hipchat_notification (event , user = None , * args , ** kwargs ):
163168 from dojo .utils import get_system_setting
164- if user :
165- # HipChat doesn't seem to offer direct message functionality, so no HipChat PM functionality here...
166- return
167-
168169 try :
170+ if user :
171+ # HipChat doesn't seem to offer direct message functionality, so no HipChat PM functionality here...
172+ return
173+
169174 # We use same template for HipChat as for slack
170175 res = requests .request (
171176 method = 'POST' ,
@@ -185,16 +190,17 @@ def send_hipchat_notification(event, user=None, *args, **kwargs):
185190
186191def send_mail_notification (event , user = None , * args , ** kwargs ):
187192 from dojo .utils import get_system_setting
193+ try :
188194
189- if user :
190- address = user .email
191- else :
192- address = get_system_setting ('mail_notifications_to' )
195+ if user :
196+ address = user .email
197+ else :
198+ address = get_system_setting ('mail_notifications_to' )
199+
200+ subject = '%s notification' % get_system_setting ('team_name' )
201+ if 'title' in kwargs :
202+ subject += ': %s' % kwargs ['title' ]
193203
194- subject = '%s notification' % get_system_setting ('team_name' )
195- if 'title' in kwargs :
196- subject += ': %s' % kwargs ['title' ]
197- try :
198204 email = EmailMessage (
199205 subject ,
200206 create_notification_message (event , user , 'mail' , * args , ** kwargs ),
@@ -214,18 +220,28 @@ def send_mail_notification(event, user=None, *args, **kwargs):
214220
215221
216222def send_alert_notification (event , user = None , * args , ** kwargs ):
217- icon = kwargs .get ('icon' , 'info-circle' )
218- alert = Alerts (
219- user_id = user ,
220- title = kwargs .get ('title' ),
221- description = create_notification_message (event , user , 'alert' , * args , ** kwargs ),
222- url = kwargs .get ('url' , reverse ('alerts' )),
223- icon = icon ,
224- source = Notifications ._meta .get_field (event ).verbose_name .title ())
225- alert .save ()
223+ print ('sending alert notification' )
224+ try :
225+ icon = kwargs .get ('icon' , 'info-circle' )
226+ alert = Alerts (
227+ user_id = user ,
228+ title = kwargs .get ('title' )[:100 ],
229+ description = create_notification_message (event , user , 'alert' , * args , ** kwargs ),
230+ url = kwargs .get ('url' , reverse ('alerts' )),
231+ icon = icon [:25 ],
232+ source = Notifications ._meta .get_field (event ).verbose_name .title ()[:100 ]
233+ )
234+ # relative urls will fail validation
235+ alert .clean_fields (exclude = ['url' ])
236+ alert .save ()
237+ except Exception as e :
238+ logger .exception (e )
239+ log_alert (e , * args , ** kwargs )
240+ pass
226241
227242
228243def log_alert (e , * args , ** kwargs ):
244+ # no try catch here, if this fails we need to show an error
229245 users = Dojo_User .objects .filter (is_superuser = True )
230246 for user in users :
231247 alert = Alerts (
@@ -235,4 +251,6 @@ def log_alert(e, *args, **kwargs):
235251 description = "%s" % e ,
236252 icon = "exclamation-triangle" ,
237253 source = "Notifications" )
254+ # relative urls will fail validation
255+ alert .clean_fields (exclude = ['url' ])
238256 alert .save ()
0 commit comments