Ejemplo n.º 1
0
def create_website_event(request):
    if request.method == 'POST':
        form = WebsiteEventForm(request.POST)
        if form.is_valid():
            website = form.cleaned_data['website']
            first_detection = form.cleaned_data['first_detection']
            event_type = form.cleaned_data['event_type']
            location = Location.retrieve_location(form.cleaned_data['location'].split(', ')[0])

            logging.info(location)

            event = Event()
            event.active = True
            if first_detection:
                event.first_detection_utc = first_detection
            else:
                event.first_detection_utc = datetime.datetime.now()
            event.last_detection_utc  = datetime.datetime.now()
            event.target = website
            event.target_type = TargetType.Website
            if event_type:
                # 'censor', 'throttling', 'offline'
                if event_type == 'censor':
                    event.event_type = EventType.Censor
                elif event_type == 'throttling':
                    event.event_type = EventType.Throttling
                else:
                    event.event_type = EventType.Offline
            else:
                event.event_type = EventType.Offline

            logging.info(location)

            if location!=None:
                event.location_ids.append(location.id)
                event.location_names.append(location.fullname)
                event.location_country_names.append(location.country_name)
                event.location_country_codes.append(location.country_code)
                event.lats.append(location.lat)
                event.lons.append(location.lon)
                event.isps.append('')

            event.save()
            NotificationSystem.publishEvent(event)

            return HttpResponse(json.dumps(dict(status='OK',
                       msg='Website event added successfully!',
                       errors=None)))
        else:
            return HttpResponse(json.dumps(dict(status='FAILED',
                    msg='Failed to add event.',
                    errors=form.errors)))
    
    form = WebsiteEventForm()
    return render_to_response('gui/create_website_event.html', locals(), context_instance=RequestContext(request))