Ejemplo n.º 1
0
def classification():
    form = ClassificationForm()
    models = get_models_from_root('./models')
    form.classification_model.choices = models
    data = []
    predictions = []
    success = False
    error_found = True
    length = 0
    if form.validate_on_submit():
        input_file = request.files['input_file']
        if input_file and allowed_file(input_file.filename,
                                       ALLOWED_EXTENSIONS):
            filename = secure_filename(input_file.filename)
            input_file.save(os.path.join(app.config['UPLOAD_FOLDER'],
                                         filename))
        modelname = form.classification_model.data
        file = f'{UPLOAD_FOLDER}{filename}'
        try:
            data, predictions = predict_ddos_attack(modelname, file)
            length = len(predictions)
            success = True
        except:
            error_found = True
            return 'Something goes wrong! Try again', 400
    return render_template('classification.html',
                           form=form,
                           data=data,
                           predictions=predictions,
                           length=length,
                           success=success,
                           error_found=error_found,
                           attacks=ATTACKS)
Ejemplo n.º 2
0
def edit_classification(request, pk):
    classification = Classification.objects.get(pk=pk)
    form = ClassificationForm(request.POST or None, instance=classification)
    if request.method == "POST" and form.is_valid():
        if "cancel" in request.POST:
            return redirect('/annotations/classifications/')
        with reversion.create_revision():
            form.save()
            reversion.set_user(request.user)
            comment = request.POST['comment'] or "Changed classification"
            reversion.set_comment(comment)
            log(request, classification, comment)
        return redirect('/annotations/classification/%s' % pk)
    ctx = {'classification': classification, 'form': form, 'action': 'Edit'}
    return render_to_response('annotations/classification_form.html',
                              ctx,
                              context_instance=RequestContext(request))
Ejemplo n.º 3
0
def add_classification(request, template='annotations/classification_form.html'):
    form = ClassificationForm(request.POST or None)
    if request.method == "POST" and form.is_valid():
        with reversion.create_revision():
            classification = form.save(commit=False)
            form.save()
            if isinstance(request.user, AnonymousUser):
                request.user = User.objects.get(username="******")
            reversion.set_user(request.user)
            comment = request.POST['comment'] or "Added classification"
            reversion.set_comment(comment)
            log(request, classification, comment)
            msg = "Successfully added classification."
            messages.add_message(request, messages.SUCCESS, _(msg))
            return redirect('/annotations/classification/%s' %
                            classification.pk)
    return render(request, template, {'form': form, 'action': 'Add'})
Ejemplo n.º 4
0
def edit_classification(request, pk):
    classification = Classification.objects.get(pk=pk)
    form = ClassificationForm(request.POST or None, instance=classification)
    if request.method == "POST" and form.is_valid():
        if "cancel" in request.POST:
            return redirect('/annotations/classifications/')
        with reversion.create_revision():
            form.save()
            reversion.set_user(request.user)
            comment = request.POST['comment'] or "Changed classification"
            reversion.set_comment(comment)
            log(request, classification, comment)
        return redirect('/annotations/classification/%s' % pk)
    ctx = {'classification': classification,
           'form': form,
           'action': 'Edit'}
    return render_to_response('annotations/classification_form.html', ctx,
        context_instance=RequestContext(request))
Ejemplo n.º 5
0
def add_classification(request,
                       template='annotations/classification_form.html'):
    form = ClassificationForm(request.POST or None)
    if request.method == "POST" and form.is_valid():
        with reversion.create_revision():
            classification = form.save(commit=False)
            form.save()
            if isinstance(request.user, AnonymousUser):
                request.user = User.objects.get(username="******")
            reversion.set_user(request.user)
            comment = request.POST['comment'] or "Added classification"
            reversion.set_comment(comment)
            log(request, classification, comment)
            msg = "Successfully added classification."
            messages.add_message(request, messages.SUCCESS, _(msg))
            return redirect('/annotations/classification/%s' %
                            classification.pk)
    return render(request, template, {'form': form, 'action': 'Add'})