156 lines
6.6 KiB
Python
156 lines
6.6 KiB
Python
from flask import Flask, render_template, request, send_file
|
|
import os,uuid
|
|
from database_tools_2 import *
|
|
|
|
# outputpath = os.path.join(os.getcwd(),"临时文件")
|
|
# makedir(outputpath)
|
|
# raw_pro_dict = load_dict("../题库0.3/Problems.json")
|
|
# obj_dict = load_dict("../题库0.3/LessonObj.json")
|
|
# basicknowledge_dict = load_dict("../题库0.3/BasicKnowledge.json")
|
|
# jsonpath = "../备课组"
|
|
# jsondicts = {}
|
|
# for loc,dirs,files in os.walk(jsonpath):
|
|
# if "校本材料.json" in files:
|
|
# currentdict = load_dict(os.path.join(loc,"校本材料.json"))
|
|
# for key in currentdict["notes"]:
|
|
# jsondicts[key] = currentdict["notes"][key].copy()
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
def index():
|
|
if request.method == 'POST':
|
|
global pid,configjson,uuiddir,outputpath
|
|
uuiddir = str(uuid.uuid4())
|
|
outputpath = os.path.join(os.getcwd(),f"临时文件/{uuiddir}")
|
|
makedir(outputpath)
|
|
pid = request.form['pid'].strip().upper()
|
|
pid = RefinePunctuations(pid).replace(" ","")
|
|
version = request.form['version']
|
|
if version == "teacher":
|
|
configjson = {"教师版": True,
|
|
"字段显示设置": {
|
|
"题后空间": False,
|
|
"课时目标": True,
|
|
"题目标签": True,
|
|
"答案": True,
|
|
"解答与提示": True,
|
|
"使用记录": [-1,-1],
|
|
"来源": True,
|
|
"备注": True,
|
|
"届别": []
|
|
},
|
|
"编译单个文件": True,
|
|
"编译合集": False
|
|
}
|
|
elif version == "student":
|
|
configjson = {"教师版": False,
|
|
"字段显示设置": {
|
|
"题后空间": True,
|
|
"课时目标": False,
|
|
"题目标签": False,
|
|
"答案": False,
|
|
"解答与提示": False,
|
|
"使用记录": [-2,-1],
|
|
"来源": False,
|
|
"备注": False,
|
|
"届别": []
|
|
},
|
|
"编译单个文件": True,
|
|
"编译合集": False
|
|
}
|
|
elif version == "withanswer":
|
|
configjson = {"教师版": False,
|
|
"字段显示设置": {
|
|
"题后空间": False,
|
|
"课时目标": False,
|
|
"题目标签": False,
|
|
"答案": True,
|
|
"解答与提示": False,
|
|
"使用记录": [-2,-1],
|
|
"来源": False,
|
|
"备注": False,
|
|
"届别": []
|
|
},
|
|
"编译单个文件": True,
|
|
"编译合集": False
|
|
}
|
|
global filename
|
|
mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="tikuuser", pwd="Kjmathds_2024", db = "tiku")
|
|
mycursor = mydb.cursor()
|
|
notes_dict = load_notes_dict_from_mariadb(mycursor)
|
|
structure_dict = load_structures_dict_from_mariadb(mycursor)
|
|
if re.findall(r"[A-Za-z]",pid) != []:
|
|
if pid in notes_dict:
|
|
try:
|
|
filename = pid+notes_dict[pid]["filename"].replace("(","").replace(")","")+".tex"
|
|
papertype = pid[0:5]
|
|
# grade = pid[3:5]
|
|
# raw_pro_dict = load_dict("../题库0.3/Problems.json")
|
|
# obj_dict = load_dict("../题库0.3/LessonObj.json")
|
|
# basicknowledge_dict = load_dict("../题库0.3/BasicKnowledge.json")
|
|
consecutivenumbering = structure_dict[papertype]["consecutivenumbering"]
|
|
texdata = GenerateSingleLessonNotefromMariaDB(mycursor,pid,structure_dict,notes_dict,"./模板文件/讲义模板.txt",os.path.join(outputpath,filename),configjson,consecutivenumbering) #20240415版讲义生成
|
|
mydb.close()
|
|
return f'''
|
|
<h1>{pid} PDF 文件生成完毕</h1>
|
|
<a href="/downloadpdf">下载生成的PDF文件</a>
|
|
<a href="/downloadtex">下载生成的TEX源文件</a>
|
|
'''
|
|
|
|
except:
|
|
mydb.close()
|
|
return "出现错误"
|
|
else:
|
|
mydb.close()
|
|
return f"{pid} 未被收录, 请退回重试."
|
|
else:
|
|
try:
|
|
filename = "题号选题.tex"
|
|
# raw_pro_dict = load_dict("../题库0.3/Problems.json")
|
|
# obj_dict = load_dict("../题库0.3/LessonObj.json")
|
|
# basicknowledge_dict = load_dict("../题库0.3/BasicKnowledge.json")
|
|
mycursor.execute("SELECT ID FROM problems WHERE NOT obsolete;")
|
|
id_list_in_DB = [ret[0] for ret in mycursor.fetchall()]
|
|
id_dict = {a:[] for a in id_list_in_DB}
|
|
problems_list = generate_number_set(pid,id_dict)
|
|
if len(problems_list) <= 100:
|
|
bodystring = "\\begin{enumerate}\n\n"
|
|
for singleid in problems_list:
|
|
bodystring += generateLaTeXBodyContentfromMariaDB(mycursor,singleid,configjson) + "\n"
|
|
bodystring += "\\end{enumerate}\n\n"
|
|
templatepath = "./模板文件/讲义模板.txt"
|
|
outputfilepath = os.path.join(outputpath,filename)
|
|
texdata = ReadTextFile(templatepath)
|
|
texdata = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",texdata,("选题编译",bodystring))
|
|
SaveTextFile(texdata,outputfilepath)
|
|
XeLaTeXCompile(outputpath,filename)
|
|
mydb.close()
|
|
return f'''
|
|
<h1>{pid} PDF 文件生成完毕</h1>
|
|
<a href="/downloadpdf">下载生成的PDF文件</a>
|
|
<a href="/downloadtex">下载生成的TEX源文件</a>
|
|
'''
|
|
else:
|
|
mydb.close()
|
|
return f'''<h1>题号列表中的题目数量超过100, 请寻求备课组题库负责同志的帮助</h1>'''
|
|
except:
|
|
mydb.close()
|
|
return "出现错误"
|
|
|
|
|
|
return render_template('get_id.html')
|
|
|
|
@app.route('/downloadpdf')
|
|
def downloadpdf():
|
|
return send_file(os.path.join(outputpath,filename[:-4]+".pdf"), as_attachment=True)
|
|
|
|
@app.route('/downloadtex')
|
|
def downloadtex():
|
|
return send_file(os.path.join(outputpath,filename), as_attachment=True)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host = "0.0.0.0", port = 25432) |