def edit_threshold(request, rule_id): """Controller for editing threshold rules""" rule = get_object_or_404(ThresholdRule, pk=rule_id) if request.method == 'POST': form = ThresholdForm(request.POST, instance=rule) metric = request.POST.get('target') if form.is_valid(): handle_threshold_form(form, request) return redirect('threshold-index') else: form = ThresholdForm(instance=rule) metric = rule.target heading = 'Edit threshold rule' navpath = get_path() + [(heading, )] title = create_title(navpath) context = { 'heading': heading, 'form': form, 'metric': metric, 'title': title, 'navpath': navpath, 'id': rule.id, } return render(request, 'threshold/set_threshold.html', context)
def add_threshold(request, metric=None): """Controller for threshold search""" if request.method == 'POST': form = ThresholdForm(request.POST) metric = request.POST.get('target') if form.is_valid(): handle_threshold_form(form, request) return redirect('threshold-index') else: if metric: form = ThresholdForm(initial={'target': metric}) else: form = ThresholdForm() heading = 'Add threshold rule' navpath = get_path() + [(heading, )] title = create_title(navpath) context = { 'heading': heading, 'form': form, 'metric': metric, 'title': title, 'navpath': navpath, 'id': None, } return render(request, 'threshold/set_threshold.html', context)
def test_thresholdform_with_invalid_period_should_not_validate(): form = ThresholdForm( dict( target='some.graphite.metric', alert='>15', clear='<14', period='bad value', )) assert not form.is_valid()
def test_filled_thresholdform_should_validate(): form = ThresholdForm( dict( target='some.graphite.metric', alert='>15', clear='<14', period='15m', )) assert form.is_valid()
def test_thresholdform_with_invalid_alert_expression_should_not_validate(): form = ThresholdForm( dict( target='some.graphite.metric', alert='>><15', clear='<14', period='10m', )) assert not form.is_valid()
def test_empty_thresholdform_should_not_validate(): form = ThresholdForm() assert not form.is_valid()