示例#1
0
def asset_editor(request, data):
    try:
        asset = Asset.objects.filter(sn=data).prefetch_related('tags')[0]
    except Exception:
        return redirect(reverse('cmdb:asset-add'))
    if request.method == 'POST':
        JSON_RESULT = {'status': 200, 'message': '', 'error': '', 'data': []}
        forms = AssetForm(data=request.POST, instance=asset)
        if forms.is_valid():
            instance = forms.save()
            label = forms.cleaned_data.get('label', '').split(',')
            tags = []
            for t in label:
                t = t.strip()
                if t:
                    tag, flag = Tag.objects.get_or_create(name=t)
                    tags.append(tag)
            if tags:
                instance.tags.set(tags)
            JSON_RESULT['message'] = '更新成功'
            return JsonResponse(JSON_RESULT)
        JSON_RESULT['status'] = 201
        JSON_RESULT['error'] = forms.errors.as_json()
        return JsonResponse(JSON_RESULT)

    else:
        forms = AssetForm(instance=asset)
    kwargs = {
        'html_title': '更新资产',
        'cancel': reverse('cmdb:asset'),
        'col_md': 'col-md-5',
        'forms': forms,
        'header_temp': 'header.html',
    }
    return render(request, 'cmdb/asset/asset-editor.html', kwargs)
示例#2
0
def asset_add(request):
    if request.method == 'POST':
        JSON_RESULT = {'status': 200, 'message': '', 'error': '', 'data': []}
        forms = AssetForm(data=request.POST)
        if forms.is_valid():
            instance = forms.save()
            print(forms.cleaned_data.get('label'))
            label = forms.cleaned_data.get('label', '').split(',')
            tags = []
            for t in label:
                t = t.strip()
                if t:
                    tag, flag = Tag.objects.get_or_create(name=t)
                    tags.append(tag)
            if tags:
                instance.tags.add(*tags)
            JSON_RESULT['message'] = '添加成功'
            return JsonResponse(JSON_RESULT)
        JSON_RESULT['status'] = 201
        JSON_RESULT['error'] = forms.errors.as_json()
        return JsonResponse(JSON_RESULT)
    else:
        forms = AssetForm()
    kwargs = {
        'html_title': '添加资产',
        'cancel': reverse('cmdb:asset'),
        'col_md': 'col-md-4',
        'forms': forms,
        'header_temp': 'header.html',
    }
    return render(request, 'cmdb/asset/asset-add.html', kwargs)
示例#3
0
def asset_add(request):
    if request.method == "POST":
        a_form = AssetForm(request.POST)
        if a_form.is_valid():
            a_form.save()
            tips = u"增加成功!"
            display_control = ""
        else:
            tips = u"增加失败!"
            display_control = ""
        return render(request, "cmdb/asset_add.html", locals())
    else:
        display_control = "none"
        a_form = AssetForm()
        return render(request, "cmdb/asset_add.html", locals())
示例#4
0
文件: asset.py 项目: shunzi115/AntOps
def asset_edit(request, ids):
    status = 0
    # asset_types = ASSET_TYPE
    obj = get_object(models.Host, id=ids)
    if request.method == 'POST':
        form = AssetForm(request.POST, instance=obj)
        if form.is_valid():
            form.save()
            status = 1
        else:
            status = 2
    else:
        af = AssetForm(instance=obj)

    return render(request, 'cmdb/asset_edit.html', locals())
示例#5
0
def asset_edit(request, asset_id):
    project = Host.objects.get(id=asset_id)
    if request.method == 'POST':
        form = AssetForm(request.POST, instance=project)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('cmdb'))
    else:
        form = AssetForm(instance=project)
    display_control = "none"
    results = {
        'asset_form': form,
        'asset_id': asset_id,
        'request': request,
        'display_control': display_control,
    }
    return render(request, 'cmdb/asset_base.html', results)
示例#6
0
def asset_add(request):
    if request.method == "POST":
        asset_form = AssetForm(request.POST)
        if asset_form.is_valid():
            asset_form.save()
            tips = u"增加成功!"
            display_control = ""
        else:
            tips = u"增加失败!"
            display_control = ""
        # 添加完成后,跳转到列表页面
        # 不使用return render, 防止重复提交表单, 使用重定向url才一致
        return HttpResponseRedirect(reverse('cmdb'))
    else:
        display_control = "none"
        asset_form = AssetForm()
        return render(request, "cmdb/asset_base.html", locals())