优化"新增基础知识梳理"功能, 预导入和提交分离

This commit is contained in:
weiye.wang 2024-04-27 20:32:29 +08:00
parent bbd150c916
commit 2845233ea6
3 changed files with 102 additions and 48 deletions

View File

@ -25,23 +25,28 @@ class Ui_Form(object):
Form.resize(760, 490) Form.resize(760, 490)
self.label = QLabel(Form) self.label = QLabel(Form)
self.label.setObjectName(u"label") self.label.setObjectName(u"label")
self.label.setGeometry(QRect(200, 160, 91, 16)) self.label.setGeometry(QRect(120, 140, 91, 16))
self.lineEdit_filepath = QLineEdit(Form) self.lineEdit_filepath = QLineEdit(Form)
self.lineEdit_filepath.setObjectName(u"lineEdit_filepath") self.lineEdit_filepath.setObjectName(u"lineEdit_filepath")
self.lineEdit_filepath.setGeometry(QRect(200, 180, 381, 20)) self.lineEdit_filepath.setGeometry(QRect(120, 160, 471, 20))
self.lineEdit_filepath.setReadOnly(True) self.lineEdit_filepath.setReadOnly(True)
self.pushButton_selectfilepath = QPushButton(Form) self.pushButton_selectfilepath = QPushButton(Form)
self.pushButton_selectfilepath.setObjectName(u"pushButton_selectfilepath") self.pushButton_selectfilepath.setObjectName(u"pushButton_selectfilepath")
self.pushButton_selectfilepath.setGeometry(QRect(494, 150, 91, 24)) self.pushButton_selectfilepath.setGeometry(QRect(500, 130, 91, 24))
self.label_2 = QLabel(Form) self.label_2 = QLabel(Form)
self.label_2.setObjectName(u"label_2") self.label_2.setObjectName(u"label_2")
self.label_2.setGeometry(QRect(200, 210, 291, 51)) self.label_2.setGeometry(QRect(120, 190, 291, 51))
self.pushButton_exec = QPushButton(Form) self.pushButton_exec = QPushButton(Form)
self.pushButton_exec.setObjectName(u"pushButton_exec") self.pushButton_exec.setObjectName(u"pushButton_exec")
self.pushButton_exec.setGeometry(QRect(500, 210, 81, 51)) self.pushButton_exec.setGeometry(QRect(420, 190, 81, 51))
font = QFont() font = QFont()
font.setBold(True) font.setBold(True)
self.pushButton_exec.setFont(font) self.pushButton_exec.setFont(font)
self.pushButton_commit = QPushButton(Form)
self.pushButton_commit.setObjectName(u"pushButton_commit")
self.pushButton_commit.setGeometry(QRect(510, 190, 81, 51))
self.pushButton_commit.setFont(font)
self.pushButton_commit.setStyleSheet(u"background-color: rgb(255, 255, 127);")
self.retranslateUi(Form) self.retranslateUi(Form)
@ -55,6 +60,8 @@ class Ui_Form(object):
self.label_2.setText(QCoreApplication.translate("Form", u"LaTeX\u6587\u4ef6\u4e2d\u6709\u4e00\u4e2aenumerate\u73af\u5883.\n" self.label_2.setText(QCoreApplication.translate("Form", u"LaTeX\u6587\u4ef6\u4e2d\u6709\u4e00\u4e2aenumerate\u73af\u5883.\n"
"\u6bcf\u4e00\u4e2aitem\u540e\u9762\u6709\u4e00\u4e2a\u4e2d\u62ec\u53f7\u5305\u56f4\u7684\u4fe1\u606f, \u4e4b\u540e\u662f\u5185\u5bb9.\n" "\u6bcf\u4e00\u4e2aitem\u540e\u9762\u6709\u4e00\u4e2a\u4e2d\u62ec\u53f7\u5305\u56f4\u7684\u4fe1\u606f, \u4e4b\u540e\u662f\u5185\u5bb9.\n"
"\u4f8b\u5982: \\item [K0101/K0101001B,K0101002B] \u5185\u5bb9", None)) "\u4f8b\u5982: \\item [K0101/K0101001B,K0101002B] \u5185\u5bb9", None))
self.pushButton_exec.setText(QCoreApplication.translate("Form", u"\u6dfb\u52a0\u57fa\u7840\u77e5\u8bc6", None)) self.pushButton_exec.setText(QCoreApplication.translate("Form", u"\u9884\u5bfc\u5165\n"
"\u57fa\u7840\u77e5\u8bc6", None))
self.pushButton_commit.setText(QCoreApplication.translate("Form", u"\u63d0\u4ea4\u81f3\u6570\u636e\u5e93", None))
# retranslateUi # retranslateUi

