def release(): Log("用户进入发布页面了") if request.method == 'POST': if not session.get('QUESTION'): name = request.form['name'] test = [] for t in range(1, int(request.form['count']) + 1): test.append({ 'Context': request.form['testcontext%s' % t].strip(), 'Answer': request.form['testrst%s' % t].strip() }) #session['QUESTION'] = {'Context': request.form['context'], 'test': test} obj = {'Context': request.form['context'], 'Test': test} print(obj) dbmanager.logOn() jsontext = json.dumps(obj) filename = name + ".json" with open('./static/questions/' + filename, "w+", encoding='UTF-8') as file: file.write(jsontext) dbmanager.m_useTable('Questions') Log(name) Log(request.form['type']) Log(filename) dbmanager.m_insertItem( [name, '0', '0', request.form['type'], filename, '0']) return render_template('Release.html')
def writeQuestion(Qname): Log("进入写题界面,题目:%s" % Qname) dbmanager.logOn() dbmanager.m_useTable("Questions") Tpath = dbmanager.m_selectItem(['Tpath'], where="name='%s'" % Qname)[0] global jsonfile try: jsonfile = open("./static/questions/%s" % Tpath, encoding='UTF-8') except IOError: Log("没有这个文件:%s" % Tpath) file = json.load(jsonfile) print(file['Context']) return render_template("Answer.html", name=Qname, context=file['Context'])
def signup(): Log("进入signup界面") if request.method == 'POST': username = request.form['username'] psd = request.form['password'] Log("用户注册,username:%s, psd:%s" % (username, psd)) dbmanager.m_useTable("Customers") if dbmanager.m_itemExists(['username', 'psd'], where="username='******' AND psd='%s'" % (username, psd)): Log("用户已经存在") return render_template("signup.html", signuped=False) session['LOGINED'] = True dbmanager.m_insertItem([username, psd, 'NONE', 0, 0, 0]) Log("增加用户了") return redirect(url_for("questions")) return render_template("signup.html", signuped=True)
def admin(): Log("进入管理员界面") dbmanager.m_useTable("Customers") cust_name = dbmanager.m_selectItem(['username', "psd"]) dbmanager.m_useTable("Questions") quest_name = dbmanager.m_selectItem(['name', 'checked']) return render_template("admin.html", questions=quest_name, customers=cust_name)
def checkanswer(name): Log("检查代码答案") Log(request.form['codeArea']) type = request.form['languages'] code = request.form['codeArea'] checkobj = CheckAnswer() dbmanager.m_useTable("Questions") Tpath = dbmanager.m_selectItem(['Tpath'], where="name='%s'" % name)[0][0] Log(Tpath) jsonfile = open("./static/questions/%s" % Tpath, "r+", encoding='UTF-8') obj = json.load(jsonfile) jsonfile.close() rst = checkobj.check(code, obj['Test'], type) Log("结果正确吗:%s" % rst) if rst: flash('check', True) else: flash('check', False) return redirect(url_for("writeQuestion", Qname=name))
def deleteQuestion(name): dbmanager.logOn() dbmanager.m_useTable("Questions") jsonfile = dbmanager.m_selectItem(["Tpath"], where="name='%s'" % name)[0][0] #print(jsonfile) #os.remove(url_for('static', filename="questions/%s" % jsonfile)) os.remove("./static/questions/%s" % jsonfile) dbmanager.m_deleteItem("name='%s'" % name) Log("删除了题目:%s" % name) return redirect(url_for('admin'))
def addedQuestion(): Log("用户对题目进行修改了") if not session.get('QUESTION'): session['QUESTION'] = { 'Context': request.form['context'], 'Text': [{ 'Context': request.form['test'], 'Answer': request.form['result'] }] } jsontext = json.dumps(session['QUESTION']) filename = request.form['name'] + ".json" with open('./static/questions/' + filename, "w+") as file: file.write(jsontext) dbmanager.m_useTable('Questions') dbmanager.m_insertItem( [request.form['name'], 0, 0, request.form['type'], filename, 0]) return redirect(url_for('release'))
def login(): Log("进入login界面") if request.method == 'POST': Log("recieved POST") Log("session['LOGINED']:%s" % session.get('LOGINED')) if not session.get('LOGINED'): session['LOGINED'] = False username = request.form['username'] psd = request.form['password'] Log("user logined\nusename:%s\npassword:%s" % (username, psd)) dbmanager.m_useTable('Customers') if not dbmanager.m_itemExists(['username, psd'], 'username="******" AND psd="%s"' % (username, psd)): Log("登录失败") return render_template('login.html', loginfailed=False) else: Log("登录成功") session['LOGINED'] = True return redirect(url_for('homepage')) return render_template('login.html')
def deleteCustom(name): dbmanager.logOn() dbmanager.m_useTable("Customers") dbmanager.m_deleteItem("username='******'" % name) Log("删除了用户:%s" % name) return redirect(url_for('admin'))
def homepage(): Log("进入主页") if not session.get('LOGINED'): session['LOGINED'] = False return render_template('index.html')
def competition(): Log("进入比赛界面") return render_template("TopicList.html")
def questions(): Log("进入题目列表") dbmanager.m_useTable("Questions") questions = dbmanager.m_selectItem(where="checked=1") return render_template("QuestionBank.html", questions=questions)
def release(): Log("进入Release页面") return render_template('Release.html')