def updateProblemSubmit(request, p_id): logger.info(str(request).replace("\n","\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') if not p_id: raise Exception(u'题号错误') p_id = int(p_id) ep = Problem.getById(p_id) if not ep: raise Exception(u'题号错误') if not ep.canManageProblem(u): raise Err(request, 'no priv') p = request.POST if request.method == 'POST': form = addProblemForm(request.POST) if not form.is_valid(): raise Exception(u'数据输入有误') prb = ep data_count=prb.data_count case_info=prb.case_info if p['change_data'] == "1": dataInList=request.FILES.getlist('data_in') dataOutList=request.FILES.getlist('data_out') dataScrList=request.POST.getlist('data_scr') if len(dataInList)!=len(dataOutList) or len(dataInList)!=len(dataScrList): raise Exception(u'上传文件有误') for idx, nowData in enumerate(dataInList): path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".in" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) for idx, nowData in enumerate(dataOutList): path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".out" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) dict={} for idx, nowScr in enumerate(dataScrList): dict[str(idx)]=nowScr case_info=json.dumps(dict) data_count=len(dataInList) dict={} dict['desc']=p['prob_desc'] dict['input_desc']=p['prob_input_desc'] dict['output_desc']=p['prob_output_desc'] dict['input_sample']=p['prob_input_sample'] dict['output_sample']=p['prob_output_sample'] prob_desc=json.dumps(dict) prb.updateProblem(prb.author.uid, p['prob_priv'], p['prob_title'], p['prob_time'], p['prob_memory'], p['prob_codelength'], prob_desc, p['is_spj'], data_count, p['course_id'], case_info) return redirect("/problem/p/"+str(prb.pid)+"/") except Exception as e: logger.error(str(e).replace("\n","\t")) return render(request, Err.ERROR_PAGE)
def addProblemSubmit(request): logger.info(str(request).replace("\n","\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') p = request.POST if request.method == 'POST': form = addProblemForm(request.POST) if not form.is_valid(): raise Exception(u'数据输入有误') course_id = int(p['course_id']) if course_id == None: raise Exception(u'课程非法') cs = Course.getById(course_id) if not cs: raise Exception(u'课程号非法') if not Problem.canAddCourseProblem(cs, u): raise Err(request, 'no priv') prb = Problem.addProblem(u.uid, p['prob_priv'], p['prob_title'], p['prob_time'], p['prob_memory'], p['prob_codelength'], p['prob_desc'], p['is_spj'], 0, p['course_id'], "") data_count=0 case_info="" #If file not exist, Create an empty file. open(Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+"0.in", 'a').close() open(Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+"0.out", 'a').close() if p['change_data'] == "1": dataInList=request.FILES.getlist('data_in') dataOutList=request.FILES.getlist('data_out') dataScrList=request.POST.getlist('data_scr') if len(dataInList)!=len(dataOutList) or len(dataInList)!=len(dataScrList): raise Exception(u'上传文件有误') for idx, nowData in enumerate(dataInList): path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".in" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) for idx, nowData in enumerate(dataOutList): path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".out" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) dict={} for idx, nowScr in enumerate(dataScrList): dict[str(idx)]=nowScr case_info=json.dumps(dict) data_count=len(dataInList) dict={} dict['desc']=p['prob_desc'] dict['input_desc']=p['prob_input_desc'] dict['output_desc']=p['prob_output_desc'] dict['input_sample']=p['prob_input_sample'] dict['output_sample']=p['prob_output_sample'] prob_desc=json.dumps(dict) prb.updateProblem(u.uid, p['prob_priv'], p['prob_title'], p['prob_time'], p['prob_memory'], p['prob_codelength'], prob_desc, p['is_spj'], data_count, p['course_id'], case_info) return redirect("/problem/p/"+str(prb.pid)+"/") except Exception as e: logger.error(str(e).replace("\n","\t")) return render(request, Err.ERROR_PAGE, {'errmsg': unicode(e)})
def updateProblem(request, p_id): logger.info(str(request).replace("\n","\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') try: p_id = int(p_id) p = Problem.getById(p_id) except: logger.error(str(e).replace("\n","\t")) raise Err(request, 'unknown err') if not p.canManageProblem(u): raise Err(request, 'no priv') if request.method == 'POST': form = addProblemForm(request.POST) if form.is_valid(): data_count=p.data_count case_info=p.case_info if form.cleaned_data['change_data'] == "1": dataInList=request.FILES.getlist('data_in') dataOutList=request.FILES.getlist('data_out') dataScrList=request.POST.getlist('data_scr') if len(dataInList)!=len(dataOutList) or len(dataInList)!=len(dataScrList): raise Err(request, 'unknown err') for idx, nowData in enumerate(dataInList): path = Const.PROBLEM_DATA_PATH+str(p.pid)+"/"+str(idx)+".in" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) for idx, nowData in enumerate(dataOutList): path = Const.PROBLEM_DATA_PATH+str(p.pid)+"/"+str(idx)+".out" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) dict={} for idx, nowScr in enumerate(dataScrList): dict[str(idx)]=nowScr case_info=json.dumps(dict) data_count=len(dataInList) dict={} dict['desc']=form.cleaned_data['prob_desc'] dict['input_desc']=form.cleaned_data['prob_input_desc'] dict['output_desc']=form.cleaned_data['prob_output_desc'] dict['input_sample']=form.cleaned_data['prob_input_sample'] dict['output_sample']=form.cleaned_data['prob_output_sample'] prob_desc=json.dumps(dict) p.updateProblem(u.uid, form.cleaned_data['prob_priv'], form.cleaned_data['prob_title'], form.cleaned_data['prob_time'], form.cleaned_data['prob_memory'], form.cleaned_data['prob_codelength'], prob_desc, form.cleaned_data['is_spj'], data_count, p.course_id, case_info) return redirect("/problem/p/"+str(p.pid)+"/") else: raise Err(request, 'unknown err') else: desc=json.loads(p.prob_desc) form = addProblemForm( initial={ 'prob_title': p.prob_title, 'prob_priv': p.prob_priv, 'prob_time': p.prob_time, 'prob_memory': p.prob_memory, 'prob_codelength': p.prob_codelength, 'prob_desc': desc['desc'], 'prob_input_desc': desc['input_desc'], 'prob_output_desc': desc['output_desc'], 'prob_input_sample': desc['input_sample'], 'prob_output_sample': desc['output_sample'], 'is_spj': p.is_spj, 'change_data': 0 } ) cases = range(p.data_count) return render(request,'newtpl/problem/modifyProblem.html',{'problem':p, 'form': form,'tpl':{'sp':True}, 'update':True, 'cases': cases}) except Exception as e: logger.error(str(e).replace("\n","\t")) return render(request, Err.ERROR_PAGE)
def addProblem(request, course_id): # modified logger.info(str(request).replace("\n","\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') if course_id == None: return redirect('/problem/addProblem/') try: course_id = int(course_id) cs = Course.getById(course_id) except: raise Exception(u'课程号非法') if not Problem.canAddCourseProblem(cs, u): raise Err(request, 'no priv') if request.method == 'POST': form = addProblemForm(request.POST) if form.is_valid(): prb = Problem.addProblem(u.uid, form.cleaned_data['prob_priv'], form.cleaned_data['prob_title'], form.cleaned_data['prob_time'], form.cleaned_data['prob_memory'], form.cleaned_data['prob_codelength'], form.cleaned_data['prob_desc'], form.cleaned_data['is_spj'], 0, course_id, "") data_count=0 case_info="" if form.cleaned_data['change_data'] == "1": dataInList=request.FILES.getlist('data_in') dataOutList=request.FILES.getlist('data_out') dataScrList=request.POST.getlist('data_scr') if len(dataInList)!=len(dataOutList) or len(dataInList)!=len(dataScrList): raise Exception(u'上传文件有误') for idx, nowData in enumerate(dataInList): path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".in" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) for idx, nowData in enumerate(dataOutList): path = Const.PROBLEM_DATA_PATH+str(prb.pid)+"/"+str(idx)+".out" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) dict={} for idx, nowScr in enumerate(dataScrList): dict[str(idx)]=nowScr case_info=json.dumps(dict) data_count=len(dataInList) dict={} dict['desc']=form.cleaned_data['prob_desc'] dict['input_desc']=form.cleaned_data['prob_input_desc'] dict['output_desc']=form.cleaned_data['prob_output_desc'] dict['input_sample']=form.cleaned_data['prob_input_sample'] dict['output_sample']=form.cleaned_data['prob_output_sample'] prob_desc=json.dumps(dict) prb.updateProblem(u.uid, form.cleaned_data['prob_priv'], form.cleaned_data['prob_title'], form.cleaned_data['prob_time'], form.cleaned_data['prob_memory'], form.cleaned_data['prob_codelength'], prob_desc, form.cleaned_data['is_spj'], data_count, course_id, case_info) return redirect("/problem/p/"+str(prb.pid)+"/") else: raise Err(request, 'problem info illegal') else: form = addProblemForm() return render(request,'newtpl/problem/modifyProblem.html',{'form': form,'course': cs, 'tpl':{'sp':True}}) except Exception as e: logger.error(str(e).replace("\n","\t")) return render(request, Err.ERROR_PAGE)
def updateProblemSubmit(request, p_id): logger.info(str(request).replace("\n", "\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') if not p_id: raise Exception(u'题号错误') p_id = int(p_id) ep = Problem.getById(p_id) if not ep: raise Exception(u'题号错误') if not ep.canManageProblem(u): raise Err(request, 'no priv') p = request.POST if request.method == 'POST': form = addProblemForm(request.POST) if not form.is_valid(): raise Exception(u'数据输入有误') prb = ep data_count = prb.data_count case_info = prb.case_info if p['change_data'] == "1": dataInList = request.FILES.getlist('data_in') dataOutList = request.FILES.getlist('data_out') dataScrList = request.POST.getlist('data_scr') if len(dataInList) != len(dataOutList) or len(dataInList) != len( dataScrList): raise Exception(u'上传文件有误') for idx, nowData in enumerate(dataInList): path = Const.PROBLEM_DATA_PATH + str( prb.pid) + "/" + str(idx) + ".in" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) for idx, nowData in enumerate(dataOutList): path = Const.PROBLEM_DATA_PATH + str( prb.pid) + "/" + str(idx) + ".out" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) dict = {} for idx, nowScr in enumerate(dataScrList): dict[str(idx)] = nowScr case_info = json.dumps(dict) data_count = len(dataInList) dict = {} dict['desc'] = p['prob_desc'] dict['input_desc'] = p['prob_input_desc'] dict['output_desc'] = p['prob_output_desc'] dict['input_sample'] = p['prob_input_sample'] dict['output_sample'] = p['prob_output_sample'] prob_desc = json.dumps(dict) prb.updateProblem(prb.author.uid, p['prob_priv'], p['prob_title'], p['prob_time'], p['prob_memory'], p['prob_codelength'], prob_desc, p['is_spj'], data_count, p['course_id'], case_info) return redirect("/problem/p/" + str(prb.pid) + "/") except Exception as e: logger.error(str(e).replace("\n", "\t")) return render(request, Err.ERROR_PAGE)
def updateProblem(request, p_id): logger.info(str(request).replace("\n", "\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') try: p_id = int(p_id) p = Problem.getById(p_id) except: logger.error(str(e).replace("\n", "\t")) raise Err(request, 'unknown err') if not p.canManageProblem(u): raise Err(request, 'no priv') if request.method == 'POST': form = addProblemForm(request.POST) if form.is_valid(): data_count = p.data_count case_info = p.case_info if form.cleaned_data['change_data'] == "1": dataInList = request.FILES.getlist('data_in') dataOutList = request.FILES.getlist('data_out') dataScrList = request.POST.getlist('data_scr') if len(dataInList) != len(dataOutList) or len( dataInList) != len(dataScrList): raise Err(request, 'unknown err') for idx, nowData in enumerate(dataInList): path = Const.PROBLEM_DATA_PATH + str( p.pid) + "/" + str(idx) + ".in" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) for idx, nowData in enumerate(dataOutList): path = Const.PROBLEM_DATA_PATH + str( p.pid) + "/" + str(idx) + ".out" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) dict = {} for idx, nowScr in enumerate(dataScrList): dict[str(idx)] = nowScr case_info = json.dumps(dict) data_count = len(dataInList) dict = {} dict['desc'] = form.cleaned_data['prob_desc'] dict['input_desc'] = form.cleaned_data['prob_input_desc'] dict['output_desc'] = form.cleaned_data['prob_output_desc'] dict['input_sample'] = form.cleaned_data['prob_input_sample'] dict['output_sample'] = form.cleaned_data['prob_output_sample'] prob_desc = json.dumps(dict) p.updateProblem(u.uid, form.cleaned_data['prob_priv'], form.cleaned_data['prob_title'], form.cleaned_data['prob_time'], form.cleaned_data['prob_memory'], form.cleaned_data['prob_codelength'], prob_desc, form.cleaned_data['is_spj'], data_count, p.course_id, case_info) return redirect("/problem/p/" + str(p.pid) + "/") else: raise Err(request, 'unknown err') else: desc = json.loads(p.prob_desc) form = addProblemForm( initial={ 'prob_title': p.prob_title, 'prob_priv': p.prob_priv, 'prob_time': p.prob_time, 'prob_memory': p.prob_memory, 'prob_codelength': p.prob_codelength, 'prob_desc': desc['desc'], 'prob_input_desc': desc['input_desc'], 'prob_output_desc': desc['output_desc'], 'prob_input_sample': desc['input_sample'], 'prob_output_sample': desc['output_sample'], 'is_spj': p.is_spj, 'change_data': 0 }) cases = range(p.data_count) return render( request, 'newtpl/problem/modifyProblem.html', { 'problem': p, 'form': form, 'tpl': { 'sp': True }, 'update': True, 'cases': cases }) except Exception as e: logger.error(str(e).replace("\n", "\t")) return render(request, Err.ERROR_PAGE)
def addProblemSubmit(request): logger.info(str(request).replace("\n", "\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') p = request.POST if request.method == 'POST': form = addProblemForm(request.POST) if not form.is_valid(): raise Exception(u'数据输入有误') course_id = int(p['course_id']) if course_id == None: raise Exception(u'课程非法') cs = Course.getById(course_id) if not cs: raise Exception(u'课程号非法') if not Problem.canAddCourseProblem(cs, u): raise Err(request, 'no priv') prb = Problem.addProblem(u.uid, p['prob_priv'], p['prob_title'], p['prob_time'], p['prob_memory'], p['prob_codelength'], p['prob_desc'], p['is_spj'], 0, p['course_id'], "") data_count = 0 case_info = "" #If file not exist, Create an empty file. open(Const.PROBLEM_DATA_PATH + str(prb.pid) + "/" + "0.in", 'a').close() open(Const.PROBLEM_DATA_PATH + str(prb.pid) + "/" + "0.out", 'a').close() if p['change_data'] == "1": dataInList = request.FILES.getlist('data_in') dataOutList = request.FILES.getlist('data_out') dataScrList = request.POST.getlist('data_scr') if len(dataInList) != len(dataOutList) or len(dataInList) != len( dataScrList): raise Exception(u'上传文件有误') for idx, nowData in enumerate(dataInList): path = Const.PROBLEM_DATA_PATH + str( prb.pid) + "/" + str(idx) + ".in" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) for idx, nowData in enumerate(dataOutList): path = Const.PROBLEM_DATA_PATH + str( prb.pid) + "/" + str(idx) + ".out" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) dict = {} for idx, nowScr in enumerate(dataScrList): dict[str(idx)] = nowScr case_info = json.dumps(dict) data_count = len(dataInList) dict = {} dict['desc'] = p['prob_desc'] dict['input_desc'] = p['prob_input_desc'] dict['output_desc'] = p['prob_output_desc'] dict['input_sample'] = p['prob_input_sample'] dict['output_sample'] = p['prob_output_sample'] prob_desc = json.dumps(dict) prb.updateProblem(u.uid, p['prob_priv'], p['prob_title'], p['prob_time'], p['prob_memory'], p['prob_codelength'], prob_desc, p['is_spj'], data_count, p['course_id'], case_info) return redirect("/problem/p/" + str(prb.pid) + "/") except Exception as e: logger.error(str(e).replace("\n", "\t")) return render(request, Err.ERROR_PAGE, {'errmsg': unicode(e)})
def addProblem(request, course_id): # modified logger.info(str(request).replace("\n", "\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') if course_id == None: return redirect('/problem/addProblem/') try: course_id = int(course_id) cs = Course.getById(course_id) except: raise Exception(u'课程号非法') if not Problem.canAddCourseProblem(cs, u): raise Err(request, 'no priv') if request.method == 'POST': form = addProblemForm(request.POST) if form.is_valid(): prb = Problem.addProblem(u.uid, form.cleaned_data['prob_priv'], form.cleaned_data['prob_title'], form.cleaned_data['prob_time'], form.cleaned_data['prob_memory'], form.cleaned_data['prob_codelength'], form.cleaned_data['prob_desc'], form.cleaned_data['is_spj'], 0, course_id, "") data_count = 0 case_info = "" if form.cleaned_data['change_data'] == "1": dataInList = request.FILES.getlist('data_in') dataOutList = request.FILES.getlist('data_out') dataScrList = request.POST.getlist('data_scr') if len(dataInList) != len(dataOutList) or len( dataInList) != len(dataScrList): raise Exception(u'上传文件有误') for idx, nowData in enumerate(dataInList): path = Const.PROBLEM_DATA_PATH + str( prb.pid) + "/" + str(idx) + ".in" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) for idx, nowData in enumerate(dataOutList): path = Const.PROBLEM_DATA_PATH + str( prb.pid) + "/" + str(idx) + ".out" if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, nowData) dict = {} for idx, nowScr in enumerate(dataScrList): dict[str(idx)] = nowScr case_info = json.dumps(dict) data_count = len(dataInList) dict = {} dict['desc'] = form.cleaned_data['prob_desc'] dict['input_desc'] = form.cleaned_data['prob_input_desc'] dict['output_desc'] = form.cleaned_data['prob_output_desc'] dict['input_sample'] = form.cleaned_data['prob_input_sample'] dict['output_sample'] = form.cleaned_data['prob_output_sample'] prob_desc = json.dumps(dict) prb.updateProblem(u.uid, form.cleaned_data['prob_priv'], form.cleaned_data['prob_title'], form.cleaned_data['prob_time'], form.cleaned_data['prob_memory'], form.cleaned_data['prob_codelength'], prob_desc, form.cleaned_data['is_spj'], data_count, course_id, case_info) return redirect("/problem/p/" + str(prb.pid) + "/") else: raise Err(request, 'problem info illegal') else: form = addProblemForm() return render(request, 'newtpl/problem/modifyProblem.html', { 'form': form, 'course': cs, 'tpl': { 'sp': True } }) except Exception as e: logger.error(str(e).replace("\n", "\t")) return render(request, Err.ERROR_PAGE)