Ejemplo n.º 1
0
def virtual_platform_edit(request):
    """
    virtual_platform edit view
    """
    header_title, path1, path2 = u'编辑平台', u'资产管理', u'编辑平台'
    platform_id = request.GET.get('id', '')
    platform = get_object(Virtual_platform, id=platform_id)
    host = get_object(Host_info,id=platform.host_info.id)
    idrac = get_object(Idrac,id=host.Idrac.id)
    if request.method == 'POST':
        platform_form = VirtualForm(request.POST,instance=platform)
        host_form  = HostForm(request.POST, instance=host)
        idrac_form = IdracForm(request.POST,instance=idrac)
        # print host_form
        # print idrac_form
        if all((platform_form.is_valid(),host_form.is_valid(),idrac_form.is_valid())):
            platform_model = platform_form.save(commit=False)
            host_model = host_form.save(commit=False)
            host_model.Idrac = idrac_form.save()
            host_model.env_type = "virtual"
            host_model.save()
            platform_model.host_info = host_model
            platform_model.save()
            return HttpResponseRedirect(reverse('virtual_platform_list'))
        else:
            emg = "error"
            return my_render('jasset/virtual_platform_edit.html', locals(), request)
    else:
        platform_form = VirtualForm(instance=platform)
        host_form = HostForm(instance=host)
        idrac_form = IdracForm(instance=idrac)
        print host_form,idrac_form
        return my_render('jasset/virtual_platform_edit.html', locals(), request)
Ejemplo n.º 2
0
def virtual_platform_add(request):
    """
    virtual_platform   add view
    """
    header_title, path1, path2 = u'添加虚拟化平台', u'资产管理', u'添加虚拟化平台'
    if request.method == 'POST':
        platform_form  = VirtualForm(request.POST)
        host_form = AssetForm(request.POST)
        idrac_form = IdracForm(request.POST)
        # print platform_form
        # print host_form
        # print idrac_form
        if all((platform_form.is_valid(), host_form.is_valid()) ):
        #
        # if platform_form.is_valid():
            platform_name = platform_form.cleaned_data['platform_name']
            if Virtual_platform.objects.filter(platform_name=platform_name):
                emg = u'添加虚拟化平台失败, 此 %s 已存在!' % platform_name
                return my_render('jasset/virtual_platform_add.html', locals(), request)
            else:
                platform_model =platform_form.save(commit=False)
                host_model = host_form.save(commit=False)
                if idrac_form.is_valid():
                    host_model.remote_ip = idrac_form.save()
                host_model.env = 4
                host_model.save()
                platform_model.host_info = host_model
                platform_model.save()
                smg = u'虚拟化平台: %s添加成功' % platform_name
            return HttpResponseRedirect(reverse('virtual_platform_list'))
    else:
        platform_form = VirtualForm()
        host_form = AssetForm()
        idrac_form = IdracForm()
    return my_render('jasset/virtual_platform_add.html', locals(), request)
Ejemplo n.º 3
0
def asset_edit(request):
    """
    edit a asset
    修改主机
    """
    header_title, path1, path2 = u'修改资产', u'资产管理', u'修改资产'

    asset_id = request.GET.get('id', '')
    username = request.user.username
    asset = get_object(Asset, id=asset_id)
    if asset:
        password_old = asset.password
    # asset_old = copy_model_instance(asset)
    af = AssetForm(instance=asset)
    if asset.remote_ip:
        remote_instance = get_object(Idrac,Idrac_ip=asset.remote_ip.Idrac_ip)
    else:
        remote_instance = None
    Idrac_form = IdracForm(instance=remote_instance)
    if request.method == 'POST':
        af_post = AssetForm(request.POST, instance=asset)
        remote_ip_post = IdracForm(request.POST, instance=remote_instance)
        ip = request.POST.get('ip', '')
        hostname = request.POST.get('hostname', '')
        password = request.POST.get('password', '')
        is_active = True if request.POST.get('is_active') == '1' else False
        use_default_auth = request.POST.get('use_default_auth', '')
        try:
            asset_test = get_object(Asset, hostname=hostname)
            if asset_test and asset_id != unicode(asset_test.id):
                emg = u'该主机名 %s 已存在!' % hostname
                raise ServerError(emg)
            if len(hostname) > 54:
                emg = u'主机名长度不能超过54位!'
                raise ServerError(emg)
            else:
                if af_post.is_valid():
                    af_save = af_post.save(commit=False)
                    if use_default_auth:
                        af_save.username = ''
                        af_save.password = ''
                        # af_save.port = None
                    else:
                        if password:
                            password_encode = CRYPTOR.encrypt(password)
                            af_save.password = password_encode
                        else:
                            af_save.password = password_old
                    af_save.is_active = True if is_active else False
                    # print remote_ip_post
                    if remote_ip_post.is_valid():
                        rf_save = remote_ip_post.save()
                        af_save.remote_ip=rf_save
                    af_save.save()
                    af_post.save_m2m()
                    # asset_new = get_object(Asset, id=asset_id)
                    # asset_diff_one(asset_old, asset_new)
                    info = asset_diff(af_post.__dict__.get('initial'), request.POST)
                    # print af_post.__dict__.get('initial') # 获取表单初始的数据
                    # print remote_ip_post.__dict__.get('initial')
                    remote_info = asset_diff(remote_ip_post.__dict__.get('initial'), request.POST)
                    # print remote_info
                    db_asset_alert(asset, username, info)
                    db_asset_alert(asset, username, remote_info)

                    smg = u'主机 %s 修改成功' % ip
                else:
                    emg = u'主机 %s 修改失败' % ip
                    raise ServerError(emg)
        except ServerError as e:
            error = e.message
            return my_render('jasset/asset_edit.html', locals(), request)
        return HttpResponseRedirect(reverse('asset_detail')+'?id=%s' % asset_id)

    return my_render('jasset/asset_edit.html', locals(), request)