def GET(self , template_name):
        if self.templates.count(template_name) <= 0 :
            return "404"
        render_template = 'render._' + str(template_name)
        foo = eval(render_template)
        if template_name == 'manage_script' :
            i = web.input(group_id='0', res='None')
            current_group = None
            if i['group_id'] == None or i['group_id'] == '0' :
                script_list = helper.get_all_scripts()
            else :
                script_list = helper.get_scripts_by_group_id(i['group_id'])
                current_group = helper.get_script_group_by_id(i['group_id'])
            group_list = helper.get_script_group_list()
            if group_list['val'] == None :
                group_list['val'] = []
            if script_list['val'] == None :
                script_list['val'] = []
            return foo({"scripts_list" : script_list['val'] , "group_list" : group_list['val'] , "group" : current_group , "result" : get_result_alert(i['res'])})
        elif template_name == 'manage_server' :
            i = web.input(group_id='0' , res='None')
            current_group = None
            if i['group_id'] == None or i['group_id'] == '0' :
                server_list = helper.get_all_servers()
            else :
                server_list = helper.get_servers_by_group_id(i['group_id'])
                current_group = helper.get_server_group_by_id(i['group_id'])
            group_list = helper.get_server_group_list()
            if group_list['val'] == None :
                group_list['val'] = []
            if server_list['val'] == None :
                server_list['val'] = []
            return foo({"servers_list" : server_list['val'] , "group_list" : group_list['val'] , "group" : current_group , "result" : get_result_alert(i['res'])})
        elif template_name == 'add_script':
            i = web.input(res='None')
            if i['res']== 'success' :
                return foo({"result":get_result_alert2('success','Success','Add a new script(group)')})
            elif i['res'] == 'error' :
                return foo({"result":get_result_alert2('error','Error','Fail to add script(group)')})
            else :
                return foo({"result":""})
        elif template_name == 'add_server':
            i = web.input(res='None')
            if i['res']== 'success' :
                return foo({"result":get_result_alert2('success','Success','Add a new server(group)')})
            elif i['res'] == 'error' :
                return foo({"result":get_result_alert2('error','Error','Fail to add server(group)')})
            else :
                return foo({"result":""})
        elif template_name == 'edit_script' :
            i = web.input(res=None)
            script_id = i['script_id']
            script = helper.get_script_by_id(script_id)["val"]
            script_content = helper.get_script_content_by_id(script_id)["val"]
            args = {}
            args["script_id"] = script_id
            args["script"] = script
            args["content"] = script_content
            if i['res']== 'success' :
                args["result"] = get_result_alert2('success','Success','Update script(group) successfully!')
            elif i['res'] == 'error' :
                args["result"] = get_result_alert2('error','Error','Fail to update script(group)')
            else :
                args["result"] = ''
            return foo(args)
        elif template_name == 'edit_server' :
            i = web.input(res=None)
            server_id = i["server_id"]
            server = helper.get_server_by_id(server_id)["val"]
            args = {}
            args["server"] = server
            if i['res']== 'success' :
                args["result"] = get_result_alert2('success','Success','Update server(group) successfully!')
            elif i['res'] == 'error' :
                args["result"] = get_result_alert2('error','Error','Fail to update server(group)')
            else :
                args["result"] ='' 
            return foo(args)

        elif template_name == 'manage_task' :
            args = {}
            server_group_list = helper.get_server_group_list()
            script_group_list = helper.get_script_group_list()
            if server_group_list["status"] == -1 or server_group_list["val"] == None :
                server_group_list = []
            else:
                server_group_list = server_group_list["val"]
            if script_group_list["status"] == -1 or script_group_list["val"] == None :
                script_group_list = []
            else:
                script_group_list = script_group_list["val"]
            
            task_list_res = helper.get_all_tasks()
            if task_list_res["status"] == -1 or task_list_res["val"] == None :
                task_list = []
            else :
                task_list = task_list_res["val"]
            args["server_group_list"] = server_group_list
            args["script_group_list"] = script_group_list
            args["task_list"] = task_list
            args["result"] = ""
            i =web.input(res='None')
            if i['res'] != None :
                if i['res'] == 'notstart' :
                    args["result"] = get_result_alert2('error' , 'Waring' , 'Task has not started!')
                elif i['res'] == 'runerror' :
                    args["result"] = get_result_alert2('error' , 'Waring' , 'Exception occurs when run task , server group and script group can not be empty , check!')
                elif i['res'] == 'runsuccess' :
                    args["result"] = get_result_alert2('success' , 'Success' , 'Task start ! Please click ViewStatus button to view scripts status')
                elif i['res'] == 'addtasksuccess' :
                    args["result"] = get_result_alert2('success' , 'Success' , 'A new Task created! ')
                elif i['res'] == 'addtaskerror' :
                    args["result"] = get_result_alert2('error' , 'Error' , 'Fail to Create task , please check!')
                elif i['res'] == 'deletetasksuccess' :
                    args["result"] = get_result_alert2('success','Success' , 'The task has been deleted successfullly')
                elif i['res'] == 'deletetaskerror' :
                    args["result"] = get_result_alert2('error' , 'Error' , 'Fail to delete task , please check!')
            return foo(args)
        else :
            return "404"
    def GET(self, template_name):
        if self.templates.count(template_name) <= 0:
            return "404"
        render_template = 'render._' + str(template_name)
        foo = eval(render_template)
        if template_name == 'manage_script':
            i = web.input(group_id='0', res='None')
            current_group = None
            if i['group_id'] == None or i['group_id'] == '0':
                script_list = helper.get_all_scripts()
            else:
                script_list = helper.get_scripts_by_group_id(i['group_id'])
                current_group = helper.get_script_group_by_id(i['group_id'])
            group_list = helper.get_script_group_list()
            if group_list['val'] == None:
                group_list['val'] = []
            if script_list['val'] == None:
                script_list['val'] = []
            return foo({
                "scripts_list": script_list['val'],
                "group_list": group_list['val'],
                "group": current_group,
                "result": get_result_alert(i['res'])
            })
        elif template_name == 'manage_server':
            i = web.input(group_id='0', res='None')
            current_group = None
            if i['group_id'] == None or i['group_id'] == '0':
                server_list = helper.get_all_servers()
            else:
                server_list = helper.get_servers_by_group_id(i['group_id'])
                current_group = helper.get_server_group_by_id(i['group_id'])
            group_list = helper.get_server_group_list()
            if group_list['val'] == None:
                group_list['val'] = []
            if server_list['val'] == None:
                server_list['val'] = []
            return foo({
                "servers_list": server_list['val'],
                "group_list": group_list['val'],
                "group": current_group,
                "result": get_result_alert(i['res'])
            })
        elif template_name == 'add_script':
            i = web.input(res='None')
            if i['res'] == 'success':
                return foo({
                    "result":
                    get_result_alert2('success', 'Success',
                                      'Add a new script(group)')
                })
            elif i['res'] == 'error':
                return foo({
                    "result":
                    get_result_alert2('error', 'Error',
                                      'Fail to add script(group)')
                })
            else:
                return foo({"result": ""})
        elif template_name == 'add_server':
            i = web.input(res='None')
            if i['res'] == 'success':
                return foo({
                    "result":
                    get_result_alert2('success', 'Success',
                                      'Add a new server(group)')
                })
            elif i['res'] == 'error':
                return foo({
                    "result":
                    get_result_alert2('error', 'Error',
                                      'Fail to add server(group)')
                })
            else:
                return foo({"result": ""})
        elif template_name == 'edit_script':
            i = web.input(res=None)
            script_id = i['script_id']
            script = helper.get_script_by_id(script_id)["val"]
            script_content = helper.get_script_content_by_id(script_id)["val"]
            args = {}
            args["script_id"] = script_id
            args["script"] = script
            args["content"] = script_content
            if i['res'] == 'success':
                args["result"] = get_result_alert2(
                    'success', 'Success', 'Update script(group) successfully!')
            elif i['res'] == 'error':
                args["result"] = get_result_alert2(
                    'error', 'Error', 'Fail to update script(group)')
            else:
                args["result"] = ''
            return foo(args)
        elif template_name == 'edit_server':
            i = web.input(res=None)
            server_id = i["server_id"]
            server = helper.get_server_by_id(server_id)["val"]
            args = {}
            args["server"] = server
            if i['res'] == 'success':
                args["result"] = get_result_alert2(
                    'success', 'Success', 'Update server(group) successfully!')
            elif i['res'] == 'error':
                args["result"] = get_result_alert2(
                    'error', 'Error', 'Fail to update server(group)')
            else:
                args["result"] = ''
            return foo(args)

        elif template_name == 'manage_task':
            args = {}
            server_group_list = helper.get_server_group_list()
            script_group_list = helper.get_script_group_list()
            if server_group_list["status"] == -1 or server_group_list[
                    "val"] == None:
                server_group_list = []
            else:
                server_group_list = server_group_list["val"]
            if script_group_list["status"] == -1 or script_group_list[
                    "val"] == None:
                script_group_list = []
            else:
                script_group_list = script_group_list["val"]

            task_list_res = helper.get_all_tasks()
            if task_list_res["status"] == -1 or task_list_res["val"] == None:
                task_list = []
            else:
                task_list = task_list_res["val"]
            args["server_group_list"] = server_group_list
            args["script_group_list"] = script_group_list
            args["task_list"] = task_list
            args["result"] = ""
            i = web.input(res='None')
            if i['res'] != None:
                if i['res'] == 'notstart':
                    args["result"] = get_result_alert2(
                        'error', 'Waring', 'Task has not started!')
                elif i['res'] == 'runerror':
                    args["result"] = get_result_alert2(
                        'error', 'Waring',
                        'Exception occurs when run task , server group and script group can not be empty , check!'
                    )
                elif i['res'] == 'runsuccess':
                    args["result"] = get_result_alert2(
                        'success', 'Success',
                        'Task start ! Please click ViewStatus button to view scripts status'
                    )
                elif i['res'] == 'addtasksuccess':
                    args["result"] = get_result_alert2('success', 'Success',
                                                       'A new Task created! ')
                elif i['res'] == 'addtaskerror':
                    args["result"] = get_result_alert2(
                        'error', 'Error',
                        'Fail to Create task , please check!')
                elif i['res'] == 'deletetasksuccess':
                    args["result"] = get_result_alert2(
                        'success', 'Success',
                        'The task has been deleted successfullly')
                elif i['res'] == 'deletetaskerror':
                    args["result"] = get_result_alert2(
                        'error', 'Error',
                        'Fail to delete task , please check!')
            return foo(args)
        else:
            return "404"
    def do_with_view_status (self, task_id , status_res) :
        args = {}
        server_group_list = helper.get_server_group_list()
        script_group_list = helper.get_script_group_list()
        if server_group_list["status"] == -1 or server_group_list["val"] == None :
            server_group_list = []
        else:
            server_group_list = server_group_list["val"]
        if script_group_list["status"] == -1 or script_group_list["val"] == None :
            script_group_list = []
        else:
            script_group_list = script_group_list["val"]
        
        task_list_res = helper.get_all_tasks()
        if task_list_res["status"] == -1 or task_list_res["val"] == None :
            task_list = []
        else :
            task_list = task_list_res["val"]
        
        current_task = helper.get_task_by_id(task_id)["val"]
        status_list = []
        scripts_length = len(current_task["script_group"].scripts)
        for server in current_task["server_group"].servers :
            status = {}
            status["username"] = server.username
            status["host_address"] = server.host_address
            if status_res.has_key(server.id) == False :
                status["console_output"] = ""
                status["final_status"] = -2
                script_status = []
                for i in range(scripts_length) :
                    script_status.append(-1)
                status["script_status"] = script_status
                status_list.append(status)
                continue

            status["console_output"] = status_res[server.id]["content"]
            status["final_status"] = status_res[server.id]["status"]
            script_status = []
            if status_res[server.id].has_key("executing_id") == True :
                for i in range(scripts_length) :
                    if i == status_res[server.id]["executing_id"] :
                        if status_res[server.id]["status"] >=0:
                            script_status.append(1)
                        else :
                            script_status.append(-1)
                    elif i > status_res[server.id]["executing_id"] :
                        script_status.append(2)
                    else :
                        script_status.append(0)
            else :
                for i in range(scripts_length) :
                    script_status.append(-1)
            if status_res[server.id]["status"] == 0 :
                script_status = []
                for i in range(scripts_length) :
                    script_status.append(0)
            status["script_status"] = script_status
            status_list.append(status)
        current_scripts = helper.get_scripts_by_group_id(current_task["script_group"].id)["val"]
        args["server_group_list"] = server_group_list
        args["script_group_list"] = script_group_list
        args["task_list"] = task_list
        args["result"] = ""
        args["current_scripts"] = current_scripts
        args["status_list"] = status_list
        args["current_task_id"] = task_id
        return render._view_status(args)
    def do_with_view_status(self, task_id, status_res):
        args = {}
        server_group_list = helper.get_server_group_list()
        script_group_list = helper.get_script_group_list()
        if server_group_list["status"] == -1 or server_group_list[
                "val"] == None:
            server_group_list = []
        else:
            server_group_list = server_group_list["val"]
        if script_group_list["status"] == -1 or script_group_list[
                "val"] == None:
            script_group_list = []
        else:
            script_group_list = script_group_list["val"]

        task_list_res = helper.get_all_tasks()
        if task_list_res["status"] == -1 or task_list_res["val"] == None:
            task_list = []
        else:
            task_list = task_list_res["val"]

        current_task = helper.get_task_by_id(task_id)["val"]
        status_list = []
        scripts_length = len(current_task["script_group"].scripts)
        for server in current_task["server_group"].servers:
            status = {}
            status["username"] = server.username
            status["host_address"] = server.host_address
            if status_res.has_key(server.id) == False:
                status["console_output"] = ""
                status["final_status"] = -2
                script_status = []
                for i in range(scripts_length):
                    script_status.append(-1)
                status["script_status"] = script_status
                status_list.append(status)
                continue

            status["console_output"] = status_res[server.id]["content"]
            status["final_status"] = status_res[server.id]["status"]
            script_status = []
            if status_res[server.id].has_key("executing_id") == True:
                for i in range(scripts_length):
                    if i == status_res[server.id]["executing_id"]:
                        if status_res[server.id]["status"] >= 0:
                            script_status.append(1)
                        else:
                            script_status.append(-1)
                    elif i > status_res[server.id]["executing_id"]:
                        script_status.append(2)
                    else:
                        script_status.append(0)
            else:
                for i in range(scripts_length):
                    script_status.append(-1)
            if status_res[server.id]["status"] == 0:
                script_status = []
                for i in range(scripts_length):
                    script_status.append(0)
            status["script_status"] = script_status
            status_list.append(status)
        current_scripts = helper.get_scripts_by_group_id(
            current_task["script_group"].id)["val"]
        args["server_group_list"] = server_group_list
        args["script_group_list"] = script_group_list
        args["task_list"] = task_list
        args["result"] = ""
        args["current_scripts"] = current_scripts
        args["status_list"] = status_list
        args["current_task_id"] = task_id
        return render._view_status(args)