feat: 修改记录

This commit is contained in:
iridiumR 2023-06-04 14:10:30 +08:00
parent 8893189866
commit cb194bc5ab
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8

View file

@ -10,7 +10,6 @@ class TransTab(TabPage):
super().__init__() super().__init__()
self.initUI() self.initUI()
self.pg = parent.pg self.pg = parent.pg
self.rows = []
def initUI(self): def initUI(self):
# 创建顶部控件 # 创建顶部控件
@ -124,7 +123,98 @@ class TransTab(TabPage):
QMessageBox.critical(self, 'Error', str(e)) QMessageBox.critical(self, 'Error', str(e))
def onTransModifyClicked(self): def onTransModifyClicked(self):
pass # 获取当前选择的行
self.rows = self.transTable.selectionModel().selectedRows()
if len(self.rows) == 0:
QMessageBox.critical(self, 'Error', 'No row selected')
return
# 创建修改数据对话框
self.tDialog = QDialog(self)
self.tDialog.setWindowTitle('Modify Transaction')# 时间 金额 描述
timeLabel=QLabel("Time:")
timeLine=QLineEdit()
timeLine.setText(self.transData[self.rows[0].row()][4].strftime("%Y-%m-%d %H:%M:%S"))
amountLabel = QLabel("Amount:")
amountLine = QLineEdit()
num = self.transData[self.rows[0].row()][5]
# 去掉¥符号
num = num[1:]
amountLine.setText(num)
# 只允许输入两位小数
amountLine.setValidator(QDoubleValidator(0.00, 999999999.99, 2))
descriptionLabel = QLabel("Description:")
descriptionLine = QLineEdit()
descriptionLine.setText(self.transData[self.rows[0].row()][6])
cButton = QPushButton("Confirm")
aButton = QPushButton("Abort")
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(cButton)
buttonLayout.addWidget(aButton)
cButton.clicked.connect(self.tDialog.accept)
aButton.clicked.connect(self.tDialog.reject)
self.dialogLayout = QFormLayout()
self.dialogLayout.addRow(timeLabel, timeLine)
self.dialogLayout.addRow(amountLabel, amountLine)
self.dialogLayout.addRow(descriptionLabel, descriptionLine)
self.dialogLayout.addRow(buttonLayout)
self.dialogLayout.setSpacing(12)
self.dialogLayout.setContentsMargins(15, 15, 15, 15)
self.tDialog.setMinimumWidth(400)
self.tDialog.setLayout(self.dialogLayout)
if self.tDialog.exec() == QDialog.DialogCode.Accepted:
try:
# 若修改金额,则修改账户余额
if num != amountLine.text():
amount = float(amountLine.text())
amount = amount - float(num)
print(amount)
self.pg.execute("UPDATE account SET balance = balance + (%s::NUMERIC)::MONEY WHERE a_id = %s",\
(amount, self.transData[self.rows[0].row()][1],))
# 检测时间格式
time = timeLine.text()
time = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
# 把 meta 取回来
self.pg.execute("SELECT meta FROM transaction WHERE t_id = %s", (self.transData[self.rows[0].row()][0],))
meta = self.pg.fetchall()[0][0]
meta = json.dumps(meta)
# 修改描述
meta = json.loads(meta)
meta["description"] = descriptionLine.text()
meta = json.dumps(meta)
# 修改数据, 使用JSONB
self.pg.execute("UPDATE transaction SET time = %s, amount = %s, meta = %s WHERE t_id = %s",\
(time, amountLine.text(), meta, self.transData[self.rows[0].row()][0],))
# 刷新表格
self.selected()
except Exception as e:
print(e)
QMessageBox.critical(self, 'Error', str(e))
else:
return