def getwrongproblemidlist(token, req): """ 获取用户错题列表 """ if checkusertoken(token, req): us = gdb.session.query(User).filter( User.user_token == token ).first() if us: usid = us.user_id wronglist = gdb.session.query(NoteBook).filter( NoteBook.user_id == usid ).all() widlist = [{"notebookid": x.notebook_id, "problemid": x.problem_id} for x in wronglist] # 检查作弊 temp = gdb.session.query(Score).filter( Score.user_id == usid).order_by(Score.score_id.desc()).first() if temp.score_timeend.strip() == "": return packinfo(infostatus=-1, infomsg="正在答题", inforesult=temp.score_id) return packinfo(infostatus=1, inforesult=widlist, infomsg="查询成功!") else: return packinfo(infostatus=0, infomsg="用户不存在!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def removeproblemsets(token, problemsetname, req): """ 移除题库 """ if checkadmintoken(token, req): temp = gdb.session.query(ProblemSet).filter( ProblemSet.problemset_title == problemsetname ).first() if temp: pid = temp.problemset_id probs = gdb.session.query(Problem).filter( Problem.problemset_id == pid ).all() for pb in probs: gdb.session.delete(pb) scores = gdb.session.query(Score).filter( Score.problemset_id == pid ).all() for sc in scores: gdb.session.delete(sc) notebooks = gdb.session.query(NoteBook).filter( NoteBook.problemset_id == pid ).all() for nb in notebooks: gdb.session.delete(nb) gdb.session.delete(temp) try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!") else: return packinfo(infostatus=1, infomsg="题库删除成功!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def addwrongproblem(token, problemsetid, problemid, req): """ 添加错题 """ if checkusertoken(token, req): us = gdb.session.query(User).filter( User.user_token == token ).first() temp = gdb.session.query(NoteBook).filter(and_( NoteBook.problem_id == problemid, NoteBook.user_id == us.user_id )).first() if not temp: usid = us.user_id nb = NoteBook(usid, problemsetid, problemid) gdb.session.add(nb) try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!") else: return packinfo(infostatus=1, infomsg="成功添加错题!") else: return packinfo(infostatus=2, infomsg="已添加的题目!") else: return packinfo(infostatus=3, infomsg="没有权限!")
def login(username, password, req): """ 管理员登录 """ aim = gdb.session.query(Admin).filter( and_(Admin.admin_username == username, Admin.admin_password == password)).first() if aim: # 登录成功 usertoken = gettoken() userip = getip(req) userlogintime = gettimestr() aim.admin_token = usertoken aim.admin_ip = userip aim.admin_logintime = userlogintime try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!请联系系统管理员!") else: return packinfo(infostatus=1, infomsg="登录成功!", inforesult={"usertoken": usertoken}) else: return packinfo(infostatus=0, infomsg="用户名或密码错误!")
def getproblemwithanswerbyid(token, problemid, req): """ 获取题目带答案 """ if checkalltoken(token, req): print(problemid) tnb = gdb.session.query(Settings).filter( Settings.settings_key == "togglenotebook" ).first() if tnb.settings_value == "0": return packinfo(infostatus=3, infomsg="管理员已关闭错题本功能!") temp = gdb.session.query(Problem).filter( Problem.problem_id == problemid ).first() us = gdb.session.query(User).filter( User.user_token == token ).first() if temp: tempdict = temp.todict() answers = list(tempdict["problem_answer"]) answers.sort() answers = "".join(answers) tempdict["problem_answer"] = answers tempscore = gdb.session.query(Score).filter( Score.user_id == us.user_id).order_by(Score.score_id.desc()).first() if len(tempscore.score_timeend.strip()) == 0: return packinfo(infostatus=-1, infomsg="答题中,无法查看错题!") return packinfo(infostatus=1, inforesult=tempdict) else: return packinfo(infostatus=0, infomsg="没有该题目!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def checktoken(username, token): """ 检测admin的token """ if checkusertoken(username, token): return packinfo(infostatus=1, infomsg="合法的token") else: return packinfo(infostatus=0, infomsg="不合法的token")
def checktoken(token, request): """ 检测admin的token """ if checkadmintoken(token, request): return packinfo(infostatus=1, infomsg="合法的token") else: return packinfo(infostatus=0, infomsg="不合法的token")
def getranklistbypsetid(token, psetid, req): """ 根据题库编号,查询排名 """ if checkalltoken(token, req): scorelist = ProblemsetDAO.getscorelistbypsetid(psetid) return packinfo(infostatus=1, infomsg="查询成功!", inforesult=scorelist) else: return packinfo(infostatus=2, infomsg="没有权限!")
def getnotebooktoggle(token, req): """ 获取用户错题本开闭状态 """ if checkadmintoken(token, req): setting = gdb.session.query(Settings).filter( Settings.settings_key == "togglenotebook" ).first() return packinfo(infostatus=1, infomsg="查询成功!", inforesult=setting.settings_value) else: return packinfo(infostatus=2, infomsg="没有权限!")
def checkexist(token, problemsetname, req): """ 检查题库是否存在 """ if checkalltoken(token, req): temp = gdb.session.query(ProblemSet).filter( ProblemSet.problemset_title == problemsetname).first() if temp: return packinfo(infostatus=1, infomsg="题库已存在!") else: return packinfo(infostatus=0, infomsg="没有此题库!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def getpsetidbypsetname(token, psetname, req): """ 根据题库名称,查题库编号 """ if checkalltoken(token, req): pset = gdb.session.query(ProblemSet).filter( ProblemSet.problemset_title == psetname ).first() if pset: return packinfo(infostatus=1, infomsg="查询成功!", inforesult=pset.problemset_id) else: return packinfo(infostatus=3, infomsg="没有此题库!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def getproblemsets(token, req): """ 返回所有题库 """ if checkalltoken(token, req): try: problemsets = gdb.session.query(ProblemSet).all() except Exception as e: return packinfo(infostatus=0, infomsg="数据库错误!") else: problemlist = [x.todict() for x in problemsets] return packinfo(infostatus=1, inforesult=problemlist) else: return packinfo(infostatus=2, infomsg="没有权限!")
def addfinishedtime(token, scoreid, req): """ 记录结束时间戳 """ if checkusertoken(token, req): temp = gdb.session.query(Score).filter( Score.score_id == scoreid).first() temp.score_timeend = gettimestr() try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!") else: return packinfo(infostatus=1, infomsg="时间更新成功!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def togglenotebookopen(token, req): """ 打开用户错题本 """ if checkadmintoken(token, req): setting = gdb.session.query(Settings).filter( Settings.settings_key == "togglenotebook" ).first() if setting.settings_value == "0": setting.settings_value = "1" try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=4, infomsg="数据库错误!") else: return packinfo(infostatus=1, infomsg="切换成功!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def getsummarywithanswerbyid(token, problemid, req): """ 获取易错题带答案 """ if checkalltoken(token, req): print(problemid) temp = gdb.session.query(Problem).filter( Problem.problem_id == problemid ).first() if temp: tempdict = temp.todict() answers = list(tempdict["problem_answer"]) answers.sort() answers = "".join(answers) tempdict["problem_answer"] = answers return packinfo(infostatus=1, inforesult=tempdict) else: return packinfo(infostatus=0, infomsg="没有该题目!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def changepassword(token, oldpassword, newpassword, req): """ 修改用户密码 """ if checkusertoken(token, req): temp = gdb.session.query(User).filter( User.user_password == oldpassword).first() if temp: temp.user_password = newpassword try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!") else: return packinfo(infostatus=1, infomsg="密码修改成功!") else: return packinfo(infostatus=3, infomsg="原密码错误!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def logout(token, username, req): """ 管理员退出 """ aim = gdb.session.query(Admin).filter( and_(Admin.admin_username == username, Admin.admin_token == token, Admin.admin_ip == getip(req))).first() if aim: # 退出系统 aim.admin_token = "" aim.admin_ip = "" aim.admin_logintime = "" try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!请联系系统管理员!") else: return packinfo(infostatus=1, infomsg="成功退出系统!") else: return packinfo(infostatus=0, infomsg="异常请求!请及时修改密码!")
def removewrongproblem(token, notebookid, req): """ 移除错题 """ if checkusertoken(token, req): temp = gdb.session.query(NoteBook).filter( NoteBook.notebook_id == notebookid ).first() if temp: gdb.session.delete(temp) try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!") else: return packinfo(infostatus=1, infomsg="移除成功!") else: return packinfo(infostatus=2, infomsg="题目不存在!") else: return packinfo(infostatus=3, infomsg="没有权限!")
def getsummarylist(token, summary_psetname, summary_pwrongpercent, req): """ 获取易错题列表 """ if checkadmintoken(token, req): print(token) print(summary_psetname) print(summary_pwrongpercent) # 查询的同时返回计数 summarylist = gdb.session.query(NoteBook.problem_id, func.count(NoteBook.problem_id)).filter( NoteBook.problemset_id == gdb.session.query(ProblemSet).filter( ProblemSet.problemset_title == summary_psetname).first().problemset_id ).group_by(NoteBook.problem_id).all() summarylist = [x for x in summarylist if x[1] >= int(summary_pwrongpercent)] summarylist.sort(key=lambda x: x[1], reverse=True) summarylist = [{"problem_id": x[0], "problem_count": x[1]} for x in summarylist] return packinfo(infostatus=1, inforesult=summarylist, infomsg="查询成功!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def setanswertime(token, problemtitle, answertime, req): """ 设置答题时间 """ if checkadmintoken(token, req): temp = gdb.session.query(ProblemSet).filter( ProblemSet.problemset_title == problemtitle ).first() if temp: temp.problemset_timeperproblem = answertime try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!") else: return packinfo(infostatus=1, infomsg="答题时间更新成功!") else: return packinfo(infostatus=3, infomsg="不存在的题库!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def addscore(token, scoreid, right, wrong, md5str, timestamp, req): """ 记录成绩 """ if checkusertoken(token, req): print(timestamp) us = gdb.session.query(User).filter( User.user_token == token ).first() if us.user_dotimestamp: oldtime = int(us.user_dotimestamp) newtime = int(timestamp) if abs(newtime - oldtime) < 2000: print("频繁请求") return packinfo(infostatus=-1, infomsg="请不要频繁操作!") us.user_dotimestamp = timestamp try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=6, infomsg="数据库错误!") mstr = getmd5(getmd5(token + right + wrong) + scoreid) print(mstr) if mstr == md5str: if right == "1" and wrong == "0": temp = gdb.session.query(Score).filter( Score.score_id == scoreid).first() temp.score_right = temp.score_right + 1 try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=4, infomsg="数据库错误!") elif right == "0" and wrong == "1": temp = gdb.session.query(Score).filter( Score.score_id == scoreid).first() temp.score_wrong = temp.score_wrong + 1 try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=5, infomsg="数据库错误!") else: return packinfo(infostatus=3, infomsg="请求异常!请重新登录答题!") return packinfo(infostatus=1, infomsg="成绩已更新!") else: return packinfo(infostatus=0, infomsg="请求异常!请重新登录答题!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def getscores(token, req): """ 获取用户积分榜 """ if checkusertoken(token, req): us = gdb.session.query(User).filter( User.user_token == token ).first() usid = us.user_id psetlist = ProblemsetDAO.getpsetlistbyuserid(usid) resultlist = [] for pid in psetlist: print(pid) scores = ProblemsetDAO.getscorelistbypsetid(pid) print(scores) uaim = [x for x in scores if x["user_id"] == usid] if len(uaim) > 0: resultlist.append(uaim[0]) else: resultlist = [] return packinfo(infostatus=1, infomsg="查询成功!", inforesult=resultlist) else: return packinfo(infostatus=2, infomsg="没有权限!")
def uploadfile(token, file, req): """ 添加题库文件 """ if checkadmintoken(token, req): if file and allowed_file(file.filename): try: filename = conf.tempfilename if not os.path.exists(os.path.join(os.getcwd(), conf.importpath, token)): os.mkdir(os.path.join( os.getcwd(), conf.importpath, token)) fileurl = os.path.join( os.getcwd(), conf.importpath, token, filename) file.save(fileurl) except Exception as e: print(e) return packinfo(infostatus=0, infomsg="目录无效,上传失败!请联系系统管理员!") else: return packinfo(infostatus=1, infomsg="上传成功!", inforesult=fileurl) else: return packinfo(infostatus=2, infomsg="上传失败!文件格式被禁止!") else: return packinfo(infostatus=3, infomsg="没有权限!")
def copyproblemset(token, psettitle, newsettitle, req): """ 复制题库,可用于组织考试时清空积分 """ if checkalltoken(token, req): pset = gdb.session.query(ProblemSet).filter( ProblemSet.problemset_title == psettitle ).first() if pset: npset = ProblemSet(newsettitle, pset.problemset_desp, pset.problemset_count, pset.problemset_token, pset.problemset_answercount, pset.problemset_timeperproblem) try: gdb.session.add(npset) gdb.session.commit() except Exception as e: return packinfo(infostatus=4, infomsg="数据库错误!") else: newid = gdb.session.query(ProblemSet).filter( ProblemSet.problemset_title == newsettitle ).first().problemset_id plist = gdb.session.query(Problem).filter( Problem.problemset_id == pset.problemset_id ).all() for p in plist: np = Problem(newid, p.problem_desp, p.problem_picpath, p.problem_choiceA, p.problem_choiceB, p.problem_choiceC, p.problem_choiceD, p.problem_answer) gdb.session.add(np) try: gdb.session.commit() except Exception as e: return packinfo(infostatus=5, infomsg="数据库错误!") else: return packinfo(infostatus=1, infomsg="复制成功!") else: return packinfo(infostatus=3, infomsg="没有此题库!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def checkuseragent(data): """ 检测浏览器 """ if data: useragent = data["useragent"] print(useragent) if "mobile" in useragent: return packinfo(infostatus=2, infomsg="此系统不支持移动端浏览器!请切换至计算机上使用。") elif "firefox" in useragent: return packinfo(infostatus=1, infomsg="支持的浏览器") elif "edge" in useragent: return packinfo(infostatus=1, infomsg="支持的浏览器") elif "chrome" in useragent: return packinfo(infostatus=1, infomsg="支持的浏览器") else: return packinfo( infostatus=3, infomsg="此系统不支持您的浏览器!请使用Edge、Firefox或Chrome浏览器。") else: return packinfo(infostatus=-1, infomsg="系统异常!请重新打开!")
def addproblemset(token, protitle, prodesp, req): """ 添加题库 """ print(token) if checkadmintoken(token, req): serverfilepath = os.path.join(os.getcwd(), conf.importpath, token, conf.tempfilename) print(serverfilepath) print(protitle) print(prodesp) file_zip = zipfile.ZipFile(serverfilepath, "r") tempdir = os.path.join(os.getcwd(), conf.importpath, token) for f in file_zip.namelist(): print(tempdir) print(f) file_zip.extract(f, tempdir) file_zip.close() # os.remove(serverfilepath) xlsxfile = get_data_xlsx(os.path.join(os.getcwd(), tempdir, conf.tempdirname, conf.dataxlsxname))["题目"][1:] print(xlsxfile) psetcount = len(xlsxfile) protoken = gettoken() if len(prodesp) > 0: prset = ProblemSet(protitle, prodesp, psetcount, protoken, 0, 0) gdb.session.add(prset) gdb.session.commit() pro = gdb.session.query(ProblemSet).filter( ProblemSet.problemset_title == protitle).first() proid = pro.problemset_id if len(prodesp) > 0: actualcount = 0 else: actualcount = pro.problemset_count for dt in xlsxfile: problem_desp = dt[0] problem_choiceA = dt[1] problem_choiceB = dt[2] problem_choiceC = dt[3] problem_choiceD = dt[4] problem_answer = dt[5] problem_picname = "" print(os.getcwd()) if len(dt) > 6: newpicname = gettoken() + "." + dt[6].rsplit(".", 1)[-1] shutil.move(os.path.join(os.getcwd(), tempdir, conf.tempdirname, conf.datapicdir, dt[6]), os.path.join(os.getcwd(), conf.problempicdir, newpicname)) problem_picname = newpicname drawwaterprint(os.path.join(os.getcwd(), conf.problempicdir, newpicname)) """ print(problem_desp) print(problem_choiceA) print(problem_choiceB) print(problem_choiceC) print(problem_choiceD) print(problem_answer) print(problem_picname) print(psetcount) """ temp = gdb.session.query(Problem).filter(and_( Problem.problemset_id == proid, Problem.problem_desp == problem_desp, Problem.problem_choiceA == problem_choiceA, Problem.problem_choiceB == problem_choiceB, Problem.problem_choiceC == problem_choiceC, Problem.problem_choiceD == problem_choiceD, Problem.problem_answer == problem_answer )).first() if temp: print("已存在的题目") continue else: pro = Problem(proid, problem_desp, problem_picname, problem_choiceA, problem_choiceB, problem_choiceC, problem_choiceD, problem_answer) gdb.session.add(pro) actualcount = actualcount + 1 shutil.rmtree(tempdir) try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!题目添加失败!") else: pro = gdb.session.query(ProblemSet).filter( ProblemSet.problemset_title == protitle).first() pro.problemset_count = actualcount try: gdb.session.commit() except Exception as e: print(e) else: return packinfo(infostatus=1, infomsg="题目添加成功!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def getproblembyid(token, problemid, req): """ 获取题目 """ if checkusertoken(token, req): print(problemid) temp = gdb.session.query(Problem).filter( Problem.problem_id == problemid ).first() tempset = gdb.session.query(ProblemSet).filter( ProblemSet.problemset_id == temp.problemset_id ).first() tempuser = gdb.session.query(User).filter( User.user_token == token ).first() tempscore = gdb.session.query(Score).filter( Score.user_id == tempuser.user_id).order_by(Score.score_id.desc()).first() if len(tempscore.score_timeend.strip()) != 0: return packinfo(infostatus=-1, infomsg="答题被终止!") if temp: tempdict = temp.todict() answers = list(tempdict["problem_answer"]) answers.sort() answers = "".join(answers) choicelist = [["A", tempdict["problem_choiceA"]], ["B", tempdict["problem_choiceB"]], ["C", tempdict["problem_choiceC"]], ["D", tempdict["problem_choiceD"]]] answerlist = list(answers) print(choicelist) print(answerlist) for cho in choicelist: for ans in answerlist: if ans == cho[0]: cho.append("$") break else: cho.append("#") random.shuffle(choicelist) choicelist[0][0] = "A" choicelist[1][0] = "B" choicelist[2][0] = "C" choicelist[3][0] = "D" tempdict["problem_choiceA"] = choicelist[0][1] tempdict["problem_choiceB"] = choicelist[1][1] tempdict["problem_choiceC"] = choicelist[2][1] tempdict["problem_choiceD"] = choicelist[3][1] newanswer = "" for ch in choicelist: if ch[-1] == "$": newanswer = newanswer + ch[0] print(choicelist) print(newanswer) answers = newanswer tempdict["problem_answer"] = getmd5(getmd5(str(answers) + str(tempdict["problem_id"]))+str(answers)) tempdict["problemset_timeperproblem"] = tempset.problemset_timeperproblem problemseat = tempuser.user_problemseat problemstream = tempuser.user_problemstream print("pseat:", problemseat) print("pstream:", problemstream) streamlist = problemstream.split("#") if streamlist.index(str(problemseat)) < len(streamlist) - 1: nextpid = streamlist[streamlist.index( str(problemseat)) + 1] tempuser.user_problemseat = nextpid try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=3, infomsg="数据库错误!") else: nextpid = -1 tempdict["problem_nextpid"] = nextpid return packinfo(infostatus=1, inforesult=tempdict) else: return packinfo(infostatus=0, infomsg="没有该题目!") else: return packinfo(infostatus=2, infomsg="没有权限!")
def initproblems(token, problemtitle, req): """ 初始化答题 """ if checkusertoken(token, req): temp = gdb.session.query(ProblemSet, Problem).filter(and_( ProblemSet.problemset_title == problemtitle, Problem.problemset_id == ProblemSet.problemset_id )).all() if temp: problemids = [x[1].problem_id for x in temp] print(problemids) problemanswercount = temp[0][0].problemset_answercount print(problemanswercount) problemsetid = temp[0][0].problemset_id print(problemsetid) random.shuffle(problemids) aimproblems = problemids[:problemanswercount] print(aimproblems) aimproblems = [str(x) for x in aimproblems] userproblemstream = "#".join(aimproblems) print(userproblemstream) userproblemsetid = problemsetid userproblemseat = problemids[0] firstpid = userproblemseat user = gdb.session.query(User).filter( User.user_token == token ).first() if user: user.user_problemsetid = userproblemsetid user.user_problemstream = userproblemstream user.user_problemseat = userproblemseat try: gdb.session.commit() timestart = gettimestr() sco = Score(user.user_id, problemsetid, 0, 0, timestart, "", temp[0][0].problemset_answercount, temp[0][0].problemset_timeperproblem) gdb.session.add(sco) try: gdb.session.commit() except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!") else: scoretemp = gdb.session.query(Score).filter(and_( Score.user_id == user.user_id, Score.score_timestart == timestart )).first() scoreid = scoretemp.score_id except Exception as e: print(e) return packinfo(infostatus=0, infomsg="数据库错误!") else: return packinfo(infostatus=1, infomsg="初始化答题成功!", inforesult=[firstpid, scoreid]) else: return packinfo(infostatus=2, infomsg="用户不存在!") else: return packinfo(infostatus=3, infomsg="没有该题库!") else: return packinfo(infostatus=4, infomsg="没有权限!")