迁移 编辑题目

This commit is contained in:
weiye.wang 2024-04-25 23:26:08 +08:00
parent e88ebc3413
commit 8613d6c338
4 changed files with 471 additions and 1 deletions

View File

@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file '题目内容直接编辑.ui'
##
## Created by: Qt User Interface Compiler version 6.6.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QLineEdit, QPlainTextEdit,
QPushButton, QSizePolicy, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(760, 490)
self.label = QLabel(Form)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(60, 110, 54, 16))
self.lineEdit_ID = QLineEdit(Form)
self.lineEdit_ID.setObjectName(u"lineEdit_ID")
self.lineEdit_ID.setGeometry(QRect(60, 130, 71, 20))
self.pushButton_content = QPushButton(Form)
self.pushButton_content.setObjectName(u"pushButton_content")
self.pushButton_content.setGeometry(QRect(60, 160, 75, 24))
self.pushButton_genre = QPushButton(Form)
self.pushButton_genre.setObjectName(u"pushButton_genre")
self.pushButton_genre.setGeometry(QRect(60, 190, 75, 24))
self.pushButton_ans = QPushButton(Form)
self.pushButton_ans.setObjectName(u"pushButton_ans")
self.pushButton_ans.setGeometry(QRect(60, 220, 75, 24))
self.pushButton_solution = QPushButton(Form)
self.pushButton_solution.setObjectName(u"pushButton_solution")
self.pushButton_solution.setGeometry(QRect(60, 250, 75, 24))
self.pushButton_origin = QPushButton(Form)
self.pushButton_origin.setObjectName(u"pushButton_origin")
self.pushButton_origin.setGeometry(QRect(60, 280, 75, 24))
self.pushButton_remarks = QPushButton(Form)
self.pushButton_remarks.setObjectName(u"pushButton_remarks")
self.pushButton_remarks.setGeometry(QRect(60, 310, 75, 24))
self.label_2 = QLabel(Form)
self.label_2.setObjectName(u"label_2")
self.label_2.setGeometry(QRect(140, 110, 191, 16))
self.plainTextEdit_toedit = QPlainTextEdit(Form)
self.plainTextEdit_toedit.setObjectName(u"plainTextEdit_toedit")
self.plainTextEdit_toedit.setGeometry(QRect(140, 130, 471, 201))
self.pushButton_exec = QPushButton(Form)
self.pushButton_exec.setObjectName(u"pushButton_exec")
self.pushButton_exec.setGeometry(QRect(620, 130, 61, 91))
self.pushButton_tocommit = QPushButton(Form)
self.pushButton_tocommit.setObjectName(u"pushButton_tocommit")
self.pushButton_tocommit.setGeometry(QRect(620, 230, 61, 101))
font = QFont()
font.setBold(True)
self.pushButton_tocommit.setFont(font)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"\u9898\u5e93\u5185\u5bb9\u76f4\u63a5\u7f16\u8f91", None))
self.label.setText(QCoreApplication.translate("Form", u"\u9898\u53f7", None))
self.pushButton_content.setText(QCoreApplication.translate("Form", u"\u9898\u76ee\u5185\u5bb9", None))
self.pushButton_genre.setText(QCoreApplication.translate("Form", u"\u7c7b\u578b", None))
self.pushButton_ans.setText(QCoreApplication.translate("Form", u"\u7b54\u6848", None))
self.pushButton_solution.setText(QCoreApplication.translate("Form", u"\u89e3\u7b54", None))
self.pushButton_origin.setText(QCoreApplication.translate("Form", u"\u6765\u6e90", None))
self.pushButton_remarks.setText(QCoreApplication.translate("Form", u"\u5907\u6ce8\u5217\u8868", None))
self.label_2.setText(QCoreApplication.translate("Form", u"\u9898\u53f7\u4e3a \u7684 \u5b57\u6bb5", None))
self.pushButton_exec.setText(QCoreApplication.translate("Form", u"\u4fee\u6539", None))
self.pushButton_tocommit.setText(QCoreApplication.translate("Form", u"\u63d0\u4ea4", None))
# retranslateUi

View File

@ -22,6 +22,7 @@ from 单元挂钩 import MyWindow_dygg
from 统考数据导入 import MyWindow_tkdr
from 手动统计结果导入 import MyWindow_sddr
from 获取小闲平台使用数据 import MyWindow_xxdr
from 题目内容直接编辑 import MyWindow_bjtm
from database_tools_2 import *
class MyWindow(QMainWindow,Ui_MainWindow):
@ -49,6 +50,10 @@ class MyWindow(QMainWindow,Ui_MainWindow):
self.tjgl = MyWindow_tjgl(self.database)
self.layout_tjgl.addWidget(self.tjgl) #以上三行为初始化“添加关联”tab
self.layout_bjtm = QVBoxLayout(self.tab_bjtm)
self.bjtm = MyWindow_bjtm(self.database)
self.layout_bjtm.addWidget(self.bjtm) #以上三行为初始化“编辑题目”tab
self.layout_xxdr = QVBoxLayout(self.tab_xxdr)
self.xxdr = MyWindow_xxdr()
self.layout_xxdr.addWidget(self.xxdr) #以上三行为初始化“小闲统计数据导入”tab
@ -135,7 +140,8 @@ class MyWindow(QMainWindow,Ui_MainWindow):
self.bdsl.setdbname,
self.tjjc.setdbname,
self.gxsy.setdbname,
self.dygg.setdbname
self.dygg.setdbname,
self.bjtm.setdbname
]: #在列表中的tab里传送数据库名的连接
self.sendDBname.connect(func)
def sendValue(self):

