Add sessions table. Save session information

This commit is contained in:
Christopher T. Johnson
2023-12-12 12:26:44 -05:00
parent 3aec9cc2c8
commit ceee56cca3
3 changed files with 67 additions and 5 deletions

View File

@@ -4,9 +4,9 @@ from PyQt6.QtCore import Qt, QTime, QTimer, pyqtSignal, pyqtSlot
from PyQt6.QtGui import ( from PyQt6.QtGui import (
QStandardItem, QStandardItem,
QStandardItemModel, QStandardItemModel,
QTextBlockFormat,
QTextCursor, QTextCursor,
QTextDocument, QTextDocument,
QTextBlockFormat
) )
from PyQt6.QtSql import QSqlQuery from PyQt6.QtSql import QSqlQuery
from PyQt6.QtWidgets import QDialog from PyQt6.QtWidgets import QDialog
@@ -65,7 +65,48 @@ class SessionDialog(QDialog, Ui_Dialog):
@pyqtSlot() @pyqtSlot()
def accept(self) -> None: def accept(self) -> None:
print("Accepted") if not self.sessionStart:
super().accept()
return
if not self.sessionEnd:
self.sessionEnd = datetime.now()
query = QSqlQuery()
query.prepare(
"INSERT INTO sessions "
"(person_id, start, stop, notes) "
"VALUES (:person_id, :start, :stop, :notes)"
)
query.bindValue(":person_id", self.person_id)
query.bindValue(":start", self.sessionStart.isoformat())
query.bindValue(":stop", self.sessionEnd.isoformat())
query.bindValue(":notes", self.textEdit.toPlainText())
if not query.exec():
query_error(query)
session_id = query.lastInsertId()
model = self.wordView.model()
sql = "INSERT INTO session_word (session_id,word_id) VALUES "
parameters = ["(?,?)" for x in range(model.rowCount())]
sql += ", ".join(parameters)
query.prepare(sql)
for row in range(model.rowCount()):
query.addBindValue(session_id)
query.addBindValue(model.item(row).data(SessionDialog.WordIdRole))
if not query.exec():
query_error(query)
sql = (
"INSERT INTO session_block (session_id, section_id, block) VALUES "
)
parameters = ["(?,?,?)" for x in range(self.blocks.rowCount())]
sql += ",".join(parameters)
query.prepare(sql)
for row in range(self.blocks.rowCount()):
query.addBindValue(session_id)
item = self.blocks.item(row)
query.addBindValue(item.data(SessionDialog.SectionIdRole))
query.addBindValue(item.data(SessionDialog.BlockRole))
if not query.exec():
query_error(query)
super().accept() super().accept()
return return
@@ -133,6 +174,8 @@ class SessionDialog(QDialog, Ui_Dialog):
@pyqtSlot(int, int) @pyqtSlot(int, int)
def addBlock(self, section_id, block) -> None: def addBlock(self, section_id, block) -> None:
if not self.activeBox.isChecked():
return
new_block = QStandardItem() new_block = QStandardItem()
new_block.setData(section_id, SessionDialog.SectionIdRole) new_block.setData(section_id, SessionDialog.SectionIdRole)
new_block.setData(block, SessionDialog.BlockRole) new_block.setData(block, SessionDialog.BlockRole)
@@ -164,6 +207,9 @@ class SessionDialog(QDialog, Ui_Dialog):
blockFormat = QTextBlockFormat() blockFormat = QTextBlockFormat()
blockFormat.setTextIndent(25.0) blockFormat.setTextIndent(25.0)
self.textBrowser.textCursor().setBlockFormat(blockFormat) self.textBrowser.textCursor().setBlockFormat(blockFormat)
self.textBrowser.textCursor().insertHtml('<p>'+textBlock.text()+'</p>') self.textBrowser.textCursor().insertHtml(
"<p>" + textBlock.text() + "</p>"
)
self.textBrowser.textCursor().insertBlock() self.textBrowser.textCursor().insertBlock()
self.textBrowser.ensureCursorVisible()
return return

16
main.py
View File

@@ -192,6 +192,22 @@ SQL_CMDS = [
"(word_id INTEGER REFERENCES words ON DELETE CASCADE, " "(word_id INTEGER REFERENCES words ON DELETE CASCADE, "
"section_id INTEGER REFERENCES sections ON DELETE CASCADE, " "section_id INTEGER REFERENCES sections ON DELETE CASCADE, "
"block INTEGER NOT NULL, start INTEGER NOT NULL, end INTEGER NOT NULL)", "block INTEGER NOT NULL, start INTEGER NOT NULL, end INTEGER NOT NULL)",
#
"CREATE TABLE IF NOT EXISTS sessions "
"(session_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"person_id INTEGER REFERENCES people ON DELETE CASCADE, "
"start TEXT DEFAULT '', "
"stop TEXT DEFAULT '', "
"notes TEXT DEFAULT '')",
#
"CREATE TABLE IF NOT EXISTS session_word "
"(session_id INTEGER REFERENCES sessions ON DELETE CASCADE, "
"word_id INTEGER REFERENCES words ON DELETE CASCADE)",
#
"CREATE TABLE IF NOT EXISTS session_block "
"(session_id INTEGER REFERENCES sessions ON DELETE CASCADE, "
"section_id INTEGER REFERENCES sections ON DELETE CASCADE, "
"block INTEGER)",
] ]