def updateProblem(self, uid, prob_priv, prob_title, prob_time, prob_memory, prob_codelength, prob_desc, is_spj, data_count, course_id, case_info): try: if (Problem.isLegalTitle(prob_title)): self.prob_title = prob_title else: raise Exception(Problem.TITLE_ERRMSG) if (Problem._isLegalDesc(prob_desc)): self.prob_desc = prob_desc else: raise Exception(Problem.DESC_ERRMSG) self.prob_priv = prob_priv self.prob_title = prob_title self.prob_time = prob_time self.prob_memory = prob_memory self.prob_codelength = prob_codelength self.is_spj = is_spj #self.author = User.getById(uid) #will change the author... self.data_count = data_count self.course = Course.getById(course_id) self.case_info = case_info self.save() return True except Exception as e: raise e
def addProblem(cls, uid, prob_priv, prob_title, prob_time, prob_memory, prob_codelength, prob_desc, is_spj, data_count, course_id, case_info): try: p = Problem() if (cls.isLegalTitle(prob_title)): p.prob_title = prob_title else: raise Exception(cls.TITLE_ERRMSG) if (cls._isLegalDesc(prob_desc)): p.prob_desc = prob_desc else: raise Exception(cls.DESC_ERRMSG) p.prob_priv = prob_priv p.prob_title = prob_title p.prob_time = prob_time p.prob_memory = prob_memory p.prob_codelength = prob_codelength p.is_spj = is_spj p.author = User.getById(uid) p.data_count = data_count p.course = Course.getById(course_id) p.case_info = case_info p.save() return p except Exception as e: raise e
def deleteProblem(self): try: self.prob_priv = 0 self.author = User.getById(1) #modified self.course = Course.getById(9) # a course to store the deleted problems self.save() return self.pid except Exception as e: raise e
def deleteProblem(self): try: self.prob_priv = 0 self.author = User.getById(1) #modified self.course = Course.getById( 9) # a course to store the deleted problems self.save() return self.pid except Exception as e: raise e
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)
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)