Esempio n. 1
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 request.method == 'POST':
        af_post = AssetForm(request.POST, instance=asset)
        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
                    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)
                    db_asset_alert(asset, username, info)

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

    return render_to_response('asset/asset_edit.html', locals(), RequestContext(request))
Esempio n. 2
0
 def post(self, request):
     asset_form = AssetForm(request.POST,
                            auto_id="form-asset-%s",
                            label_suffix='')
     asset_sku_form = AssetSkuForm(request.POST,
                                   auto_id="form-asset-sku-%s",
                                   label_suffix='')
     asset_set_form = AssetSetForm(request.POST,
                                   auto_id="form-asset-set-%s",
                                   label_suffix='')
     asset_creation_form = AssetCreationForm(request.POST,
                                             auto_id="form-asset-create-%s",
                                             label_suffix='')
     ret = {
         'asset_creation_form': asset_creation_form,
         'asset_form': asset_form,
         'asset_sku_form': asset_sku_form,
         'asset_set_form': asset_set_form,
     }
     save_ret = save_asset_creation(asset_form, asset_sku_form,
                                    asset_set_form, asset_creation_form)
     asset = save_ret.get('asset')
     if asset is not None:
         ret.update({'msg': '编号【%s】设备的建账申请已提交!' % asset})
     elif save_ret.get('error_msg'):
         ret.update(save_ret['error_msg'])
     return render(request, 'create-asset.html', ret)
Esempio n. 3
0
 def post(self, request, asset_id):
     asset = get_object_or_404(Asset, pk=asset_id)
     asset_form = AssetForm(auto_id="form-asset-%s", label_suffix='')
     asset_creation_form = AssetCreationForm(request.POST,
                                             auto_id="form-create-%s",
                                             label_suffix='')
     asset_sku_form = AssetSkuForm(request.POST,
                                   auto_id="form-asset-sku-%s",
                                   label_suffix='')
     asset_set_form = AssetSetForm(request.POST,
                                   auto_id="form-asset-set-%s",
                                   label_suffix='')
     ret = {
         'asset_creation_form': asset_creation_form,
         'asset_sku_form': asset_sku_form,
         'asset_set_form': asset_set_form,
         'readonly': True,
         'asset': asset,
         'asset_form': asset_form,
     }
     save_ret = save_asset_creation(asset_form,
                                    asset_sku_form,
                                    asset_set_form,
                                    asset_creation_form,
                                    on_asset=True,
                                    asset=asset)
     if save_ret['asset'] is not None:
         ret.update({'msg': '基于编号【{}】设备的建账申请已提交!'.format(asset.aid)})
     elif save_ret.get('error_msg'):
         ret.update(save_ret.get('error_msg'))
     return render(request, 'create-asset.html', ret)
Esempio n. 4
0
def asset_add(request):
    """
    Asset add view
    添加资产
    """
    header_title, path1, path2 = u'添加资产', u'资产管理', u'添加资产'
    asset_group_all = AssetGroup.objects.all()
    af = AssetForm()
    default_setting = None
    default_port = default_setting.field2 if default_setting else ''
    if request.method == 'POST':
        af_post = AssetForm(request.POST)
        ip = request.POST.get('ip', '')
        hostname = request.POST.get('hostname', '')

        is_active = True if request.POST.get('is_active') == '1' else False
        use_default_auth = request.POST.get('use_default_auth', '')
        try:
            if Asset.objects.filter(hostname=unicode(hostname)):
                error = u'该主机名 %s 已存在!' % hostname
                raise ServerError(error)
            if len(hostname) > 54:
                error = u"主机名长度不能超过53位!"
                raise ServerError(error)
        except ServerError:
            pass
        else:
            if af_post.is_valid():
                asset_save = af_post.save(commit=False)
                if not use_default_auth:
                    password = request.POST.get('password', '')
                    password_encode = CRYPTOR.encrypt(password)
                    asset_save.password = password_encode
                if not ip:
                    asset_save.ip = hostname
                asset_save.is_active = True if is_active else False
                asset_save.save()
                af_post.save_m2m()

                msg = u'主机 %s 添加成功' % hostname
            else:
                esg = u'主机 %s 添加失败' % hostname

    return render_to_response('asset/asset_add.html', locals(), RequestContext(request))
Esempio n. 5
0
def asset_add(request):
    """
    Asset add view
    添加资产
    """
    header_title, path1, path2 = u'添加资产', u'资产管理', u'添加资产'
    asset_group_all = AssetGroup.objects.all()
    af = AssetForm()
    default_setting = None
    default_port = default_setting.field2 if default_setting else ''
    if request.method == 'POST':
        af_post = AssetForm(request.POST)
        ip = request.POST.get('ip', '')
        hostname = request.POST.get('hostname', '')

        is_active = True if request.POST.get('is_active') == '1' else False
        use_default_auth = request.POST.get('use_default_auth', '')
        try:
            if Asset.objects.filter(hostname=unicode(hostname)):
                error = u'该主机名 %s 已存在!' % hostname
                raise ServerError(error)
            if len(hostname) > 54:
                error = u"主机名长度不能超过53位!"
                raise ServerError(error)
        except ServerError:
            pass
        else:
            if af_post.is_valid():
                asset_save = af_post.save(commit=False)
                if not use_default_auth:
                    password = request.POST.get('password', '')
                    password_encode = CRYPTOR.encrypt(password)
                    asset_save.password = password_encode
                if not ip:
                    asset_save.ip = hostname
                asset_save.is_active = True if is_active else False
                asset_save.save()
                af_post.save_m2m()

                msg = u'主机 %s 添加成功' % hostname
            else:
                esg = u'主机 %s 添加失败' % hostname

    return render_to_response('asset/asset_add.html', locals(),
                              RequestContext(request))
