def trigger(notif): try: existing = Notification.objects.get(uid=notif.uid) #alert already generated #todo: add hook for amending or auto-dismissing alerts here (might not be the right place for auto-dismiss)? return 'alert already exists %s' % notif.uid except Notification.DoesNotExist: #new alert; save to db notif.initialize() notif.save() #'created' comment comment = NotificationComment(notification=notif, user=None, text='notification created') comment.save() def sms_send(user, content): try: conn = Connection.objects.get(contact__user=user) except: print 'user [%s] has no contact info; can\'t send sms alert' % user_name(user) logging.exception('error retriving contact info for user [%s]; can\'t send sms alert' % user_name(user)) return send_message(conn, content) print 'sent sms alert to [%s]' % user_name(user) notif.trigger_sms(sms_send) return 'new alert %s' % notif
def add_user_comment(alert, user, text): comment = NotificationComment( notification=alert, user=user, text=text ) comment.save() return comment
def trigger_notifications(): for notif in get_notifications(): try: existing = Notification.objects.get(uid=notif.uid) #alert already generated #todo: add hook for amending or auto-dismissing alerts here (might not be the right place for auto-dismiss)? print 'alert already exists', notif.uid except Notification.DoesNotExist: #new alert; save to db notif.initialize() notif.save() print 'new alert', notif #'created' comment comment = NotificationComment(notification=notif, user=None, text='notification created') comment.save() def sms_send(user, content): try: #conn = Connection.objects.get(contact__user=user) _conn = Connection.objects.filter(contact=user) if len(_conn) > 0: conn = _conn[0] else: conn = None except: #print 'user [%s] has no contact info; can\'t send sms alert' % user_name(user) logging.exception('error retriving contact info for healthprovider [%s]; can\'t send sms alert' % user.name) return #send_message(conn, content) if conn: Message.objects.create(direction='O', text=content, connection=conn, status='Q') #print 'sent sms alert to [%s]' % user_name(user) notif.trigger_sms(sms_send)