View File

@ -14,49 +14,74 @@ class MyWindow_tjjc(QWidget,Ui_Form):
self.database_name = string self.database_name = string
# print(self.database_name) # print(self.database_name)
def bind(self): def bind(self):
self.errorflag = True
self.pushButton_exec.setDisabled(True) self.pushButton_exec.setDisabled(True)
self.pushButton_selectfilepath.clicked.connect(self.selectfilepath) self.pushButton_selectfilepath.clicked.connect(self.selectfilepath)
self.pushButton_exec.clicked.connect(self.exec) self.pushButton_exec.clicked.connect(self.execstep1)
self.pushButton_commit.clicked.connect(self.commitstep)
self.pushButton_commit.setDisabled(True)
self.mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name)
def selectfilepath(self): def selectfilepath(self):
self.filepath = QFileDialog.getOpenFileName(self,"选择.tex文件",os.getcwd(),"tex文件(*.tex);;所有文件(*)")[0] self.filepath = QFileDialog.getOpenFileName(self,"选择.tex文件",os.getcwd(),"tex文件(*.tex);;所有文件(*)")[0]
self.lineEdit_filepath.setText(self.filepath) self.lineEdit_filepath.setText(self.filepath)
self.pushButton_exec.setEnabled(True) self.pushButton_exec.setEnabled(True)
def exec(self): def execstep1(self):
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name) mycursor = self.mydb.cursor()
mycursor = mydb.cursor()
mycursor.execute("SELECT bn_id FROM basic_knowledges;") mycursor.execute("SELECT bn_id FROM basic_knowledges;")
bnids = [ret[0] for ret in mycursor.fetchall()] bnids = [ret[0] for ret in mycursor.fetchall()]
bkdata_raw = ReadTextFile(self.filepath) bkdata_raw = ReadTextFile(self.filepath)
bkdatabody = re.findall(r"\\begin\{enumerate\}([\s\S]*?)\\end\{enumerate\}",bkdata_raw)[0].strip().split("\\item") bkdatabody = re.findall(r"\\begin\{enumerate\}([\s\S]*?)\\end\{enumerate\}",bkdata_raw)[0].strip().split("\\item")
bkdata = [line.strip() for line in bkdatabody if "[" in line and "]" in line] bkdata = [line.strip() for line in bkdatabody if "[" in line and "]" in line]
print(f"目前数据库中基础知识编号的最大值: {int(max(bnids)[1:])}")
currentid = int(max(bnids)[1:])+1 currentid = int(max(bnids)[1:])+1
self.errorflag = False
try:
for line in bkdata:
for line in bkdata: if not line.strip() == "":
if not line.strip() == "": bnid = f"B{str(currentid).zfill(5)}"
bnid = f"B{str(currentid).zfill(5)}" pos = line.index("]")
pos = line.index("]") head = line[1:pos]
head = line[1:pos] content = line[pos+1:].strip()
content = line[pos+1:].strip() lesson,objs = head.split("/")
lesson,objs = head.split("/") obj_list = objs.split(",")
obj_list = objs.split(",") print(f"{bnid} {obj_list} {content}")
print(f"{bnid} {obj_list} {content}") sql = "INSERT INTO basic_knowledges (bn_id,bn_content) VALUE (%s,%s);"
sql = "INSERT INTO basic_knowledges (bn_id,bn_content) VALUE (%s,%s);" val = (bnid,content)
val = (bnid,content)
mycursor.execute(sql,val)
for objid in obj_list:
sql = "INSERT INTO bn_obj_corresp (bn_id,obj_id) VALUE (%s,%s);"
val = (bnid,objid)
mycursor.execute(sql,val) mycursor.execute(sql,val)
sql = "INSERT INTO logs (DATE,TIME,username,action,db_content) VALUE (%s,%s,%s,%s,%s);" print(f"已导入基础知识 编号: {bnid},\n 内容: {content}.")
val = (GetDate(),GetTime(),get_git_username(),"新增基础",f"{bnid} {obj_list} {content}") for objid in obj_list:
mycursor.execute(sql,val) sql = "SELECT * FROM lessonobj WHERE objid = %s;"
currentid += 1 val = (objid,)
mydb.commit() mycursor.execute(sql,val)
mydb.close() ret =mycursor.fetchall()
print("已录入完成") if ret == []:
print(f"!!!目标编号: {objid} 有误")
raise ValueError
else:
print(f"对应目标编号 {objid}, 目标内容 {ret[0][1]}")
sql = "INSERT INTO bn_obj_corresp (bn_id,obj_id) VALUE (%s,%s);"
val = (bnid,objid)
mycursor.execute(sql,val)
sql = "INSERT INTO logs (DATE,TIME,username,action,db_content) VALUE (%s,%s,%s,%s,%s);"
val = (GetDate(),GetTime(),get_git_username(),"新增基础",f"{bnid} {obj_list} {content}")
mycursor.execute(sql,val)
currentid += 1
print("---------- ---------- ----------")
self.pushButton_commit.setEnabled(True)
except Exception as e:
print(f"错误: {type(e).__name__}")
print(f"内容为 {line} 的输入有误.")
self.errorflag = True
self.mydb.rollback()
print(f"数据库 {self.database_name} 已回滚")
def commitstep(self):
if self.errorflag == False:
self.mydb.commit()
self.mydb.close()
print(f"提交至数据库 {self.database_name} 已完成.")
self.pushButton_commit.setDisabled(True)

