Feedback on assigning books

This commit is contained in:
Christopher T. Johnson
2023-11-16 11:00:15 -05:00
parent 4a2bdcdb49
commit f5e5bde497

62
main.py
View File

@@ -14,7 +14,7 @@ from PyQt6.QtGui import (
QTextListFormat, QTextListFormat,
) )
from PyQt6.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel from PyQt6.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
from PyQt6.QtWidgets import QApplication, QFileDialog, QMainWindow from PyQt6.QtWidgets import QApplication, QFileDialog, QMainWindow, QMessageBox
from lib import * from lib import *
from ui.MainWindow import Ui_MainWindow from ui.MainWindow import Ui_MainWindow
@@ -33,27 +33,36 @@ def query_error(query: QSqlQuery) -> None:
class ModelOverride(QSqlQueryModel): class ModelOverride(QSqlQueryModel):
enableFlag = False
def flags(self, index: QModelIndex) -> Qt.ItemFlag: def flags(self, index: QModelIndex) -> Qt.ItemFlag:
itemFlags = super(ModelOverride, self).flags(index) itemFlags = super(ModelOverride, self).flags(index)
if self.enableFlag:
return itemFlags
value = index.siblingAtColumn(3).data() value = index.siblingAtColumn(3).data()
if not value or value < 1: if not value or value < 1:
itemFlags &= ~(Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled) itemFlags &= ~(Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled)
return itemFlags return itemFlags
@pyqtSlot()
def toggle(self) -> None:
self.enableFlag = not self.enableFlag
return
class MainWindow(QMainWindow, Ui_MainWindow): class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self) -> None: def __init__(self) -> None:
super(MainWindow, self).__init__() super(MainWindow, self).__init__()
self.setupUi(self) self.setupUi(self)
# model = QSqlQueryModel()
model = ModelOverride() model = ModelOverride()
query = QSqlQuery("SELECT * FROM people ORDER BY name") query = QSqlQuery("SELECT * FROM people ORDER BY name")
model.setQuery(query) model.setQuery(query)
self.peopleView.setModel(model) self.peopleView.setModel(model)
self.peopleView.setModelColumn(1) self.peopleView.setModelColumn(1)
self.ReadButton.clicked.connect(self.readAction) self.ReadButton.clicked.connect(model.toggle)
self.bookBtn.clicked.connect(self.bookAction) self.bookBtn.clicked.connect(self.bookAction)
self.peopleView.doubleClicked.connect(self.readAction) self.peopleView.doubleClicked.connect(self.readAction)
self.actionQuit.triggered.connect(self.close)
self.createActions() self.createActions()
self.show() self.show()
return return
@@ -78,15 +87,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
if len(indexes) < 1: if len(indexes) < 1:
return return
person_id = indexes[0].siblingAtColumn(0).data() person_id = indexes[0].siblingAtColumn(0).data()
print(person_id)
query = QSqlQuery() query = QSqlQuery()
query.prepare(
"UPDATE people SET book_id = :book_id " "WHERE person_id = :person_id"
)
query.bindValue(":book_id", book_id)
query.bindValue(":person_id", person_id)
if not query.exec():
query_error(query)
query.prepare( query.prepare(
"SELECT * FROM person_book " "SELECT * FROM person_book "
"WHERE person_id = :person_id " "WHERE person_id = :person_id "
@@ -96,8 +97,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
query.bindValue(":book_id", book_id) query.bindValue(":book_id", book_id)
if not query.exec(): if not query.exec():
query_error(query) query_error(query)
if query.next(): if not query.next():
return
query.prepare( query.prepare(
"SELECT * FROM sections WHERE sequence = 0 " "AND book_id = :book_id" "SELECT * FROM sections WHERE sequence = 0 " "AND book_id = :book_id"
) )
@@ -108,13 +108,42 @@ class MainWindow(QMainWindow, Ui_MainWindow):
raise Exception(f"book_id: {book_id} has no section 0!") raise Exception(f"book_id: {book_id} has no section 0!")
section_id = query.value("section_id") section_id = query.value("section_id")
query.prepare( query.prepare(
"INSERT INTO person_book " "VALUES (:person_id, :book_id, :section_id, 0)" "INSERT INTO person_book "
"VALUES (:person_id, :book_id, :section_id, 0)"
) )
query.bindValue(":person_id", person_id) query.bindValue(":person_id", person_id)
query.bindValue(":book_id", book_id) query.bindValue(":book_id", book_id)
query.bindValue(":section_id", section_id) query.bindValue(":section_id", section_id)
if not query.exec(): if not query.exec():
query_error(query) query_error(query)
query.prepare(
"UPDATE people SET book_id = :book_id " "WHERE person_id = :person_id"
)
query.bindValue(":book_id", book_id)
query.bindValue(":person_id", person_id)
if not query.exec():
query_error(query)
query.prepare(
"SELECT p.name,b.title FROM people p "
"LEFT JOIN books b "
"ON (p.book_id = b.book_id) "
"WHERE p.person_id = :person_id"
)
query.bindValue(":person_id", person_id)
if not query.exec():
query_error(query)
query.next()
title = query.value("title")
name = query.value("name")
QMessageBox.information(
self, "Book Assignment", f"{title} was assigned to {name}"
)
self.resetPeopleModel()
return
def resetPeopleModel(self) -> None:
query = QSqlQuery("SELECT * FROM people ORDER BY name")
self.peopleView.model().setQuery(query)
return return
@pyqtSlot() @pyqtSlot()
@@ -201,13 +230,12 @@ def schema_update(db: QSqlDatabase) -> None:
query = QSqlQuery() query = QSqlQuery()
for sql in SQL_CMDS: for sql in SQL_CMDS:
print(sql) inlower = sql.lower().strip()
inlower = sql.lower()
if not inlower.startswith("create table "): if not inlower.startswith("create table "):
if not query.exec(sql): if not query.exec(sql):
query_error(query) query_error(query)
continue continue
create_cmd = re.sub(r"IF NOT EXISTS ", "", sql) create_cmd = re.sub(r"IF NOT EXISTS ", "", sql.strip())
create_cmd = re.sub(r"\s\s*", " ", create_cmd) create_cmd = re.sub(r"\s\s*", " ", create_cmd)
matches = re.search(r"^(CREATE TABLE )([^ ]+)( \(.+)$", create_cmd) matches = re.search(r"^(CREATE TABLE )([^ ]+)( \(.+)$", create_cmd)
if matches: if matches: