def get_account_list(request): """ 获取用户列表 """ user = request.user status = 500 data = {} try: accounts = _account.get_accounts_by_params(is_delete=False, create_user=user) data.update({ 'accounts': [{ "a_id": account.id, "a_name": u'%s 账号:%s 创建人:%s' % (account.name, account.name_abbr, account.create_user) } for account in accounts] }) status = 200 except: data['msg'] = u"账户列表更新失败" error = traceback.format_exc() print error return HttpResponse( json.dumps({ 'status': status, 'result': data }, cls=LazyEncoder))
def get_account_list_v2(request): """ 获取授权用户列表 """ user = request.user status = 500 data = {} try: perms = _account.get_perms_by_params(to_user=user, is_delete=False, ptype=enum_account.PERM_PTYPE_SCRIPT) scripts = _script.Script.objects.filter(Q(create_user=user)|Q(pk__in=[perm.object_id for perm in perms]), is_delete=False,is_once=False) scrip_id_list = [script.id for script in scripts] user_set = set() user_set.add(user) accounts_perm = _account.get_perms_by_params(object_id__in=scrip_id_list, is_delete=False) for account_perm in accounts_perm: user_set.add(account_perm.create_user) user_set.add(account_perm.to_user) accounts = _account.get_accounts_by_params(is_delete=False,create_user__in=list(user_set)) data.update({ 'accounts':[{"a_id":account.id,"a_name":u'%s 账号:%s 创建人:%s'%(account.name,account.name_abbr,account.create_user)} for account in accounts] }) status = 200 except: data['msg'] = u"账户列表更新失败" error = traceback.format_exc() print error return HttpResponse(json.dumps({ 'status':status, 'result':data }, cls=LazyEncoder))
def send_file(request): now = datetime.datetime.now() name = u'分发文件-%s'%datetime.datetime.strftime(now,'%Y%m%d%H%M%S') accounts = _account.get_accounts_by_params(is_delete=False) accountForm = AccountForm() max_upload_size = MAX_UPLOAD_SIZE user = request.user fileserver_url = FILE_SERVER_HOST return locals()
def template_edit(request,template_id): """ show detail of the template after clicked the button. """ user = request.user status = 500 data = {} try: method = request.method template_file = "md_manage/edit_md.html" check_id = datetime.datetime.now().strftime("%Y%m%d%H%M%S") template = _template.get_template_by_params(id=template_id) ajax_url = "/md_manage/template_step/list/{0}/".format(template.pk) accounts = _account.get_accounts_by_params(is_delete=False) accountForm = AccountForm() target_ips = json.loads(template.target) if template.target else [] hide_ip_dict = build_hidden_ip_dict(target_ips) hide_ip_json = json.dumps(hide_ip_dict) if method == 'POST': templateForm = EditTemplateForm(request.POST,instance=template,user=request.user) check_id=request.POST.get("check_id",check_id) work_type=request.POST.get("work_type","") data.update({"template_id":template.id,"check_id":check_id}) if templateForm.is_valid(): user = request.user template = templateForm.save(commit=False) template.update_user = user template.work_type = work_type template.save() status = 200 else: html = render_to_string(template_file, locals()) data.update({"html":html}) data.update({"template_name":template.name}) return HttpResponse(json.dumps({ "status":status, "result":data, }, cls=LazyEncoder)) else: account_id = template.account.id if template.account else 0 template_name = template.name templateForm = EditTemplateForm(instance=template,user=request.user) work_type = template.work_type html = render_to_string(template_file, locals()) status=200 data.update({"html":html,"template_name":template_name,"work_type":work_type}) except: error = traceback.format_exc() logger.error(error) print error data['msg'] = u"服务器错误" return HttpResponse(json.dumps({ "status":status, "result":data }, cls=LazyEncoder))
def get_account_list_v2(request): """ 获取授权用户列表 """ user = request.user status = 500 data = {} try: perms = _account.get_perms_by_params( to_user=user, is_delete=False, ptype=enum_account.PERM_PTYPE_SCRIPT) scripts = _script.Script.objects.filter( Q(create_user=user) | Q(pk__in=[perm.object_id for perm in perms]), is_delete=False, is_once=False) scrip_id_list = [script.id for script in scripts] user_set = set() user_set.add(user) accounts_perm = _account.get_perms_by_params( object_id__in=scrip_id_list, is_delete=False) for account_perm in accounts_perm: user_set.add(account_perm.create_user) user_set.add(account_perm.to_user) accounts = _account.get_accounts_by_params( is_delete=False, create_user__in=list(user_set)) data.update({ 'accounts': [{ "a_id": account.id, "a_name": u'%s 账号:%s 创建人:%s' % (account.name, account.name_abbr, account.create_user) } for account in accounts] }) status = 200 except: data['msg'] = u"账户列表更新失败" error = traceback.format_exc() print error return HttpResponse( json.dumps({ 'status': status, 'result': data }, cls=LazyEncoder))
def script(request): user = request.user sdicts = {} method = request.method now = datetime.datetime.now() name = 'script-%s'%datetime.datetime.strftime(now,'%Y%m%d%H%M%S') accounts = _account.get_accounts_by_params(is_delete=False,create_user=user) accountForm = AccountForm() template_file = "script/contentDiv.html" html = render_to_string(template_file, locals()) sdicts['html'] = html if method == 'POST': pass return HttpResponse(json.dumps(sdicts, ensure_ascii=False))
def script(request): user = request.user sdicts = {} method = request.method now = datetime.datetime.now() name = 'script-%s' % datetime.datetime.strftime(now, '%Y%m%d%H%M%S') accounts = _account.get_accounts_by_params(is_delete=False, create_user=user) accountForm = AccountForm() template_file = "script/contentDiv.html" html = render_to_string(template_file, locals()) sdicts['html'] = html if method == 'POST': pass return HttpResponse(json.dumps(sdicts, ensure_ascii=False))
def get_account_list(request): """ 获取用户列表 """ user = request.user status = 500 data = {} try: accounts = _account.get_accounts_by_params(is_delete=False,create_user=user) data.update({ 'accounts':[{"a_id":account.id,"a_name":u'%s 账号:%s 创建人:%s'%(account.name,account.name_abbr,account.create_user)} for account in accounts] }) status = 200 except: data['msg'] = u"账户列表更新失败" error = traceback.format_exc() print error return HttpResponse(json.dumps({ 'status':status, 'result':data }, cls=LazyEncoder))
history_step_script = _history.get_historyStepScript_by_params( step=history_step) version = history_step_script.version script = version.script file_path = os.path.join(MEDIA_ROOT, 'scripts', '%s' % version.sfile) file_content = '' f = open(file_path) try: file_content = f.read() except Exception, e: print e finally: f.close() step = history_step_script.step accounts = _account.get_accounts_by_params(is_delete=False) account_id = step.account.id if step.account else 0 elif step_type == enum_template.STEP_TYPE_PUSH_FILE: #分发文件. history_file_infos = HistoryFileInfo.objects.filter( step=history_step).order_by("location_type") template_file = "history/file_view.html" history_step_push_file = _history.get_historyStepPushFile_by_params( step=history_step) step = history_step_push_file.step elif step_type == enum_template.STEP_TYPE_PULL_FILE: #拉取文件. history_file_infos = HistoryFileInfo.objects.filter( step=history_step).order_by("location_type")
def account_list(request): name = request.GET.get('name', None) name_abbr = request.GET.get('name_abbr', None) update_user = request.GET.get('update_user', None) created_from = request.GET.get('created_from', None) created_to = request.GET.get('created_to', None) updated_from = request.GET.get('updated_from', None) updated_to = request.GET.get('updated_to', None) iDisplayLength = int(request.GET.get('iDisplayLength')) iDisplayStart = int(request.GET.get('iDisplayStart')) sEcho = int(request.GET.get('sEcho')) # sSearch = request.GET.get('sSearch', None) iSortCol_0 = int(request.GET.get('iSortCol_0', 0)) sSortDir_0 = request.GET.get('sSortDir_0') order_list = [ 'name', 'create_user__username', 'name_abbr', 'created', 'updated', 'update_user__username', None ] order_item = order_list[iSortCol_0] user = request.user accounts = _account.get_accounts_by_params(is_delete=False, create_user=user) # if sSearch: # pass #查询 if name: accounts = accounts.filter(name__icontains=name) if name_abbr: accounts = accounts.filter(name_abbr__icontains=name_abbr) one_day = datetime.timedelta(days=1) if update_user: accounts = accounts.filter( update_user__username__icontains=update_user) if created_from: accounts = accounts.filter(created__gte=created_from) if created_to: created_to = datetime.datetime.strptime(created_to, '%Y-%m-%d') accounts = accounts.filter(created__lte=(created_to + one_day)) if updated_from: accounts = accounts.filter(updated__gte=updated_from) if updated_to: updated_to = datetime.datetime.strptime(updated_to, '%Y-%m-%d') accounts = accounts.filter(updated__lte=(updated_to + one_day)) #排序 if order_item: if sSortDir_0 == "desc": order_item = '-%s' % order_item accounts = accounts.order_by(order_item) else: accounts = accounts.order_by('id') #第一个参数表示需要分页的对象,可为list、tuple、QuerySet,只要它有count()或者__len__()函数 #第二个参数为每页显示记录数 p = Paginator(accounts, iDisplayLength) total = p.count #总数 page_range = p.page_range #页数list page = p.page(page_range[iDisplayStart / iDisplayLength]) object_list = page.object_list sdicts = {} sdicts["sEcho"] = sEcho sdicts["iTotalRecords"] = total sdicts["iTotalDisplayRecords"] = total sdicts["aaData"] = [] for obj in object_list: #封装数组,按照列数填入对应的值 # check_html = u"<input type='checkbox' name='sonCkb' value='%s' id='sonCkb'/>" % obj.id change_html = '<a style="color: blue;" href="javascript:void(0)" onclick="javascript:account_edit(\'%s\',\'%s\');">%s</a>' % ( obj.id, obj.name, obj.name) delete_url = u'/account/del/%d/' % obj.id delete_html = '<a style="color: blue;" href="javascript:void(0)" onclick="javascript:account_del($(\'#account_form\'),\'%s\',\'POST\');">%s</a>' % ( delete_url, u'删除账户', ) created = datetime.datetime.strftime(obj.created, '%Y-%m-%d %H:%M:%S') updated = datetime.datetime.strftime(obj.updated, '%Y-%m-%d %H:%M:%S') data = [ change_html, obj.create_user.username, obj.name_abbr, created, updated, obj.update_user.username, delete_html ] sdicts["aaData"].append(data) return HttpResponse(json.dumps(sdicts, cls=LazyEncoder))
def template_step_edit_v2(request,template_step_id): """ edit template step. """ req = request.POST user = request.user status = 500 data = {} msg = "" try: step = _template.get_templateStep_by_params(id=template_step_id) if not step: msg = u"步骤不存在" raise Exception,msg # if step.template.create_user!=user: # msg = u"您不是该步骤的所有者" # raise Exception,msg type_value = step.step_type template_file = "" if type_value == enum_template.STEP_TYPE_SCRIPT: template_file = "job_manage/edit_script.html" sub_step = _template.get_templateStepScript_by_params(step=step) perms = _account.get_perms_by_params(to_user=user, is_delete=False, ptype=enum_account.PERM_PTYPE_SCRIPT) scripts = _script.Script.objects.filter(Q(create_user=user)|Q(pk__in=[perm.object_id for perm in perms]), is_delete=False,is_once=False) version = sub_step.version script = version.script if version and version.script in scripts else None versions = _script.get_versions_by_params(script=script,is_delete=False) if script else None scrip_id_list = [script.id for script in scripts] user_set = set() user_set.add(user) accounts_perm = _account.get_perms_by_params(object_id__in=scrip_id_list, is_delete=False) for account_perm in accounts_perm: user_set.add(account_perm.create_user) user_set.add(account_perm.to_user) accounts = _account.get_accounts_by_params(is_delete=False,create_user__in=list(user_set)) accountForm = AccountForm(auto_id="step_script_%s") account_id = step.account.id if step.account else 0 elif type_value == enum_template.STEP_TYPE_PULL_FILE: template_file = "job_manage/edit_pull_file.html" sub_step = _template.get_templateStepPullFile_by_params(step=step) file_paths = [] if not sub_step.file_paths else json.loads(sub_step.file_paths) elif type_value == enum_template.STEP_TYPE_PUSH_FILE: template_file = "job_manage/edit_send_file.html" sub_step = _template.get_templateStepPushFile_by_params(step=step) file_infos = _template.get_templateFileInfos_by_params(step=step) local_uploads = file_infos.filter(location_type=enum_template.UPLOAD_TYPE_LOCAL).values('record') remote_uploads = file_infos.filter(location_type=enum_template.UPLOAD_TYPE_REMOTE) local_ids = "" if not local_uploads else " ".join(str(item.get('record')) for item in local_uploads)+" " remote_uploads_str = "" if remote_uploads.count()>0: remote_dict = {} remote_dict['remote'] = [{"name":item.remote_path,"ip":item.remote_ip} for item in remote_uploads] remote_uploads_str = json.dumps(remote_dict) user = request.user max_upload_size = MAX_UPLOAD_SIZE fileserver_url = FILE_SERVER_HOST elif type_value == enum_template.STEP_TYPE_TEXT: template_file = "job_manage/edit_text.html" sub_step = _template.get_templateStepText_by_params(step=step) if type_value in [enum_template.STEP_TYPE_PULL_FILE,enum_template.STEP_TYPE_PUSH_FILE,enum_template.STEP_TYPE_SCRIPT]: target_ips = json.loads(step.target) if step.target else [] hide_ip_dict = build_hidden_ip_dict(target_ips) hide_ip_json = json.dumps(hide_ip_dict) status=200 data['html'] = render_to_string(template_file,locals()) data['name'] = step.name except Exception,e: data['msg'] = msg if msg else u"步骤信息获取失败" error = traceback.format_exc() logger.error(error) print error
def template_step_edit_v2(request, template_step_id): """ edit template step. """ req = request.POST user = request.user status = 500 data = {} msg = "" try: step = _template.get_templateStep_by_params(id=template_step_id) if not step: msg = u"步骤不存在" raise Exception, msg # if step.template.create_user!=user: # msg = u"您不是该步骤的所有者" # raise Exception,msg type_value = step.step_type template_file = "" if type_value == enum_template.STEP_TYPE_SCRIPT: template_file = "job_manage/edit_script.html" sub_step = _template.get_templateStepScript_by_params(step=step) perms = _account.get_perms_by_params( to_user=user, is_delete=False, ptype=enum_account.PERM_PTYPE_SCRIPT) scripts = _script.Script.objects.filter( Q(create_user=user) | Q(pk__in=[perm.object_id for perm in perms]), is_delete=False, is_once=False) version = sub_step.version script = version.script if version and version.script in scripts else None versions = _script.get_versions_by_params( script=script, is_delete=False) if script else None scrip_id_list = [script.id for script in scripts] user_set = set() user_set.add(user) accounts_perm = _account.get_perms_by_params( object_id__in=scrip_id_list, is_delete=False) for account_perm in accounts_perm: user_set.add(account_perm.create_user) user_set.add(account_perm.to_user) accounts = _account.get_accounts_by_params( is_delete=False, create_user__in=list(user_set)) accountForm = AccountForm(auto_id="step_script_%s") account_id = step.account.id if step.account else 0 elif type_value == enum_template.STEP_TYPE_PULL_FILE: template_file = "job_manage/edit_pull_file.html" sub_step = _template.get_templateStepPullFile_by_params(step=step) file_paths = [] if not sub_step.file_paths else json.loads( sub_step.file_paths) elif type_value == enum_template.STEP_TYPE_PUSH_FILE: template_file = "job_manage/edit_send_file.html" sub_step = _template.get_templateStepPushFile_by_params(step=step) file_infos = _template.get_templateFileInfos_by_params(step=step) local_uploads = file_infos.filter( location_type=enum_template.UPLOAD_TYPE_LOCAL).values('record') remote_uploads = file_infos.filter( location_type=enum_template.UPLOAD_TYPE_REMOTE) local_ids = "" if not local_uploads else " ".join( str(item.get('record')) for item in local_uploads) + " " remote_uploads_str = "" if remote_uploads.count() > 0: remote_dict = {} remote_dict['remote'] = [{ "name": item.remote_path, "ip": item.remote_ip } for item in remote_uploads] remote_uploads_str = json.dumps(remote_dict) user = request.user max_upload_size = MAX_UPLOAD_SIZE fileserver_url = FILE_SERVER_HOST elif type_value == enum_template.STEP_TYPE_TEXT: template_file = "job_manage/edit_text.html" sub_step = _template.get_templateStepText_by_params(step=step) if type_value in [ enum_template.STEP_TYPE_PULL_FILE, enum_template.STEP_TYPE_PUSH_FILE, enum_template.STEP_TYPE_SCRIPT ]: target_ips = json.loads(step.target) if step.target else [] hide_ip_dict = build_hidden_ip_dict(target_ips) hide_ip_json = json.dumps(hide_ip_dict) status = 200 data['html'] = render_to_string(template_file, locals()) data['name'] = step.name except Exception, e: data['msg'] = msg if msg else u"步骤信息获取失败" error = traceback.format_exc() logger.error(error) print error
def template_edit(request, template_id): """ show detail of the template after clicked the button. """ user = request.user status = 500 data = {} try: method = request.method template_file = "md_manage/edit_md.html" check_id = datetime.datetime.now().strftime("%Y%m%d%H%M%S") template = _template.get_template_by_params(id=template_id) ajax_url = "/md_manage/template_step/list/{0}/".format(template.pk) accounts = _account.get_accounts_by_params(is_delete=False) accountForm = AccountForm() target_ips = json.loads(template.target) if template.target else [] hide_ip_dict = build_hidden_ip_dict(target_ips) hide_ip_json = json.dumps(hide_ip_dict) if method == 'POST': templateForm = EditTemplateForm(request.POST, instance=template, user=request.user) check_id = request.POST.get("check_id", check_id) work_type = request.POST.get("work_type", "") data.update({"template_id": template.id, "check_id": check_id}) if templateForm.is_valid(): user = request.user template = templateForm.save(commit=False) template.update_user = user template.work_type = work_type template.save() status = 200 else: html = render_to_string(template_file, locals()) data.update({"html": html}) data.update({"template_name": template.name}) return HttpResponse( json.dumps({ "status": status, "result": data, }, cls=LazyEncoder)) else: account_id = template.account.id if template.account else 0 template_name = template.name templateForm = EditTemplateForm(instance=template, user=request.user) work_type = template.work_type html = render_to_string(template_file, locals()) status = 200 data.update({ "html": html, "template_name": template_name, "work_type": work_type }) except: error = traceback.format_exc() logger.error(error) print error data['msg'] = u"服务器错误" return HttpResponse( json.dumps({ "status": status, "result": data }, cls=LazyEncoder))
template_file = "history/script_view.html" history_step_script = _history.get_historyStepScript_by_params(step=history_step) version = history_step_script.version script = version.script file_path = os.path.join(MEDIA_ROOT,'scripts','%s'%version.sfile) file_content = '' f = open(file_path) try: file_content = f.read() except Exception,e: print e finally: f.close() step = history_step_script.step accounts = _account.get_accounts_by_params(is_delete=False) account_id = step.account.id if step.account else 0 elif step_type == enum_template.STEP_TYPE_PUSH_FILE: #分发文件. history_file_infos = HistoryFileInfo.objects.filter(step=history_step).order_by("location_type") template_file = "history/file_view.html" history_step_push_file = _history.get_historyStepPushFile_by_params(step=history_step) step = history_step_push_file.step elif step_type == enum_template.STEP_TYPE_PULL_FILE: #拉取文件. history_file_infos = HistoryFileInfo.objects.filter(step=history_step).order_by("location_type") template_file = "history/pullfile_view.html" history_step_pull_file = _history.get_historyStepPullFile_by_params(step=history_step) file_paths = json.loads(history_step_pull_file.file_paths)