def Contestadd(req): id = 1 count = int(db.Fetchone('SELECT COUNT(*) FROM contests')[0]) if count > 0: id = int(db.Fetchone('SELECT MAX(id) FROM contests')[0]) + 1 req_problems = req['problems'].split(',') problems = [] for problem in req_problems: if db.Read_Problem(problem) == None: flash(r'题目#%s不存在' % problem, 'error') return modules.Page_Back() problems.append({'id': int(problem)}) try: begin_time = datetime.datetime.strptime(req['begin_time'], date_format) end_time = datetime.datetime.strptime(req['end_time'], date_format) except: flash(r'日期格式不对. 应为 yyyy-mm-dd HH:MM:SS', 'error') return modules.Page_Back() db.Execute('INSERT INTO contests VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)', (id, req['title'], req['subtitle'], req['begin_time'], req['end_time'], req['description'], '{"type":"oi-intoj"}', request.cookies['username'], json.dumps(problems), '')) flash('添加成功', 'ok') return redirect('/contest/%d' % id)
def Submit(req): is_public = 1 if req.get('is_public') != None else 0 id = req['id'] if req['id'] != '': if int(req['id']) <= 0: flash(r'编号必须为正', 'error') return render_template("problemadd.html") count = int( db.Fetchone('SELECT COUNT(*) FROM problems WHERE id=%s', req['id'])['COUNT(*)']) if count > 0: flash(r'题目编号 %s 已经有过了.', 'error') return render_template("problemadd.html") db.Execute("INSERT INTO problems VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",(req['id'],req['title'],req['description'],req['input_format'],req['output_format'],\ req['example'],req['limit_and_hint'],req['time_limit'],req['memory_limit'],is_public)) else: id = 1 count = int(db.Fetchone('SELECT COUNT(*) FROM problems')['COUNT(*)']) if count > 0: id = int( db.Fetchone('SELECT MAX(id) FROM problems')['MAX(id)']) + 1 db.Execute("INSERT INTO problems VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",(id,req['title'],req['description'],req['input_format'],req['output_format'],\ req['example'],req['limit_and_hint'],req['time_limit'],req['memory_limit'],is_public)) return redirect('/problem/%s' % id)
def Submit(problemid,req,contest_id=0): if not modules.Is_Loggedin(): flash('请先登录','error') return modules.Page_Back() code = req['code'] if len(code) < 10: flash('这么短真的没问题?','error') return modules.Page_Back() username = request.cookies['username'] last_submit_time = modules.Get_Session('last_submit_time',username) now_time = datetime.datetime.now() if last_submit_time != None: seconds = (now_time-last_submit_time).seconds if seconds < submit_delay_time: flash('请在 %ss 后提交'%(submit_delay_time-seconds),'error') return modules.Page_Back() modules.Set_Session('last_submit_time',username,now_time) maxid_result = db.Fetchone("SELECT MAX(id) FROM records;")[0] runid = 1 if maxid_result == None else int(maxid_result) + 1 nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') db.Execute("INSERT INTO records VALUES(%s,%s,%s,%s,0,0,'','{\"subtasks\":[]}',0,0,'',%s,%s,%s);",(runid,problemid,code,'cpp',username,contest_id,nowtime)) r=redis.Redis(host='localhost',port=6379,decode_responses=True) r.rpush('intoj-waiting',str(runid)) return redirect('/record/%d'%runid)
def Rejudge(record_id): if db.Fetchone("SELECT * FROM records WHERE id=%s",record_id) == None: flash(r'提交记录R%d没找着!\\\n可能是因为编号不对.'%record_id,'error') return redirect('/status') db.Execute("UPDATE records SET status=0,score=0,time_usage=0,memory_usage=0,result='{\"subtasks\":[]}',compilation='' WHERE id=%s",record_id) myredis.Rpush('intoj-waiting',str(record_id)) flash('成功重测.','ok') return redirect('/record/%d'%record_id)
def Rejudge(record_id): if db.Fetchone("SELECT * FROM records WHERE id=%s",record_id) == None: flash(r'提交记录R%d没找着!\\\n可能是因为编号不对.'%record_id,'error') return redirect('/status') db.Execute("UPDATE records SET status=0,score=0,result='{\"subtasks\":[]}',compilation='' WHERE id=%s",record_id) r=redis.Redis(host='localhost',port=6379,decode_responses=True) r.rpush('intoj-waiting',str(record_id)) flash('成功重测.','ok') return redirect('/record/%d'%record_id)
def Register(req): username,password,ensure_password,email = req['username'],req['password'],req['ensure_password'],req['email'] if username == '': return 0,'用户名不能为空' if password == '': return 0,'密码不能为空' if password != ensure_password: return 0,'两次输入的密码不同' if email == '': return 0,'邮箱地址不能为空' if not modules.Vaild_Username(username): return 0,'用户名只能包含大小写字母,数字,下划线和减号' count = int(db.Fetchone("SELECT COUNT(*) FROM users WHERE username=%s;",username)[0]) if count != 0: return 0,'用户名已被占用' password_sha256 = hashlib.sha256(password.encode('utf-8')).hexdigest() password_sha1 = hashlib.sha1(password.encode('utf-8')).hexdigest() db.Execute("INSERT INTO users(`username`,`password_sha256`,`password_sha1`,`email`,`nameplate`,`total_ac`,`total_submit`) VALUES(%s,%s,%s,%s,'',0,0);",(username,password_sha256,password_sha1,email)) return 1,''
def Change(origin_id, req): id = req['id'] if req['id'] == '': id = origin_id else: if int(req['id']) < 0: flash(r'编号不可以为负', 'error') return modules.Page_Back() count = int( db.Fetchone('SELECT COUNT(*) FROM problems WHERE id=%s', req['id'])[0]) if count > 0: flash(r'题目编号 %s 已经有过了.' % req['id'], 'error') modules.Page_Back() is_public = 1 if req.get('is_public') == 'on' else 0 db.Execute( "UPDATE problems SET `id`=%s,`title`=%s,`description`=%s,`input_format`=%s,`output_format`=%s,\ `example`=%s,`limit_and_hint`=%s,`time_limit`=%s,`memory_limit`=%s,`is_public`=%s\ WHERE id=%s;", (id, req['title'], req['description'], req['input_format'], req['output_format'], req['example'], req['limit_and_hint'], req['time_limit'], req['memory_limit'], is_public, origin_id)) return redirect('/problem/%s' % id)