View File

@ -0,0 +1,206 @@
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_题目内容直接编辑 import Ui_Form
from database_tools_2 import *
class MyWindow_bjtm(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
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()
self.changedids = []
# print(self.database_name)
def bind(self):
self.lineEdit_ID.textChanged.connect(self.getID)
self.pushButton_content.clicked.connect(self.showcontent)
self.pushButton_genre.clicked.connect(self.showgenre)
self.pushButton_ans.clicked.connect(self.showans)
self.pushButton_solution.clicked.connect(self.showsolution)
self.pushButton_origin.clicked.connect(self.showorigin)
self.pushButton_remarks.clicked.connect(self.showremarks)
self.pushButton_exec.clicked.connect(self.exec)
self.pushButton_tocommit.clicked.connect(self.tocommit)
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.changedids = []
def getID(self):
self.ID = self.lineEdit_ID.text().zfill(6)
def showcontent(self):
self.field = "题目内容"
self.label_2.setText(f"题号 {self.lineEdit_ID.text()}{self.field} 字段")
sql = "SELECT content FROM problems WHERE ID = (%s);"
val = (self.lineEdit_ID.text().zfill(6),)
self.cursor.execute(sql,val)
ret = self.cursor.fetchall()
if ret[0][0] is None:
self.originalcontent = ""
else:
self.originalcontent = ret[0][0].strip()
self.plainTextEdit_toedit.setPlainText(self.originalcontent)
def showgenre(self):
self.field = "类型"
self.label_2.setText(f"题号 {self.lineEdit_ID.text()}{self.field} 字段")
sql = "SELECT genre FROM problems WHERE ID = (%s);"
val = (self.lineEdit_ID.text().zfill(6),)
self.cursor.execute(sql,val)
ret = self.cursor.fetchall()
if ret[0][0] is None:
self.originalcontent = ""
else:
self.originalcontent = ret[0][0].strip()
self.plainTextEdit_toedit.setPlainText(self.originalcontent)
def showans(self):
self.field = "答案"
self.label_2.setText(f"题号 {self.lineEdit_ID.text()}{self.field} 字段")
sql = "SELECT ans FROM problems WHERE ID = (%s);"
val = (self.lineEdit_ID.text().zfill(6),)
self.cursor.execute(sql,val)
ret = self.cursor.fetchall()
if ret[0][0] is None:
self.originalcontent = ""
else:
self.originalcontent = ret[0][0].strip()
self.plainTextEdit_toedit.setPlainText(self.originalcontent)
def showsolution(self):
self.field = "解答"
self.label_2.setText(f"题号 {self.lineEdit_ID.text()}{self.field} 字段")
sql = "SELECT solution FROM problems WHERE ID = (%s);"
val = (self.lineEdit_ID.text().zfill(6),)
self.cursor.execute(sql,val)
ret = self.cursor.fetchall()
if ret[0][0] is None:
self.originalcontent = ""
else:
self.originalcontent = ret[0][0].strip()
self.plainTextEdit_toedit.setPlainText(self.originalcontent)
def showorigin(self):
self.field = "来源"
self.label_2.setText(f"题号 {self.lineEdit_ID.text()}{self.field} 字段")
sql = "SELECT origin FROM problems WHERE ID = (%s);"
val = (self.lineEdit_ID.text().zfill(6),)
self.cursor.execute(sql,val)
ret = self.cursor.fetchall()
if ret[0][0] is None:
self.originalcontent = ""
else:
self.originalcontent = ret[0][0].strip()
self.plainTextEdit_toedit.setPlainText(self.originalcontent)
def showremarks(self):
self.field = "备注"
self.label_2.setText(f"题号 {self.lineEdit_ID.text()}{self.field} 字段")
sql = "SELECT date,remark_content FROM remarks WHERE ID = (%s);"
val = (self.lineEdit_ID.text().zfill(6),)
self.cursor.execute(sql,val)
ret_list = self.cursor.fetchall()
if len(ret_list) == 0:
self.originalcontent = ""
else:
self.originalcontent = "\n\n----------\n\n".join([ret[0]+"\t"+ret[1] for ret in ret_list])
self.plainTextEdit_toedit.setPlainText(self.originalcontent)
def exec(self):
self.currentcontent = self.plainTextEdit_toedit.toPlainText().strip()
if self.currentcontent == self.originalcontent:
print("未改变, 不作处理.")
elif self.field in ["题目内容","答案","解答","来源"]:
corresp = {"题目内容":"content","答案":"ans","解答":"solution","来源":"origin"}
sql = f"UPDATE problems SET {corresp[self.field]} = %s WHERE ID = %s;"
val = (self.currentcontent,self.ID)
self.cursor.execute(sql,val)
sql = "INSERT INTO logs (DATE,TIME,username,action,id,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
val = (GetDate(),GetTime(),get_git_username(),f"修改{self.field}",self.ID,f"{self.originalcontent} -> {self.currentcontent}")
self.cursor.execute(sql,val)
if self.field == "题目内容":
sql = f"INSERT INTO edit_history (ID,date,editor) VALUE (%s,%s,%s);"
val = (self.ID,GetDate(),get_git_username())
self.cursor.execute(sql,val)
print(f"{self.ID} {self.field} 修改已完成")
self.changedids.append(self.ID)
elif self.field == "类型":
sql = "UPDATE problems SET genre = %s WHERE ID = %s;"
val = (self.currentcontent,self.ID)
if self.currentcontent == "解答题":
self.cursor.execute("UPDATE problems SET space = %s WHERE ID = %s;",("4em",self.ID))
else:
self.cursor.execute("UPDATE problems SET space = %s WHERE ID = %s;",("",self.ID))
self.cursor.execute(sql,val)
sql = "INSERT INTO logs (DATE,TIME,username,action,id,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
val = (GetDate(),GetTime(),get_git_username(),f"修改类型",self.ID,f"{self.originalcontent} -> {self.currentcontent}")
self.cursor.execute(sql,val)
print(f"{self.ID} {self.field} 修改已完成")
self.changedids.append(self.ID)
elif self.field == "备注":
self.original_list = [item.strip() for item in self.originalcontent.split("-"*10)]
self.current_list = [item.strip() for item in self.currentcontent.split("-"*10)]
if len(self.original_list) != len(self.current_list):
print("有问题, 需保持备注的数量一致")
else:
for i in range(len(self.original_list)):
if self.original_list[i] != self.current_list[i]:
rm_date_origin,rm_content_origin = parseRemark(self.original_list[i])
rm_date_current,rm_content_current = parseRemark(self.current_list[i])
sql = "UPDATE remarks SET date = %s, remark_content = %s WHERE ID = %s AND date = %s AND remark_content = %s;"
val = (rm_date_current,rm_content_current,self.ID,rm_date_origin,rm_content_origin)
self.cursor.execute(sql,val)
sql = "INSERT INTO logs (DATE,TIME,username,action,id,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
val = (GetDate(),GetTime(),get_git_username(),f"修改备注",self.ID,f"{self.original_list[i]} -> {self.current_list[i]}")
self.cursor.execute(sql,val)
print(f"{self.ID} {self.field} 修改已完成")
self.changedids.append(self.ID)
def tocommit(self):
if len(self.changedids) > 0:
configjson = BuildFullScheme
latexbody = "\\begin{enumerate}\n\n"
for id in sorted(list(set(self.changedids))):
latexbody += generateLaTeXBodyContentfromMariaDB(self.cursor,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("修改后检验成功, 已导入数据库.")
self.db.commit()
else:
print("修改后检验失败, 已回滚.")
self.db.rollback()
self.changedids = []
else:
print("未作修改.")
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow_bjtm()
windows.show()
app.exec()

View File

@ -0,0 +1,174 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>760</width>
<height>490</height>
</rect>
</property>
<property name="windowTitle">
<string>题库内容直接编辑</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>60</x>
<y>110</y>
<width>54</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>题号</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_ID">
<property name="geometry">
<rect>
<x>60</x>
<y>130</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton_content">
<property name="geometry">
<rect>
<x>60</x>
<y>160</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>题目内容</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_genre">
<property name="geometry">
<rect>
<x>60</x>
<y>190</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>类型</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_ans">
<property name="geometry">
<rect>
<x>60</x>
<y>220</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>答案</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_solution">
<property name="geometry">
<rect>
<x>60</x>
<y>250</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>解答</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_origin">
<property name="geometry">
<rect>
<x>60</x>
<y>280</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>来源</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_remarks">
<property name="geometry">
<rect>
<x>60</x>
<y>310</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>备注列表</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>140</x>
<y>110</y>
<width>191</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>题号为 的 字段</string>
</property>
</widget>
<widget class="QPlainTextEdit" name="plainTextEdit_toedit">
<property name="geometry">
<rect>
<x>140</x>
<y>130</y>
<width>471</width>
<height>201</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton_exec">
<property name="geometry">
<rect>
<x>620</x>
<y>130</y>
<width>61</width>
<height>91</height>
</rect>
</property>
<property name="text">
<string>修改</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_tocommit">
<property name="geometry">
<rect>
<x>620</x>
<y>230</y>
<width>61</width>
<height>101</height>
</rect>
</property>
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>提交</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>