This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具v4/修改目标内容.py

119 lines
5.1 KiB
Python

from PySide6.QtWidgets import QWidget, QApplication, QFileDialog, QTableWidgetItem
from Ui_修改目标内容 import Ui_Form
from database_tools_2 import *
class MyWindow_xgmb(QWidget,Ui_Form):
def __init__(self,database_name):
super().__init__()
self.database_name = database_name
self.setupUi(self)
self.bind()
def setdbname(self,string):
self.database_name = string
# print(self.database_name)
def bind(self):
self.pushButton_getcontent.clicked.connect(self.getcontent)
self.pushButton_modify.clicked.connect(self.modify)
self.pushButton_commit.clicked.connect(self.tocommit)
self.pushButton_commit.setDisabled(True)
self.pushButton_modify.setDisabled(True)
self.plainTextEdit_objcontent.textChanged.connect(lambda: self.pushButton_modify.setEnabled(True))
errorflag = True
def getcontent(self):
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_id.text().upper()
if self.id[0] == "K":
self.obj_type = "lesson"
elif self.id[0] == "D":
self.obj_type = "unit"
else:
self.obj_type = "error"
if self.obj_type == "lesson":
sql = "SELECT * FROM lessonobj WHERE objid = %s AND NOT obsolete;"
val = (self.id,)
self.cursor.execute(sql,val)
ret_list = self.cursor.fetchall()
if not len(ret_list) == 1:
print("课时目标代码有误, 请重试.")
self.plainTextEdit_objcontent.setPlainText("课时目标代码有误, 请重试.")
else:
self.objcontent_raw = ret_list[0][1]
self.plainTextEdit_objcontent.setPlainText(self.objcontent_raw)
elif self.obj_type == "unit":
sql = "SELECT * FROM unitobj WHERE unit_obj_id = %s AND NOT obsolete;"
val = (self.id,)
self.cursor.execute(sql,val)
ret_list = self.cursor.fetchall()
if not len(ret_list) == 1:
print("单元目标代码有误, 请重试.")
self.plainTextEdit_objcontent.setPlainText("单元目标代码有误, 请重试.")
else:
self.objcontent_raw = ret_list[0][1]
self.plainTextEdit_objcontent.setPlainText(self.objcontent_raw)
else:
print("目标代码类型有误, 请重试.")
self.plainTextEdit_objcontent.setPlainText("目标代码类型有误, 请重试.")
self.pushButton_modify.setDisabled(True)
def modify(self):
pass
self.objcontent_new = self.plainTextEdit_objcontent.toPlainText()
self.cursor = self.db.cursor()
if self.obj_type == "lesson":
sql = "UPDATE lessonobj SET obj_content = %s where objid = %s;"
val = (self.objcontent_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"{self.id}: {self.objcontent_raw} -> {self.objcontent_new}")
self.cursor.execute(sql,val)
self.pushButton_modify.setDisabled(True)
self.pushButton_commit.setEnabled(True)
elif self.obj_type == "unit":
sql = "UPDATE unitobj SET unit_obj_content = %s where unit_obj_id = %s;"
val = (self.objcontent_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"{self.id}: {self.objcontent_raw} -> {self.objcontent_new}")
self.cursor.execute(sql,val)
self.pushButton_modify.setDisabled(True)
self.pushButton_commit.setEnabled(True)
def tocommit(self):
self.pushButton_commit.setDisabled(True)
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.objcontent_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()
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow_xgmb("tikutest")
windows.show()
app.exec()