diff --git a/工具v3/database_tools_2.py b/工具v3/database_tools_2.py index cb7fe7a0..823abfb3 100644 --- a/工具v3/database_tools_2.py +++ b/工具v3/database_tools_2.py @@ -399,7 +399,8 @@ def CreateNewProblem(id,content,origin,dict,editor): # 构建一道新题目的 NewProblem["edit"] = [editor] return NewProblem # 返回一道新题目的字典, 已赋新的ID, 内容, 来源和编辑者 - +def AddRelatedProblemToDB(id,content,oid,editor): + pass def AddProblemstoDict2024(startingid,raworigin,problems,editor,indexed): #将来自GenerateProblemListFromString的列表中的题目添加到thedict字典, 返回题号列表(包括用老题号替代的题目) mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="root", pwd="Wwy@0018705", db = "tikutest") @@ -477,23 +478,18 @@ def CreateIDLinks(old_id_list,new_id_list): #建立已有id和新id之间的联 return id_links # 返回id联系, 每个元组表示一对id, 前者是旧id, 后者是新id -def CreateRelatedProblems(links,thedict,filepath,editor): # 根据links关联生成待编辑的新题目字典, 等待编辑修改 - try: - new_dict = {} - for item in links: - old_id,new_id = item - new_dict[old_id] = thedict[old_id].copy() - new_dict[old_id]["id"] = new_id + "待替换" - new_dict[old_id]["content"] = new_dict[old_id]["content"] - new_dict[old_id]["usages"] = [] - new_dict[old_id]["same"] = [] - new_dict[old_id]["unrelated"] = [] - new_dict[old_id]["edit"] = new_dict[old_id]["edit"].copy() + [GetDate()+"\t"+editor] - new_dict[old_id]["origin"] = {"来源": "改编题目", "前序": old_id} - save_dict(new_dict,filepath) - except: - return 1 #异常返回1 - return 0 #正常返回0 +def CreateRelatedProblems(links): # 根据links关联生成待编辑的新题目字典, 等待编辑修改 + output = "\\begin{enumerate}\n" + mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="root", pwd="Wwy@0018705", db = "tikutest") + mycursor = mydb.cursor() + sql = "SELECT content FROM problems WHERE ID = %s;" + for id,rid in links: + val = (id,) + mycursor.execute(sql,val) + ret = mycursor.fetchone()[0] + output += f"\\item ({id}->{rid}) {ret}\n\n" + output = f"{output}\n\\end{{enumerate}}\n" + return output #正常返回0 def ImportRelatedProblems(new_json,main_json): # 导入编辑过的关联题目json文件到主数据库 pro_dict = load_dict(main_json) @@ -1000,11 +996,11 @@ def StudentsGetAfterContent(id,prodict,answered,spaceflag): #生成学生版讲 string += "\\vspace*{%s}\n\n"%prodict[id]["space"] return string #生成学生讲义后的答案及空格 -def XeLaTeXCompile(filedir,filename): #在filedir目录中用XeLaTeX编译filename文件两次(能编译出正确的引用和页码) +def XeLaTeXCompile(filedir,filename,times = 2): #在filedir目录中用XeLaTeX编译filename文件两次(能编译出正确的引用和页码) flagsuc = True - for i in range(2): + for i in range(times): if os.system("xelatex -interaction=batchmode -output-directory=%s %s"%(filedir,filename)) == 0: - print("第%d次编译成功."%(i+1)) + print(f"第 {i+1} 次编译 {filename} 成功.") else: flagsuc = False return flagsuc # 若第二次编译成功则返回True, 否则返回False diff --git a/工具v3/添加关联题目.py b/工具v3/添加关联题目.py index 83ac1aa5..5fc4ed23 100644 --- a/工具v3/添加关联题目.py +++ b/工具v3/添加关联题目.py @@ -17,34 +17,28 @@ class MyWindow(QWidget,Ui_Form): editor = self.lineEdit_editor.text().strip() # 修改人姓名 new_ids = NextSpareIDBlock(starting_id) links = CreateIDLinks(generate_number_set(old_ids),generate_number_set(new_ids)) - print(old_ids,new_ids,editor) + bodydata = CreateRelatedProblems(links) + tempfilepath = "临时文件/problem_edit.tex" + latextemplate = "模板文件/题目编辑.txt" + latexdata = ReadTextFile(latextemplate) + latexdata = StringSubstitute("<<待替换>>",latexdata,(bodydata,)) + SaveTextFile(latexdata,tempfilepath) + print("编辑完毕后, 保存关闭文件继续.") + os.system(f"code -w -g {tempfilepath}") #-w表示关闭窗口后继续下一步 + if XeLaTeXCompile("临时文件",tempfilepath,times=1): + problems = GenerateProblemListFromString2024(ReadTextFile(tempfilepath)) + for problem in problems: + id_and_content = problem[0] + oid = re.findall(r"^\((\d{6})->",id_and_content)[0] + id = re.findall(r"^\(\d{6}->(\d{6})\)",id_and_content)[0] + content = re.findall(r"->\d{6}\)([\S\s]*)$",id_and_content)[0].strip() + print(id,content) + AddRelatedProblemToDB(id,content,oid,editor) + print("编译成功, 已汇入题库") + else: + print("编译失败, 未汇入题库") - - # tempfilepath = "临时文件/problem_edit.json" - - # pro_dict = load_dict("../题库0.3/Problems.json") - # obj_dict = load_dict("../题库0.3/LessonObj.json") - # pro_dict_raw_string = ReadTextFile("../题库0.3/Problems.json") - # configjson = BuildFullScheme - - - CreateRelatedProblems(links,pro_dict,tempfilepath,editor) - - # print("编辑完毕后, 保存关闭文件继续.") - # os.system("code -w -g "+tempfilepath) #-w表示关闭窗口后继续下一步 - - # editedid_list = ImportRelatedProblems(tempfilepath,"../题库0.3/Problems.json") - - # pro_dict = load_dict("../题库0.3/Problems.json") - - # if not XeLaTeXTest(editedid_list,pro_dict,obj_dict,configjson,templatepath="模板文件/讲义模板.txt",outdir = "临时文件",outfile = "problems_test.tex"): - # SaveTextFile(pro_dict_raw_string,"../题库0.3/Problems.json") - # print("编译失败, 题库文件退回原状") - # else: - # print("编译成功, 已汇入题库") - # self.close() -