View File

@ -16,8 +16,8 @@
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>200</x> <x>120</x>
<y>160</y> <y>140</y>
<width>91</width> <width>91</width>
<height>16</height> <height>16</height>
</rect> </rect>
@ -29,9 +29,9 @@
<widget class="QLineEdit" name="lineEdit_filepath"> <widget class="QLineEdit" name="lineEdit_filepath">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>200</x> <x>120</x>
<y>180</y> <y>160</y>
<width>381</width> <width>471</width>
<height>20</height> <height>20</height>
</rect> </rect>
</property> </property>
@ -42,8 +42,8 @@
<widget class="QPushButton" name="pushButton_selectfilepath"> <widget class="QPushButton" name="pushButton_selectfilepath">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>494</x> <x>500</x>
<y>150</y> <y>130</y>
<width>91</width> <width>91</width>
<height>24</height> <height>24</height>
</rect> </rect>
@ -55,8 +55,8 @@
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label_2">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>200</x> <x>120</x>
<y>210</y> <y>190</y>
<width>291</width> <width>291</width>
<height>51</height> <height>51</height>
</rect> </rect>
@ -70,8 +70,8 @@
<widget class="QPushButton" name="pushButton_exec"> <widget class="QPushButton" name="pushButton_exec">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>500</x> <x>420</x>
<y>210</y> <y>190</y>
<width>81</width> <width>81</width>
<height>51</height> <height>51</height>
</rect> </rect>
@ -82,7 +82,29 @@
</font> </font>
</property> </property>
<property name="text"> <property name="text">
<string>添加基础知识</string> <string>预导入
基础知识</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_commit">
<property name="geometry">
<rect>
<x>510</x>
<y>190</y>
<width>81</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 127);</string>
</property>
<property name="text">
<string>提交至数据库</string>
</property> </property>
</widget> </widget>
</widget> </widget>