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 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="没有权限!")
Beispiel #3
0
 def checktoken(username, token):
     """
     检测admin的token
     """
     if checkusertoken(username, token):
         return packinfo(infostatus=1, infomsg="合法的token")
     else:
         return packinfo(infostatus=0, infomsg="不合法的token")
 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 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 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 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 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 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="没有权限!")
    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="没有权限!")