From 2f11b0314ea75eef219e2c133433e4d0e62b7e8c Mon Sep 17 00:00:00 2001 From: wangweiye7840 Date: Sun, 28 Apr 2024 15:18:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E7=9F=A5=E8=AF=86=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 工具v4/基础知识编辑.py | 74 +++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/工具v4/基础知识编辑.py b/工具v4/基础知识编辑.py index 8db28d79..38639f45 100644 --- a/工具v4/基础知识编辑.py +++ b/工具v4/基础知识编辑.py @@ -1,4 +1,4 @@ -from PySide6.QtWidgets import QWidget, QApplication, QFileDialog +from PySide6.QtWidgets import QWidget, QApplication, QFileDialog, QTableWidgetItem from Ui_基础知识编辑 import Ui_Form from database_tools_2 import * @@ -11,12 +11,6 @@ class MyWindow_bjjc(QWidget,Ui_Form): def setdbname(self,string): self.database_name = string - try: - self.db.close() - except: - pass - self.db = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name) - self.cursor = self.db.cursor() # print(self.database_name) def bind(self): @@ -24,19 +18,75 @@ class MyWindow_bjjc(QWidget,Ui_Form): self.pushButton_modify.clicked.connect(self.modify) self.pushButton_commit.clicked.connect(self.tocommit) self.pushButton_commit.setDisabled(True) - self.db = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name) - self.cursor = self.db.cursor() + self.pushButton_modify.setDisabled(True) + self.plainTextEdit_content.textChanged.connect(lambda: self.pushButton_modify.setEnabled(True)) + self.tableWidget_obj.setColumnWidth(0,80) + self.tableWidget_obj.setColumnWidth(1,160) errorflag = True def getcontent(self): - pass + self.db = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name) + self.cursor = self.db.cursor() + self.id = self.lineEdit_bnid.text().upper() + if not self.id[0] == "B": + self.id = "B" + str(int(self.id)).zfill(5) + sql = "SELECT * FROM basic_knowledges WHERE bn_id = %s;" + val = (self.id,) + self.cursor.execute(sql,val) + ret_list = self.cursor.fetchall() + if not len(ret_list) == 1: + print("基础知识编号有误, 请重试.") + else: + self.bn_content_raw = ret_list[0][1] + self.plainTextEdit_content.setPlainText(self.bn_content_raw) + sql = "SELECT * from bn_obj_corresp WHERE bn_id = %s;" + val = (self.id,) + self.cursor.execute(sql,val) + ret_list = self.cursor.fetchall() + self.tableWidget_obj.setRowCount(len(ret_list)) + count = 0 + for ret in ret_list: + count += 1 + objid = ret[1] + sql = "SELECT * FROM lessonobj WHERE objid = %s;" + val = (objid,) + self.cursor.execute(sql,val) + obj_content = self.cursor.fetchall()[0][1] + self.tableWidget_obj.setItem(count-1,0,QTableWidgetItem(objid)) + self.tableWidget_obj.setItem(count-1,1,QTableWidgetItem(obj_content)) + + def modify(self): - pass + self.bn_content_new = self.plainTextEdit_content.toPlainText() + self.cursor = self.db.cursor() + sql = "UPDATE basic_knowledges SET bn_content = %s WHERE bn_id = %s;" + val = (self.bn_content_new,self.id) + self.cursor.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"修改基础知识梳理",f"{self.id}: {self.bn_content_raw} -> {self.bn_content_new}") + self.cursor.execute(sql,val) + self.pushButton_modify.setDisabled(True) + self.pushButton_commit.setEnabled(True) + def tocommit(self): self.pushButton_commit.setDisabled(True) - pass + latex_raw = ReadTextFile("模板文件/讲义模板.txt") + 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,("试编译",self.bn_content_new)) #替换标题和bodystring + outputdir = os.path.join(os.getcwd(),"临时文件") + outputfilepath = os.path.join(outputdir,"试编译.tex") + SaveTextFile(latex_data,outputfilepath) + if XeLaTeXCompile(outputdir,"试编译.tex",times=1): + print("修改后检验成功, 已导入数据库.") + self.db.commit() + else: + print("修改后检验失败, 已回滚.") + self.db.rollback() + self.db.close()