def choose(req): if req.method == 'GET': if App.objects.exists(): return ren2res("jobs/choose.html", req, {'apps': App.objects.filter(hide=False)}) else: return ren2res("jobs/choose.html", req, {'info': "抱歉,还没有可用的应用!"})
def host(req): if not req.user.is_superuser: return HttpResponseRedirect("/info/not_admin") hostlist = Host.objects.all() if req.method == "GET": return ren2res("apps/apps_host.html", req, paginate(req, hostlist)) elif req.method == "POST": i = 0 page = paginate(req, hostlist) submit = [] # get host id, refer to apps_host_add.html for n in req.POST.keys(): if n[0:6] == "hostid": host_id = n[6:] try: item = Host.objects.get(id=host_id) except: page.update(err="无效的主机ID") return ren2res("apps/apps_host.html", req, page) name = req.POST["name" + host_id].strip() ip = req.POST["ip" + host_id].strip() port = req.POST["port" + host_id].strip() port = port if port != "" else SERVANT_PORT error = host_check(name, ip, port) if error: page.update(err=error) return ren2res("apps/apps_host.html", req, page) item.name = name item.ip = ip item.port = port submit.append(item) for x in submit: x.save() page.update(info="修改成功") return ren2res("apps/apps_host.html", req, page)
def modify(req, n): if not req.user.is_superuser: return HttpResponseRedirect("/info/not_admin") # ignore GET method if req.method == "POST": # If there is not an app whose id = n ,raise 404 try: app = App.objects.get(id=n) except: raise Http404() # check the info info = app_info_check(req, n) param = Param.objects.filter(app_id=n).order_by("order") # if some errors, return error warning if info["err"]: hostlist = Host.objects.all().order_by("id") dict = {"app_i": app, "param_i": param, "host": hostlist, "err": info["err"]} return ren2res("apps/apps_detail.html", req, dict) app_submit = info["submit"] # check the parameters args = app_arg_check(req) if args["err"]: hostlist = Host.objects.all() dict = {"app_i": app, "param_i": param, "host": hostlist, "err": args["err"]} return ren2res("apps/apps_detail.html", req, dict) # uid is id of who deploy the app at first app_submit.uid_id = app.uid_id app_submit.save() param.delete() for x in args["list_submit"]: x.app_id = n x.save() return HttpResponseRedirect("/apps/")
def submit(req, aid): if req.method == 'GET': try: app = App.objects.get(id=aid) except: raise Http404() params = Param.objects.filter(app=app).filter( name__isnull=False).order_by("order") dict = {'app': app, 'params': params} return ren2res("jobs/submit.html", req, dict) elif req.method == 'POST': id = req.POST['id'] params = Param.objects.filter(app=int(id)).order_by("order") cmd = "" for param in params: value = req.POST.get(str(param.order)) if value is None: cmd += str(param.value) + " " elif not value.strip() and not param.blank: try: return ren2res( "jobs/submit.html", req, { 'app': App.objects.get(id=aid), 'params': params.filter(name__isnull=False), 'err': "带*的参数不能为空!" }) except: raise Http404() else: cmd += value + " " job = Job(uid=req.user, app_id=id, cmd=cmd.strip()) job.save() client.start(job.id) return HttpResponseRedirect("/jobs/")
def deploy(req): if not req.user.is_superuser: return HttpResponseRedirect("/info/not_admin") hostlist = Host.objects.all() # GET method requests a deploy page if req.method == "GET": return ren2res("apps/apps_deploy.html", req, {"host": hostlist}) # request to deploy a new application elif req.method == "POST": # add application info to app table info = app_info_check(req) if info["err"]: return ren2res("apps/apps_deploy.html", req, {"host": hostlist, "err": info["err"]}) uid = req.user.id app_submit = info["submit"] app_submit.uid_id = uid # add valid args to para table args = app_arg_check(req) if args["err"]: return ren2res("apps/apps_deploy.html", req, {"host": hostlist, "err": args["err"]}) app_submit.save() app_id = app_submit.id for x in args["list_submit"]: x.app_id = app_id x.save() return ren2res("apps/apps_deploy.html", req, {"host": hostlist, "info": "部署成功"})
def submit(req,aid): if req.method == 'GET': try: app = App.objects.get(id=aid) except: raise Http404() params = Param.objects.filter(app=app).filter(name__isnull=False).order_by("order") dict={'app':app,'params':params} return ren2res("jobs/submit.html",req,dict) elif req.method == 'POST': id = req.POST['id'] params = Param.objects.filter(app=int(id)).order_by("order") cmd="" for param in params: value=req.POST.get(str(param.order)) if value is None: cmd+=str(param.value)+" " elif not value.strip() and not param.blank: try: return ren2res("jobs/submit.html",req,{'app':App.objects.get(id=aid), 'params':params.filter(name__isnull=False), 'err':"带*的参数不能为空!"}) except: raise Http404() else: cmd+=value+" " job=Job(uid=req.user,app_id=id,cmd=cmd.strip()) job.save() client.start(job.id) return HttpResponseRedirect("/jobs/")
def list(req): if req.method == 'GET': jobs = Job.objects.all() if not req.user.is_superuser: jobs = jobs.filter(uid=req.user) if jobs.exists(): jobs = jobs.order_by("-add_time") return ren2res("jobs/list.html", req, paginate(req, jobs)) else: return ren2res("jobs/list.html", req, {'info': "没有提交的作业!"})
def list(req): if req.method=='GET': jobs=Job.objects.all() if not req.user.is_superuser: jobs=jobs.filter(uid=req.user) if jobs.exists(): jobs=jobs.order_by("-add_time") return ren2res("jobs/list.html",req,paginate(req,jobs)) else: return ren2res("jobs/list.html",req,{'info':"没有提交的作业!"})
def host_add(req): if not req.user.is_superuser: return HttpResponseRedirect("/info/not_admin") if req.method == "GET": return ren2res("apps/apps_host_add.html", req) elif req.method == "POST": name = req.POST["host_name"] ip = req.POST["host_ip"] port = req.POST["host_port"] port = port if port != "" else SERVANT_PORT # check ip and port, return error info if there is error = host_check(name, ip, port) if error: return ren2res("apps/apps_host_add.html", req, {"err": error}) Host(name=name, ip=ip, port=port).save() return ren2res("apps/apps_host_add.html", req, {"info": "主机添加成功"})
def list(req): if req.user.is_superuser: dict={'super':req.user.is_superuser} dict.update(paginate(req,User.objects.filter(is_active=True),8)) if req.method=='GET': return ren2res("user/list.html",req,dict) else : return HttpResponseRedirect("/info/not_admin/")
def download(req): if req.method=='GET': item=[] for f in os.listdir(DBDATA_DIR): nm=os.path.join(DBDATA_DIR,f) if os.path.isfile(nm): item.append({'name':f,'date':asctime(localtime(os.path.getmtime(nm))),'size':os.path.getsize(nm),'url':'/dbdata/'+f}) return ren2res('data/download.html',req,paginate(req,item))
def verify(req): if not req.user.is_superuser : return HttpResponseRedirect("/info/not_admin/") dict={'super':req.user.is_superuser} if req.method=='GET': dict.update(paginate(req,User.objects.filter(is_active=False).filter(last_login__isnull=True),8)) return ren2res("user/verify.html",req,dict) if req.method=='POST': try: a=User.objects.get(id=req.POST['id']) except ObjectDoesNotExist: dict.update(err="用户不存在") return ren2res("user/verify.html",req,dict) a.is_active=True a.save() dict.update(paginate(req,User.objects.filter(is_active=False),8)) dict.update(info="修改成功") return ren2res("user/verify.html",req,dict)
def app(req, n): if not req.user.is_superuser: return HttpResponseRedirect("/info/not_admin") if req.method == "GET": try: a = App.objects.get(id=n) except: raise Http404() p = Param.objects.filter(app_id=n).order_by("order") hostlist = Host.objects.all() return ren2res("apps/apps_detail.html", req, {"app_i": a, "param_i": p, "host": hostlist})
def change(req): b=req.user super=b.is_superuser if req.method=='GET': return ren2res("user/change.html",req,{'super':super}) if req.method=='POST': a=req.user npw=False if a.check_password(str(req.POST['opw'])): if req.POST['email']!="": a.email=req.POST['email'] if req.POST['npw']!='': a.set_password(str(req.POST['npw'])) npw=True a.save() if npw: return HttpResponseRedirect("/login/") else: return HttpResponseRedirect("/user/") else: return ren2res("user/change.html",req,{'err':"wrong password",'super':super})
def upload(req): if not req.user.is_superuser: return HttpResponseRedirect('/info/not_admin') if req.method == 'GET': return ren2res("platform/upload.html", req) elif req.method == 'POST': print("get") print(req.FILES) files = req.FILES.getlist('file') for f in files: submit = File(name=f.name, uid_id=req.user.id, path=UPLOAD_DIR) submit.save() try: destination = open(UPLOAD_DIR + '/' + str(submit.id) + '_' + str(f.name) , 'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() except: submit.delete() return ren2res("platform/upload.html", req, {'err': "文件上传失败"}) return ren2res("platform/upload.html", req, {'info': "上传成功"})
def download(req): if req.method == 'GET': item = [] for f in os.listdir(DBDATA_DIR): nm = os.path.join(DBDATA_DIR, f) if os.path.isfile(nm): item.append({ 'name': f, 'date': asctime(localtime(os.path.getmtime(nm))), 'size': os.path.getsize(nm), 'url': '/dbdata/' + f }) return ren2res('data/download.html', req, paginate(req, item))
def info(req,id=None): if req.method=='GET': super=req.user.is_superuser if id: if not super: return HttpResponseRedirect('/info/not_admin') try: u=User.objects.get(id=id) except: raise Http404() else: u=req.user return ren2res("user/info.html",req,{'super':super,'u':u})
def render(template,req,qs,field='trade_date'): q=QueryDict(mutable=True) dict={} qs=qs.using(USE_DB).annotate(the_filter_date=F(field)) sd=req.GET.get('sdate') if sd: q['sdate']=sd dict.update(sdate=sd) sd=datetime.strptime(sd,"%Y-%m-%d") qs=qs.filter(the_filter_date__gte=sd) ed=req.GET.get('edate') if ed: q['edate']=ed dict.update(edate=ed) ed=datetime.strptime(ed,"%Y-%m-%d") qs=qs.filter(the_filter_date__lte=ed) dict.update(paginate(req,qs)) return ren2res(template,req,dict)
def render(template, req, qs, field='trade_date'): q = QueryDict(mutable=True) dict = {} qs = qs.using(USE_DB).annotate(the_filter_date=F(field)) sd = req.GET.get('sdate') if sd: q['sdate'] = sd dict.update(sdate=sd) sd = datetime.strptime(sd, "%Y-%m-%d") qs = qs.filter(the_filter_date__gte=sd) ed = req.GET.get('edate') if ed: q['edate'] = ed dict.update(edate=ed) ed = datetime.strptime(ed, "%Y-%m-%d") qs = qs.filter(the_filter_date__lte=ed) dict.update(paginate(req, qs)) return ren2res(template, req, dict)
def detail(req, jid): if req.method == 'GET': try: job = Job.objects.get(id=jid) except: raise Http404() dict = {'job': job} try: out = open(os.path.join(RESULT_DIR, "out_" + str(job.id))) dict.update(std_out=out.read()) out.close() except: dict.update(std_out="") try: err = open(os.path.join(RESULT_DIR, "err_" + str(job.id))) dict.update(std_err=err.read()) err.close() except: dict.update(std_err="") return ren2res("jobs/detail.html", req, dict)
def detail(req,jid): if req.method=='GET': try: job=Job.objects.get(id=jid) except: raise Http404() dict={'job':job} try: out=open(os.path.join(RESULT_DIR,"out_"+str(job.id))) dict.update(std_out=out.read()) out.close() except: dict.update(std_out="") try: err=open(os.path.join(RESULT_DIR,"err_"+str(job.id))) dict.update(std_err=err.read()) err.close() except: dict.update(std_err="") return ren2res("jobs/detail.html",req,dict)
def list(req): if req.method == 'GET': f = File.objects.all() return ren2res("platform/files_list.html", req, paginate(req, f))
def apps(req): if not req.user.is_superuser: return HttpResponseRedirect("/info/not_admin") if req.method == "GET": p = App.objects.filter(hide=False).order_by("id") return ren2res("apps/apps_list.html", req, paginate(req, p))
def choose(req): if req.method=='GET': if App.objects.exists(): return ren2res("jobs/choose.html",req,{'apps':App.objects.filter(hide=False)}) else: return ren2res("jobs/choose.html",req,{'info':"抱歉,还没有可用的应用!"})