def host_add(request): """ 添加主机 """ uf = HostForm() projects = Project.objects.all() services = Service.objects.all() if request.method == 'POST': uf_post = HostForm(request.POST) physics = request.POST.get('physics', '') ip = request.POST.get('eth1', '') if Host.objects.filter(eth1=ip): emg = u'添加失败, 该IP %s 已存在!' % ip return my_render('assets/host_add.html', locals(), request) if uf_post.is_valid(): zw = uf_post.save(commit=False) # zw.mac = str(request.POST.getlist("mac")[0]).replace(':', '-').strip(" ") status = uf_post.cleaned_data['status'] if physics: physics_host = get_object_or_404(Host, eth1=physics) zw.vm = physics_host zw.type = 1 else: zw.type = 0 zw.save() uf_post.save_m2m() host = Host.objects.get(eth1=ip) server_info(host.uuid) smg = u'主机%s添加成功!' % ip return render_to_response('assets/host_add.html', locals(), context_instance=RequestContext(request)) return render_to_response('assets/host_add.html', locals(), context_instance=RequestContext(request))
def host_add(request): """ 添加主机 """ uf = HostForm() projects = Project.objects.all() services = Service.objects.all() if request.method == 'POST': uf_post = HostForm(request.POST) physics = request.POST.get('physics', '') ip = request.POST.get('eth1', '') if Host.objects.filter(eth1=ip): emg = u'添加失败, 该IP %s 已存在!' % ip return my_render('assets/host_add.html', locals(), request) if uf_post.is_valid(): zw = uf_post.save(commit=False) zw.mac = str(request.POST.getlist("mac")[0]).replace(':', '-').strip(" ") status = uf_post.cleaned_data['status'] if physics: physics_host = get_object_or_404(Host, eth1=physics) zw.vm = physics_host zw.type = 1 else: zw.type = 0 zw.save() uf_post.save_m2m() if zabbix_on and status == 1: zabbix_host_add(request) smg = u'主机%s添加成功!' % ip return render_to_response('assets/host_add.html', locals(), context_instance=RequestContext(request)) return render_to_response('assets/host_add.html', locals(), context_instance=RequestContext(request))
def host_edit(request): """ 修改主机 """ uuid = request.GET.get('uuid') host = get_object_or_404(Host, uuid=uuid) uf = HostForm(instance=host) project_all = Project.objects.all() project_host = host.business.all() projects = [p for p in project_all if p not in project_host] service_all = Service.objects.all() service_host = host.service.all() services = [s for s in service_all if s not in service_host] username = request.user.username if request.method == 'POST': physics = request.POST.get('physics', '') uf_post = HostForm(request.POST, instance=host) if uf_post.is_valid(): zw = uf_post.save(commit=False) zw.mac = str(request.POST.getlist("mac")[0]).replace( ':', '-').strip(" ") request.POST = request.POST.copy() if physics: physics_host = get_object_or_404(Host, eth1=physics) request.POST['vm'] = physics_host.uuid if host.vm: if str(host.vm.eth1) != str(physics): zw.vm = physics_host else: zw.vm = physics_host zw.type = 1 else: request.POST['vm'] = '' zw.type = 0 zw.save() uf_post.save_m2m() new_host = get_object_or_404(Host, uuid=uuid) info = get_diff(uf_post.__dict__.get('initial'), request.POST) db_to_record(username, host, info) return HttpResponseRedirect('/assets/host_detail/?uuid=%s' % uuid) return render_to_response('assets/host_edit.html', locals(), context_instance=RequestContext(request))
def host_edit(request): """ 修改主机 """ uuid = request.GET.get('uuid') host = get_object_or_404(Host, uuid=uuid) uf = HostForm(instance=host) project_all = Project.objects.all() project_host = host.business.all() projects = [p for p in project_all if p not in project_host] service_all = Service.objects.all() service_host = host.service.all() services = [s for s in service_all if s not in service_host] username = request.user.username if request.method == 'POST': physics = request.POST.get('physics', '') uf_post = HostForm(request.POST, instance=host) if uf_post.is_valid(): zw = uf_post.save(commit=False) zw.mac = str(request.POST.getlist("mac")[0]).replace(':', '-').strip(" ") request.POST = request.POST.copy() if physics: physics_host = get_object_or_404(Host, eth1=physics) request.POST['vm'] = physics_host.uuid if host.vm: if str(host.vm.eth1) != str(physics): zw.vm = physics_host else: zw.vm = physics_host zw.type = 1 else: request.POST['vm'] = '' zw.type = 0 zw.save() uf_post.save_m2m() new_host = get_object_or_404(Host, uuid=uuid) info = get_diff(uf_post.__dict__.get('initial'), request.POST) db_to_record(username, host, info) return HttpResponseRedirect('/assets/host_detail/?uuid=%s' % uuid) return render_to_response('assets/host_edit.html', locals(), context_instance=RequestContext(request))