Esempio n. 6
0
 def get(self, request):
     if request.is_ajax():
         assets = AssetSerializer(instance=Asset.objects.all(), many=True)
         return HttpResponse(json.dumps(assets.data),
                             content_type='application/json')
     else:
         asset_creation_form = AssetCreationForm(
             auto_id="form-asset-create-%s", label_suffix='')
         asset_form = AssetForm(auto_id="form-asset-%s", label_suffix='')
         asset_sku_form = AssetSkuForm(auto_id="form-asset-sku-%s",
                                       label_suffix='')
         asset_set_form = AssetSetForm(auto_id="form-asset-set-%s",
                                       label_suffix='')
         ret = {
             'asset_creation_form': asset_creation_form,
             'asset_form': asset_form,
             'asset_sku_form': asset_sku_form,
             'asset_set_form': asset_set_form,
         }
         return render(request, 'create-asset.html', ret)
Esempio n. 7
0
 def get(self, request, asset_id):
     asset = get_object_or_404(Asset, pk=asset_id)
     asset_creation_form = AssetCreationForm(auto_id="form-asset-create-%s",
                                             label_suffix='')
     asset_form = AssetForm(instance=asset,
                            auto_id="form-asset-%s",
                            label_suffix='')
     asset_sku_form = AssetSkuForm(auto_id="form-asset-sku-%s",
                                   label_suffix='')
     asset_set_form = AssetSetForm(auto_id="form-asset-set-%s",
                                   label_suffix='')
     ret = {
         'asset_creation_form': asset_creation_form,
         'asset_form': asset_form,
         'asset_sku_form': asset_sku_form,
         'asset_set_form': asset_set_form,
         'asset': asset,
         'readonly': True,
     }
     return render(request, 'create-asset.html', ret)
Esempio n. 8
0
def asset_edit_batch(request):
    af = AssetForm()
    name = request.user.username
    asset_group_all = AssetGroup.objects.all()

    if request.method == 'POST':
        env = request.POST.get('env', '')
        idc_id = request.POST.get('idc', '')
        port = request.POST.get('port', '')
        use_default_auth = request.POST.get('use_default_auth', '')
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        group = request.POST.getlist('group', [])
        cabinet = request.POST.get('cabinet', '')
        comment = request.POST.get('comment', '')
        asset_id_all = unicode(request.GET.get('asset_id_all', ''))
        asset_id_all = asset_id_all.split(',')
        for asset_id in asset_id_all:
            alert_list = []
            asset = get_object(Asset, id=asset_id)
            if asset:
                if env:
                    if asset.env != env:
                        asset.env = env
                        alert_list.append([u'运行环境', asset.env, env])
                if idc_id:
                    idc = get_object(IDC, id=idc_id)
                    name_old = asset.idc.name if asset.idc else u''
                    if idc and idc.name != name_old:
                        asset.idc = idc
                        alert_list.append([u'机房', name_old, idc.name])
                if port:
                    if unicode(asset.port) != port:
                        asset.port = port
                        alert_list.append([u'端口号', asset.port, port])

                if use_default_auth:
                    if use_default_auth == 'default':
                        asset.use_default_auth = 1
                        asset.username = ''
                        asset.password = ''
                        alert_list.append(
                            [u'使用默认管理账号', asset.use_default_auth, u'默认'])
                    elif use_default_auth == 'user_passwd':
                        asset.use_default_auth = 0
                        asset.username = username
                        password_encode = CRYPTOR.encrypt(password)
                        asset.password = password_encode
                        alert_list.append(
                            [u'使用默认管理账号', asset.use_default_auth, username])
                if group:
                    group_new, group_old, group_new_name, group_old_name = [], asset.group.all(
                    ), [], []
                    for group_id in group:
                        g = get_object(AssetGroup, id=group_id)
                        if g:
                            group_new.append(g)
                    if not set(group_new) < set(group_old):
                        group_instance = list(set(group_new) | set(group_old))
                        for g in group_instance:
                            group_new_name.append(g.name)
                        for g in group_old:
                            group_old_name.append(g.name)
                        asset.group = group_instance
                        alert_list.append([
                            u'主机组', ','.join(group_old_name),
                            ','.join(group_new_name)
                        ])
                if cabinet:
                    if asset.cabinet != cabinet:
                        asset.cabinet = cabinet
                        alert_list.append([u'机柜号', asset.cabinet, cabinet])
                if comment:
                    if asset.comment != comment:
                        asset.comment = comment
                        alert_list.append([u'备注', asset.comment, comment])
                asset.save()

            if alert_list:
                recode_name = unicode(name) + ' - ' + u'批量'
                AssetRecord.objects.create(asset=asset,
                                           username=recode_name,
                                           content=alert_list)
        return render_to_response('asset/asset_update_status.html', locals(),
                                  RequestContext(request))

    return render_to_response('asset/asset_edit_batch.html', locals(),
                              RequestContext(request))
Esempio n. 9
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 request.method == 'POST':
        af_post = AssetForm(request.POST, instance=asset)
        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
                    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)
                    db_asset_alert(asset, username, info)

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

    return render_to_response('asset/asset_edit.html', locals(),
                              RequestContext(request))