def handle_notifications(id): """ Handle notification types here. Available notification types are as follows: "use_redmine", ["use_email", "use_phone", "use_log", "use_sms"] Note: "use_redmine" is the only notification type available at this time. """ try: alert_alarm, alert_alarm_definition, notification = get_info_from_alert(id) if alert_alarm is None or alert_alarm_definition is None or notification is None: message = 'Failed to retrieve alert, definition or notification for handle_notifications. (id: %d)' % id current_app.logger.info('[handle_notifications] %s ' % message) raise Exception(message) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Process based on notification type(s) selected # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Notification type: red mine handle_redmine_notifications(id) # Notification type: Proposed (but unavailable) notification types. if notification.use_email or notification.use_phone or notification.use_log or notification.use_sms: message = 'Unavailable notification type selected (one of: use_email, use_phone, use_log or use_sms)' current_app.logger.info(message) # Notification type: Unknown else: message = 'No notification type selected; it is recommended MIOs be notified of alert or alarm activity.' current_app.logger.info(message) return except Exception as err: message = 'handle_notifications exception. %s' % err.message current_app.logger.info(message) return conflict(message)
def delete_array(id): try: array = Array.query.get_or_404(id) db.session.delete(array) db.session.commit() return jsonify({'message': 'Array deleted!', 'id': id}) except: return conflict('Invalid array id')
def update_user_event_notification(id): """ Update user_event_notification associated with SystemEventDefinition. """ log = False try: data = json.loads(request.data) # Get existing notification notification_id = data['id'] if notification_id != id: message = "Inconsistent ID, user_event_notification id provided in data does not match id provided." return bad_request(message) notification = UserEventNotification.query.filter_by( id=notification_id).first() #print '\n test notification...' if notification is None: message = "Invalid ID, user_event_notification record not found." #print '\n message: ', message return bad_request(message) # Validate user to be notified exists user = User.query.filter_by(id=notification.user_id).first() # Validate user_id matches id value user_id = data['user_id'] if user_id != notification.user_id: message = "Inconsistent User ID, user_id provided in data does not match id." return bad_request(message) notification.system_event_definition_id = data[ 'system_event_definition_id'] notification.user_id = data['user_id'] notification.use_email = data['use_email'] notification.use_redmine = data['use_redmine'] notification.use_phone = data['use_phone'] notification.use_log = data['use_log'] notification.use_sms = data['use_sms'] try: db.session.add(notification) db.session.commit() except Exception as err: db.session.rollback() return bad_request( 'IntegrityError creating user_event_notification.') #print '\n notification.to_json(): ', notification.to_json() return jsonify(notification.to_json()), 201 except Exception as err: #if log: print '\n (log) update_user_event_notification - exception: ', err.message return conflict('Insufficient data, or bad data format.')
def create_user_event_notification(): """ Create user_event_notification associated with SystemEventDefinition. Usage: Whenever a SystemEvent occurs, for the system_event_definition_id, this notification indicates who and how to contact them with the SystemEvent information. """ log = False try: data = json.loads(request.data) create_has_required_fields(data) system_event_definition_id = data['system_event_definition_id'] definition = SystemEventDefinition.query.get( system_event_definition_id) if definition is None: message = "Invalid SystemEventDefinition ID, SystemEventDefinition record not found." #if log: print '\n message: ', message return bad_request(message) #return jsonify(error=message), 404 # Validate user to be notified exists user_id = data['user_id'] user = User.query.filter_by(id=user_id).first() if not user: message = "Invalid User ID, User record not found." #if log: print '\n message: ', message return bad_request(message) # Create UserEventNotification notification = UserEventNotification() notification.system_event_definition_id = data[ 'system_event_definition_id'] notification.user_id = data['user_id'] notification.use_email = data['use_email'] notification.use_redmine = data['use_redmine'] notification.use_phone = data['use_phone'] notification.use_log = data['use_log'] notification.use_sms = data['use_sms'] try: db.session.add(notification) db.session.commit() except Exception as err: #if log: print '\n (log) create_user_event_notification - message: ', err.message db.session.rollback() return bad_request('IntegrityError creating notification') return jsonify(notification.to_json()), 201 except Exception as err: #if log: print '\n (log) create_user_event_notification - exception: ', err.message return conflict('Insufficient data, or bad data format.')
def create_array(): try: array_json = request.json if 'array_code' in array_json and 'display_name' in array_json and 'geo_location' in array_json: if array_json['array_code'] and array_json['display_name'] and array_json['geo_location']: array = Array.from_json(request.json) db.session.add(array) db.session.commit() return jsonify(array.to_json()), 201 else: raise Exception('One or more values are empty: array_code, array_name or geo_location.') else: raise Exception('Missing array_code, array_name and-or geo_location field in request.') except Exception, err: return conflict('Insufficient data, or bad data format: %s' % err.message)
def create_user_event_notification(): """ Create user_event_notification associated with SystemEventDefinition. Usage: Whenever a SystemEvent occurs, for the system_event_definition_id, this notification indicates who and how to contact them with the SystemEvent information. """ log = False try: data = json.loads(request.data) create_has_required_fields(data) system_event_definition_id = data['system_event_definition_id'] definition = SystemEventDefinition.query.get(system_event_definition_id) if definition is None: message = "Invalid SystemEventDefinition ID, SystemEventDefinition record not found." #if log: print '\n message: ', message return bad_request(message) #return jsonify(error=message), 404 # Validate user to be notified exists user_id = data['user_id'] user = User.query.filter_by(id=user_id).first() if not user: message = "Invalid User ID, User record not found." #if log: print '\n message: ', message return bad_request(message) # Create UserEventNotification notification = UserEventNotification() notification.system_event_definition_id = data['system_event_definition_id'] notification.user_id = data['user_id'] notification.use_email = data['use_email'] notification.use_redmine = data['use_redmine'] notification.use_phone = data['use_phone'] notification.use_log = data['use_log'] notification.use_sms = data['use_sms'] try: db.session.add(notification) db.session.commit() except Exception as err: #if log: print '\n (log) create_user_event_notification - message: ', err.message db.session.rollback() return bad_request('IntegrityError creating notification') return jsonify(notification.to_json()), 201 except Exception as err: #if log: print '\n (log) create_user_event_notification - exception: ', err.message return conflict('Insufficient data, or bad data format.')
def update_user_event_notification(id): """ Update user_event_notification associated with SystemEventDefinition. """ log = False try: data = json.loads(request.data) # Get existing notification notification_id = data['id'] if notification_id != id: message = "Inconsistent ID, user_event_notification id provided in data does not match id provided." return bad_request(message) notification = UserEventNotification.query.filter_by(id=notification_id).first() #print '\n test notification...' if notification is None: message = "Invalid ID, user_event_notification record not found." #print '\n message: ', message return bad_request(message) # Validate user to be notified exists user = User.query.filter_by(id=notification.user_id).first() # Validate user_id matches id value user_id = data['user_id'] if user_id != notification.user_id: message = "Inconsistent User ID, user_id provided in data does not match id." return bad_request(message) notification.system_event_definition_id = data['system_event_definition_id'] notification.user_id = data['user_id'] notification.use_email = data['use_email'] notification.use_redmine = data['use_redmine'] notification.use_phone = data['use_phone'] notification.use_log = data['use_log'] notification.use_sms = data['use_sms'] try: db.session.add(notification) db.session.commit() except Exception as err: db.session.rollback() return bad_request('IntegrityError creating user_event_notification.') #print '\n notification.to_json(): ', notification.to_json() return jsonify(notification.to_json()), 201 except Exception as err: #if log: print '\n (log) update_user_event_notification - exception: ', err.message return conflict('Insufficient data, or bad data format.')
def update_array(id): try: array = Array.query.get_or_404(id) array.array_code = request.json.get('array_code', array.array_code) array.description = request.json.get('description', array.description) array.geo_location = request.json.get('geo_location', array.geo_location) array.array_name = request.json.get('array_name', array.array_name) array.display_name = request.json.get('display_name', array.display_name) if array.array_code and array.display_name and (array.geo_location is not None): db.session.add(array) db.session.commit() return jsonify(array.to_json()) else: raise Exception('One or more values are empty: array_code, display_name or geo_location.') except Exception, err: return conflict('Insufficient data, or bad data format: %s' % err.message)
def get_user_event_notifications(): """ List all user_event_notifications (by user_id if provided) """ result = [] try: if "user_id" in request.args: user_id = request.args.get("user_id") user = User.query.get(user_id) if user is None: message = "Invalid User ID, User record not found." return bad_request(message) notifications = UserEventNotification.query.filter_by(user_id=user_id).all() else: notifications = UserEventNotification.query.all() if notifications: result = [notify.to_json() for notify in notifications] return jsonify({"notifications": result}) except: return conflict("Insufficient data, or bad data format.")
def update_user_event_notification(id): """ Update user_event_notification associated with SystemEventDefinition. """ try: data = json.loads(request.data) # Get existing notification notification_id = data["id"] if notification_id != id: message = "Inconsistent ID, user_event_notification id provided in data does not match id provided." return bad_request(message) notification = UserEventNotification.query.filter_by(id=notification_id).first() if notification is None: message = "Invalid ID, user_event_notification record not found." return bad_request(message) # Validate user to be notified exists user = User.query.filter_by(id=notification.user_id).first() # Validate user_id matches id value user_id = data["user_id"] if user_id != notification.user_id: message = "Inconsistent User ID, user_id provided in data does not match id." return bad_request(message) notification.system_event_definition_id = data["system_event_definition_id"] notification.user_id = data["user_id"] notification.use_email = data["use_email"] notification.use_redmine = data["use_redmine"] notification.use_phone = data["use_phone"] notification.use_log = data["use_log"] notification.use_sms = data["use_sms"] try: db.session.add(notification) db.session.commit() except: db.session.rollback() return bad_request("IntegrityError creating user_event_notification.") return jsonify(notification.to_json()), 201 except: return conflict("Insufficient data, or bad data format.")
def get_user_event_notifications(): """ List all user_event_notifications (by user_id if provided) """ result = [] try: if 'user_id' in request.args: user_id = request.args.get('user_id') user = User.query.get(user_id) if user is None: message = "Invalid User ID, User record not found." return bad_request(message) notifications = UserEventNotification.query.filter_by( user_id=user_id).all() else: notifications = UserEventNotification.query.all() if notifications: result = [notify.to_json() for notify in notifications] return jsonify({'notifications': result}) except: return conflict('Insufficient data, or bad data format.')