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 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)
def asset_add(request): """ Asset add view 添加资产 """ header_title, path1, path2 = u'添加资产', u'资产管理', u'添加资产' asset_group_all = AssetGroup.objects.all() af = AssetForm() default_setting = get_object(Setting, name='default') 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 username = request.POST.get('username', '') private_key = request.POST.get('key', '') if private_key: private_key_dir = os.path.join(KEY_DIR, 'admin_key') private_key_path = os.path.join( private_key_dir, '%s-%s.pem' % (username, hostname)) mkdir(private_key_dir) with open(private_key_path, 'w') as f: f.write(private_key) os.chmod(private_key_path, 0600) asset_save.private_key_path = private_key_path 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 my_render('jasset/asset_add.html', locals(), request)
def asset_add(request): """ Asset add view 添加资产 """ header_title, path1, path2 = u'添加资产', u'资产管理', u'添加资产' #asset_group_all = AssetGroup.objects.all() login_user_id = request.user.id if not login_user_id: return HttpResponseRedirect(reverse('index')) login_user = User.objects.get(id=login_user_id) productlines = ProductLine.objects.all() af = AssetForm() default_setting = get_object(Setting, name='default') 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', '') productline_name = request.POST.get('productline_name', '') productLine = get_object_or_404(ProductLine, name=productline_name) 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), productLine=productLine): 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.productLine = productLine 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 my_render('jasset/asset_add.html', locals(), request)
def asset_add(request): """ Asset add view 添加资产 """ header_title, path1, path2 = u'添加资产', u'资产管理', u'添加资产' asset_group_all = AssetGroup.objects.all() af = AssetForm() default_setting = get_object(Setting, name='default') 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', '') uuid_r = uuid.uuid4().get_hex() try: if Asset.objects.filter(hostname=unicode(hostname)): error = u'该主机名 %s 已存在!' % hostname 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.uuid = uuid_r asset_save.save() af_post.save_m2m() viewer_vnc = os.path.join(KEY_DIR, 'keys', 'viewer.vnc') if viewer_vnc: fwrite = file(viewer_vnc, "a+") context= "%s: %s:5901" % (uuid_r, hostname) fwrite.write(context) fwrite.close() msg = u'主机 %s 添加成功' % hostname else: esg = u'主机 %s 添加失败' % hostname return my_render('jasset/asset_add.html', locals(), request)
def asset_add(request): """ Asset add view 添加资产 """ header_title, path1, path2 = '添加资产', '资产管理', '添加资产' asset_group_all = AssetGroup.objects.all() af = AssetForm() default_setting = get_object(Setting, name='default') 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=str(hostname)): error = '该主机名 %s 已存在!' % hostname raise ServerError(error) if len(hostname) > 54: error = "主机名长度不能超过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 = '主机 %s 添加成功' % hostname else: esg = '主机 %s 添加失败' % hostname return my_render('jasset/asset_add.html', locals(), request)
def asset_add(request): """ Asset add view 添加资产 """ header_title, path1, path2 = u'添加资产', u'资产管理', u'添加资产' asset_group_all = AssetGroup.objects.all() af = AssetForm() default_setting = get_object(Setting, name='default') 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 my_render('jasset/asset_add.html', locals(), request)