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="没有权限!")
Beispiel #2
0
 def checktoken(token, request):
     """
     检测admin的token
     """
     if checkadmintoken(token, request):
         return packinfo(infostatus=1, infomsg="合法的token")
     else:
         return packinfo(infostatus=0, infomsg="不合法的token")
 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 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 changepassword(token, oldpassword, newpassword, request):
     """
     修改管理员密码
     """
     if checkadmintoken(token, request):
         temp = gdb.session.query(Admin).filter(
             Admin.admin_password == oldpassword).first()
         if temp:
             temp.admin_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 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 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 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 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="没有权限!")