[项目] server.views.py(LOAMAW v1.0)

from time import sleep
from django.shortcuts import render
from .models import *
from django.http import HttpResponse

# Create your views here.


def vserver_register(request):
    if request.method == 'GET':
        return render(request,'server_register.html')
    else:
        vserver_name = request.POST.get('tserver_name','')
        vserver_main_ip = request.POST.get('tserver_main_ip','')

    vnewserver = server_register(vserver_name,vserver_main_ip)
    if vnewserver:
        return render(request,'success.html')
    return render(request,'fail.html')


def vadmin_register(request):
    if request.method == 'GET':
        return render(request,'admin_register.html')
    else:
        vadmin_name = request.POST.get('tadmin_name','')
        vadmin_gid = request.POST.get('tadmin_gid','')
        vadmin_email = request.POST.get('tadmin_email','')
        vadmin_phone = request.POST.get('tadmin_phone','')
        vadmin_level = request.POST.get('tadmin_level','')

    vnewadmin = admin_register(vadmin_name,vadmin_gid,vadmin_email,vadmin_phone,vadmin_level)
    if vnewadmin:
        return render(request,'success.html')
    return render(request,'fail.html')


def vserver_bind_admin(request):
    if request.method == 'GET':
        return render(request,'server_bind_admin.html')
    else:
        vserver_bind = request.POST.get('server_bind_name','')
        vadmin_bind = request.POST.get('admin_bind_name','')
        dbind(vserver_bind,vadmin_bind)
        vnewbind = server.objects.filter(server_name = vserver_bind).filter(admin__admin_name = vadmin_bind)
        if vnewbind:
            return render(request,'success.html')
        return render(request,'fail.html')


def vserver_list(request):

    vserver_name_choice = request.POST.getlist('tserver_name_choice','')

    if vserver_name_choice != '':

        for vserver_name_choices in vserver_name_choice:
            os_check_thread = threading.Thread(target=os_check_all, args=[vserver_name_choices])
            cpu_check_thread = threading.Thread(target=cpu_check_all, args=[vserver_name_choices])
            mem_check_thread = threading.Thread(target=mem_check_all, args=[vserver_name_choices])
            space_check_thread = threading.Thread(target=space_check_all, args=[vserver_name_choices])
            nc_check_thread = threading.Thread(target=nc_check_all, args=[vserver_name_choices])

            os_check_thread.start()
            cpu_check_thread.start()
            mem_check_thread.start()
            space_check_thread.start()
            nc_check_thread.start()

            os_check_thread.join()
            cpu_check_thread.join()
            mem_check_thread.join()
            space_check_thread.join()
            nc_check_thread.join()

        for vserver_name_choices in vserver_name_choice:
            server_change_status(vserver_name_choices)


    vserver_all = server.objects.all()
    return render(request,'server_list.html',{'tserver_all':vserver_all})


import threading
def vserver_detail(request):
    vsdmserver = request.GET.get('server_name','')
    vrefresh = request.POST.get('trefresh','')


    if vrefresh == 'single thread':
        os_check_all(vsdmserver)
        cpu_check_all(vsdmserver)
        mem_check_all(vsdmserver)
        space_check_all(vsdmserver)
        nc_check_all(vsdmserver)
        server_change_status(vsdmserver)


    if vrefresh == 'multi thread':
        os_check_thread = threading.Thread(target=os_check_all, args=[vsdmserver])
        cpu_check_thread = threading.Thread(target=cpu_check_all, args=[vsdmserver])
        mem_check_thread = threading.Thread(target=mem_check_all, args=[vsdmserver])
        space_check_thread = threading.Thread(target=space_check_all, args=[vsdmserver])
        nc_check_thread = threading.Thread(target=nc_check_all, args=[vsdmserver])

        os_check_thread.start()
        cpu_check_thread.start()
        mem_check_thread.start()
        space_check_thread.start()
        nc_check_thread.start()

        os_check_thread.join()
        cpu_check_thread.join()
        mem_check_thread.join()
        space_check_thread.join()
        nc_check_thread.join()

        server_change_status(vsdmserver)

    vsdserver = server.objects.filter(server_name = vsdmserver)
    vsdos = server.objects.get(server_name = vsdmserver).os_set.all()
    vsdcpu = server.objects.get(server_name = vsdmserver).cpu_set.all()
    vsdmem = server.objects.get(server_name = vsdmserver).mem_set.all()
    vsdspace = server.objects.get(server_name = vsdmserver).space_set.all()
    vsdnc = server.objects.get(server_name = vsdmserver).nc_set.all()
    vsdadmin = server.objects.get(server_name = vsdmserver).admin_set.all()

    return render(request,'server_detail.html',{'tserver':vsdserver,'tos':vsdos,'tcpu':vsdcpu,'tmem':vsdmem,'tspace':vsdspace,'tnc':vsdnc,'tsdadmin':vsdadmin})