def events_notify(request): notifier = NotifierQueue(EventNotification) try: log = notifier.send_emails(_("You have new alert(s) " "in Network Administrator"), clear_queue=True) except NotifierEmptyQueue: log = [] if log: response = "<p>Emails sent:</p>%s" % "<br />".join(log) else: response = "<p>No emails to send</p>" return HttpResponse(response)
def create(self, request): """ Receives one or more notifications and saves them to the database. This method is a part of private API. Method: POST URL: /api/event/report/ Case 1: Reporting single event (notification) --------------------------------------------- Request parameters: * description - message describing the event * short_description - shorter message, e.g. to use in list * timestamp - when the event occurred * event_type - type of the event which should also describe its importance * protocol - network protocol related to the event * hostname - name of the source host * source_host_ipv4, source_host_ipv6 - IPv4 and IPv6 addresses of the source host * fields_class - monitoring module identifier Any additional data provided with the event will be serialized and saved together with fields described above. Response: * status - **ok** or **error** * message - details of the result Case 2: Reporting multiple events at once ----------------------------------------- Request parameters: * events - list of events serialized with JSON Response: * status - **ok** or **error** * message - details of the result """ notifier = NotifierQueue(EventNotification) if request.POST.get('events'): try: events = json.loads(request.POST.get('events', '')) except ValueError: return api_error(_('No events could be read')) for event_dict in events: try: event_data = get_event_data(request, event_dict) except EventParseError, e: message = str(e) return api_error(_(message)) event = Event(**event_data) event.save() if event.event_type.notify: notifier.push(user=request.user, event=event) return api_ok(_('Events reported successfully'))