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

133 lines
6.2 KiB
Python

from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_讲义结构与内容录入 import Ui_Form
from database_tools_2 import *
class MyWindow_jglr(QWidget,Ui_Form):
def __init__(self,database_name):
super().__init__()
self.setupUi(self)
self.database_name = database_name
self.bind()
def bind(self):
self.label_next.setVisible(False)
self.pushButton_exec.clicked.connect(self.exec)
self.lineEdit_structure.textChanged.connect(self.unvisible)
self.comboBox_grade.currentIndexChanged.connect(self.unvisible)
self.comboBox_semester.currentIndexChanged.connect(self.unvisible)
def setdbname(self,string):
self.database_name = string
# print(self.database_name)
def unvisible(self):
self.label_next.setVisible(False)
def exec(self):
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name)
mycursor = mydb.cursor()
if not len(self.lineEdit_structure.text()) == 1:
print("首字母有误")
else:
self.label_next.setVisible(True)
self.repaint()
self.prefix = self.lineEdit_structure.text().upper()+self.comboBox_grade.currentText()+self.comboBox_semester.currentText()
notes_dict = load_notes_dict_from_mariadb(mycursor)
structure_dict = load_structures_dict_from_mariadb(mycursor)
# print(len(notes_dict))
if not self.prefix[0:5] in structure_dict:
AddNew = input("此类型不在列表中, 新增(A)/退出([Q]):")
if AddNew[0].upper() == "A":
descr = input("类型描述:")
cn = input("编号是否连续(T/[F]):")
if len(cn) > 0 and cn[0].upper() == "T":
cn = True
else:
cn = False
partscount = int(input("分为多少个部分:"))
new_struct_dict = {
"description": descr,
"consecutivenumbering": cn,
"structure": {}
}
for i in range(partscount):
partid = input(f"{i+1} 部分的代号:")
partname = input(f"部分 {partid} 的名称:")
spaceflag = input(f"部分 {partid}: {partname} 中是否要在题目后留空格(T/[F]):")
if len(spaceflag) > 0 and spaceflag[0].upper() == "T":
spaceflag = True
else:
spaceflag = False
new_struct_dict["structure"][partid] = {
"name": partname,
"spaceflag": spaceflag
}
sql = "INSERT INTO notestructures (initial,description,consecutivenumbering,structure) VALUES (%s,%s,%s,%s);"
val = (self.prefix[0:5],descr,cn,json.dumps(new_struct_dict["structure"],ensure_ascii = False))
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(),"添加新讲义结构",self.prefix[0:5])
mycursor.execute(sql,val)
mydb.commit()
else:
pass
else:
numberlist = []
for existing_nid in notes_dict:
if self.prefix in existing_nid:
numberlist.append(existing_nid[-2:])
print("该分类下已有材料编号: "+generate_exp(numberlist))
nid = self.prefix + input("请输入新材料编号(两位数):")
while nid in notes_dict:
print("编号重复, 请重新输入.")
nid = self.prefix + input("请输入新材料编号(两位数):")
name = input("请输入材料名称:")
filenameraw = input("生成的文件名和材料名称是否一致?([Y]/如果不一致请输入文件名):")
if filenameraw.upper() == "Y" or len(filenameraw.strip()) == 0:
filename = name
else:
filename = filenameraw
new_note_dict = {
"nid": nid,
"name": name,
"filename": filename
}
new_note_dict_structure = {}
structure = structure_dict[self.prefix[0:5]]['structure']
print(f"此类材料共有 {len(structure)} 个部分, 分别是:")
for p in structure:
print(f"{p}: {structure[p]['name']}")
new_note_dict_structure[p] = []
for p in structure:
rawdata = input(f"现在输入 {p}: {structure[p]['name']} 部分的内容编号:")
rawdata = RefinePunctuations(rawdata)
rawdata = re.sub(r"[^BXK\d:,]","",rawdata)
if re.findall(r"\d",rawdata) == []:
new_note_dict[p] = []
else:
new_note_dict[p] = generate_id_set(rawdata)
print(f"{p}: {new_note_dict[p]}")
temp_dict = new_note_dict.copy()
for key in ["nid","name","filename"]:
temp_dict.pop(key)
sql = "INSERT INTO notes (nid,name,filename,structure) VALUES (%s,%s,%s,%s);"
val = (nid,name,filename,json.dumps(temp_dict,ensure_ascii = False))
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(),"添加新讲义内容",nid)
mycursor.execute(sql,val)
mydb.commit()
print("处理完成")
mydb.close()
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow_jglr("tikutest")
windows.show()
app.exec()