import libvirt
import socket
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponse, HttpResponseRedirect
from virtmgr.model.models import *

def index(request):
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/user/login')

	def get_hosts_status():
		kvm_host = Host.objects.filter(user=request.user.id)
		name_ipddr = {}
		for host in kvm_host:
			try:
				s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
				s.settimeout(1)
				s.connect((host.ipaddr, 16509))
				s.close()
				status = 1
			except:
				status = 2
			name_ipddr[host.hostname] = (host.id, host.ipaddr, host.login, host.passwd, status)
	   	return name_ipddr
	
	def del_host(host):
		hosts = Host.objects.get(user=request.user.id, hostname=host)
		hosts.delete()

	def add_host(host, ip, usr, passw):
		hosts = Host(user_id=request.user.id, hostname=host, ipaddr=ip, login=usr, passwd=passw)
		hosts.save()

	def get_host_status(hosts):
		for host, info in hosts.items():
			print host, info

	host_info = get_hosts_status()

	if request.method == 'POST':
		action = request.POST.get('action','')
		if action == 'delete':
			host = request.POST.get('host','')
			del_host(host)
			return HttpResponseRedirect('/dashboard/')
		if action == 'add':
			field = form.cleaned_data
			name = field['host']
			ipaddr = field['ipaddr']
			login = field['sshusr']
			passw = field['passw']

			hostname = Host.objects.filter(user=request.user, hostname=name)
			ipaddr = Host.objects.filter(user=request.user, ipaddr=ipaddr)

			if not name or not ipaddr or not login or not passw:
				error = 'Inser all fields'
			elif ipaddr or hostname:
				error = 'Host alredy exist'
			else:
				add_host(name, ipaddr, login, passw)

			return HttpResponseRedirect('/dashboard/')

	return render_to_response('dashboard.html', locals())

@@ -1284,57 +1284,42 @@
 %0A%09%09%09
-field = form.cleaned_data%0A%09%09%09name = field%5B
+name = request.POST.get(
 'host'
-%5D
+,'')
 %0A%09%09%09
@@ -1331,22 +1331,33 @@
 r = 
-field%5B
+request.POST.get(
 'ipaddr'
 %5D%0A%09%09
@@ -1352,17 +1352,20 @@
 'ipaddr'
-%5D
+,'')
 %0A%09%09%09logi
@@ -1372,22 +1372,33 @@
 n = 
-field%5B
+request.POST.get(
 'sshusr'
 %5D%0A%09%09
@@ -1393,17 +1393,20 @@
 'sshusr'
-%5D
+,'')
 %0A%09%09%09pass
@@ -1413,22 +1413,36 @@
 w = 
-field%5B
+request.POST.get(
 'passw'
-%5D
+,'')
 %0A%0A%09%09

Add dashboard form fix