修改metadata迁移至工具v3完成, 至此工具v3已全部完成
This commit is contained in:
parent
af1c27b4b4
commit
2983dc1308
|
|
@ -3,6 +3,7 @@ import pandas as pd
|
|||
import numpy as np
|
||||
import pyperclip
|
||||
import mysql.connector
|
||||
import tqdm
|
||||
|
||||
|
||||
BuildFullScheme = {
|
||||
|
|
@ -844,24 +845,31 @@ def generateUsageList(db,id):
|
|||
|
||||
def ImportMetadata(db,metadatafilepath): #metadata自动修改, 根据字段自适应修改, 参数为题库字典, 目标字典, 字段字典, metadata文本文件路径, 待确定是否替换的内容的存放路径
|
||||
fieldsdict = {"objs":"objappend",
|
||||
"obj":"objappend", # 别名
|
||||
"tags":"tagappend",
|
||||
"tag":"tagappend", #别名
|
||||
"ans":"overwrite",
|
||||
"solution":"overwrite",
|
||||
"usages":"usageappend",
|
||||
"usage":"usageappend",
|
||||
"same":"mutualappend",
|
||||
"related":"mutualappend",
|
||||
"unrelated":"mutualappend",
|
||||
"space":"overwrite",
|
||||
"remark":"remarkappend"
|
||||
"remark":"remarkappend",
|
||||
"remarks":"remarkappend"
|
||||
}
|
||||
mycursor = db.cursor()
|
||||
startdate = GetDate()
|
||||
starttime = GetTime()
|
||||
pendingusagelist = []
|
||||
mycursor.execute("SELECT ID FROM problems;")
|
||||
id_list_in_DB = [ret[0] for ret in mycursor.fetchall()]
|
||||
mycursor.execute("SELECT objid FROM lessonobj;")
|
||||
obj_list_in_DB = [ret[0] for ret in mycursor.fetchall()]
|
||||
data_to_modify = ObtainDatatoModify(metadatafilepath,fieldsdict,id_list_in_DB)
|
||||
print(data_to_modify)
|
||||
for item in data_to_modify:
|
||||
for item in tqdm.tqdm(data_to_modify):
|
||||
field = item[0]
|
||||
if field == "NotAField":
|
||||
print(f"项目 {item[1]} {item[2]} 字段名有误, 未对数据库作更改, 请检查!!!")
|
||||
|
|
@ -875,10 +883,29 @@ def ImportMetadata(db,metadatafilepath): #metadata自动修改, 根据字段自
|
|||
elif method == "mutualappend":
|
||||
mutualappendinDB(db,field,item[1],item[2],id_list_in_DB)
|
||||
elif method == "objappend":
|
||||
objappendinDB(db,item[1],item[2],obj_list_in_DB.append("KNONE"))
|
||||
|
||||
objappendinDB(db,item[1],item[2],obj_list_in_DB+["KNONE"])
|
||||
elif method == "tagappend":
|
||||
tagappendinDB(db,item[1],item[2])
|
||||
elif method == "remarkappend":
|
||||
remarkappendinDB(db,item[1],item[2])
|
||||
elif method == "usageappend":
|
||||
pending_list = usageappendinDB(db,item[1],item[2])
|
||||
pendingusagelist += pending_list.copy()
|
||||
for pending_item in pendingusagelist:
|
||||
print(f"新增记录: {pending_item[0]}")
|
||||
print("原有记录:")
|
||||
for u in pending_item[1]:
|
||||
print(u)
|
||||
insertflag = input("是否导入该数据?(Y/[N])").upper()
|
||||
if insertflag == "Y":
|
||||
forceusageappendinDB(db,pending_item[0])
|
||||
|
||||
return 0 # 已在数据库中修改, 之后需要将数据库写入一次, 返回1表示字段名有误, 返回其他表示成功进行了修改
|
||||
|
||||
sql = f"SELECT id FROM logs WHERE %s > {startdate} OR %s = {startdate} AND %s >= {starttime};"
|
||||
val = (GetDate(),GetDate(),GetTime())
|
||||
mycursor.execute(sql,val)
|
||||
changed_id_list = [item[0] for item in mycursor.fetchall()]
|
||||
return changed_id_list # 已在数据库中修改, 之后需要将数据库写入一次, 返回1表示字段名有误, 返回其他表示成功进行了修改
|
||||
|
||||
def overwriteinDB(mydb,field,id,content_string): #覆盖ans,solution,space字段的内容并在logs中记录
|
||||
mycursor = mydb.cursor()
|
||||
|
|
@ -932,6 +959,99 @@ def objappendinDB(mydb,id,content_string,objlist): #新增obj对应
|
|||
val = (GetDate(),GetTime(),get_git_username(),id,f"新增目标对应",f"{id}: {objid}")
|
||||
mycursor.execute(sql,val)
|
||||
|
||||
|
||||
|
||||
def tagappendinDB(mydb,id,content_string): #新增tag对应
|
||||
mycursor = mydb.cursor()
|
||||
content_list = [item.strip() for item in content_string.split("\n") if not item.strip() == ""]
|
||||
for tag in content_list:
|
||||
sql = f"SELECT ID, tagname FROM tagcorresp WHERE ID = %s AND tagname = %s;"
|
||||
val = (id,tag)
|
||||
mycursor.execute(sql,val)
|
||||
ret_list = mycursor.fetchall()
|
||||
if len(ret_list) == 0:
|
||||
sql = f"INSERT INTO tagcorresp SET ID = %s, tagname = %s;"
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO logs (DATE,TIME,username,ID,action,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
||||
val = (GetDate(),GetTime(),get_git_username(),id,f"新增标签",f"{id}: {tag}")
|
||||
mycursor.execute(sql,val)
|
||||
|
||||
def remarkappendinDB(mydb,id,content_string): #新增备注
|
||||
mycursor = mydb.cursor()
|
||||
content_list = [item.strip() for item in content_string.split("\n") if not item.strip() == ""]
|
||||
for line in content_list:
|
||||
date,remark = parseRemark(line)
|
||||
sql = f"SELECT ID,date,remark_content FROM remarks WHERE ID = %s AND date = %s AND remark_content LIKE %s;"
|
||||
val = (id,date,"%"+remark+"%")
|
||||
mycursor.execute(sql,val)
|
||||
ret_list = mycursor.fetchall()
|
||||
if len(ret_list) == 0 or len(remark) == 0: #如果原来的记录里remark为空也需要更新
|
||||
sql = f"INSERT INTO remarks SET ID = %s, date = %s, remark_content = %s;"
|
||||
val = (id,date,remark)
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO logs (DATE,TIME,username,ID,action,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
||||
val = (GetDate(),GetTime(),get_git_username(),id,f"新增备注",f"{id}: {remark}")
|
||||
mycursor.execute(sql,val)
|
||||
|
||||
def detectsimUsage(tuple1,tuple2): #对比两个使用记录的tuple是否非常相近
|
||||
id1,date1,classname1,diff1 = tuple1
|
||||
id2,date2,classname2,diff2 = tuple2
|
||||
if id1 == id2 and classname1 == classname2 and abs(datestrtotimestamp(date1)-datestrtotimestamp(date2)) < 7*86400 and usagelistdifference(diff1,diff2)<0.05:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def tostrlist(difflist):
|
||||
return "[" + ", ".join(['"'+f"{float(data):.3f}"+'"' for data in difflist]) + "]"
|
||||
|
||||
def forceusageappendinDB(mydb,rec): #强制新增使用记录
|
||||
mycursor = mydb.cursor()
|
||||
id = rec[0]
|
||||
date = rec[1]
|
||||
classname = rec[2]
|
||||
diff = rec[3]
|
||||
print(id,date,classname,diff)
|
||||
sql = f"INSERT INTO usages (ID,date,classname,diff) VALUE (%s,%s,%s,%s);"
|
||||
val = (id,date,classname,tostrlist(diff))
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO logs (DATE,TIME,username,ID,action,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
||||
val = (GetDate(),GetTime(),get_git_username(),id,f"强制新增使用记录",f"{id}\t{date}\t{classname}\t{tostrlist(diff)}")
|
||||
mycursor.execute(sql,val)
|
||||
|
||||
def usageappendinDB(mydb,id,content_string): #新增使用记录
|
||||
pending_list = []
|
||||
mycursor = mydb.cursor()
|
||||
content_list = [parseUsage(item) for item in content_string.split("\n") if not item.strip() == ""]
|
||||
for rec in content_list:
|
||||
date = rec["date"]
|
||||
classname = rec["classid"]
|
||||
diff = rec["difficulties"]
|
||||
sql = f"SELECT ID,date,classname,diff FROM usages WHERE ID = %s and classname = %s;"
|
||||
val = (id,classname)
|
||||
mycursor.execute(sql,val)
|
||||
oldusage_list = mycursor.fetchall()
|
||||
if len(oldusage_list) > 0:
|
||||
alike = False
|
||||
for usage_raw in oldusage_list:
|
||||
usage_raw = list(usage_raw)
|
||||
usage = usage_raw[:3]+[json.loads(usage_raw[3])]
|
||||
if detectsimUsage((id,date,classname,diff),usage):
|
||||
alike = True
|
||||
break
|
||||
if alike == False:
|
||||
pending_list.append(((id,date,classname,diff),oldusage_list.copy()))
|
||||
else:
|
||||
sql = f"INSERT INTO usages (ID,date,classname,diff) VALUE (%s,%s,%s,%s);"
|
||||
val = (id,date,classname,tostrlist(diff))
|
||||
mycursor.execute(sql,val)
|
||||
sql = "INSERT INTO logs (DATE,TIME,username,ID,action,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
||||
val = (GetDate(),GetTime(),get_git_username(),id,f"新增使用记录",f"{id}\t{date}\t{classname}\t{tostrlist(diff)}")
|
||||
mycursor.execute(sql,val)
|
||||
return pending_list
|
||||
|
||||
|
||||
|
||||
|
||||
def parseUsage(usagestring): #对单行usage信息进行分词
|
||||
usagedict = {}
|
||||
datalist = re.sub(r"\s+",r"\t",usagestring.strip()).split("\t")
|
||||
|
|
|
|||
|
|
@ -15,7 +15,38 @@ from database_tools_2 import *
|
|||
mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="tikuuser", pwd="Kjmathds_2024", db = "tikutest")
|
||||
|
||||
|
||||
ImportMetadata(mydb,metadatafilepath)
|
||||
changedids = ImportMetadata(mydb,metadatafilepath)
|
||||
|
||||
|
||||
if len(changedids) > 0:
|
||||
mycursor = mydb.cursor()
|
||||
configjson = BuildFullScheme
|
||||
latexbody = "\\begin{enumerate}\n\n"
|
||||
for id in sorted(list(set(changedids))):
|
||||
latexbody += generateLaTeXBodyContentfromMariaDB(mycursor,id,configjson) + "\n\n"
|
||||
latexbody += "\\end{enumerate}"
|
||||
latex_raw = ReadTextFile("模板文件/讲义模板.txt")
|
||||
if configjson["教师版"] == True:
|
||||
latex_raw = latex_raw.replace(r"学号\blank{50} \ 姓名\blank{80}","上海市控江中学")
|
||||
|
||||
if sys.platform != "win32": #非win系统用默认字体
|
||||
latex_raw = re.sub(r"fontset[\s]*=[\s]*none","fontset = fandol",latex_raw)
|
||||
latex_raw = re.sub(r"\\setCJKmainfont",r"% \\setCJKmainfont",latex_raw)
|
||||
latex_data = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",latex_raw,("试编译",latexbody)) #替换标题和bodystring
|
||||
outputdir = os.path.join(os.getcwd(),"临时文件")
|
||||
outputfilepath = os.path.join(outputdir,"试编译.tex")
|
||||
SaveTextFile(latex_data,outputfilepath)
|
||||
if XeLaTeXCompile(outputdir,"试编译.tex",times=1):
|
||||
print("修改后检验成功, 已导入数据库.")
|
||||
mydb.commit()
|
||||
else:
|
||||
print("修改后检验失败, 已回滚.")
|
||||
mydb.rollback()
|
||||
else:
|
||||
print("未作修改.")
|
||||
|
||||
|
||||
|
||||
|
||||
mydb.commit()
|
||||
mydb.close()
|
||||
|
|
|
|||
Reference in New Issue