Feedback on assigning books
This commit is contained in:
78
main.py
78
main.py
@@ -14,7 +14,7 @@ from PyQt6.QtGui import (
|
||||
QTextListFormat,
|
||||
)
|
||||
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 ui.MainWindow import Ui_MainWindow
|
||||
@@ -33,27 +33,36 @@ def query_error(query: QSqlQuery) -> None:
|
||||
|
||||
|
||||
class ModelOverride(QSqlQueryModel):
|
||||
enableFlag = False
|
||||
|
||||
def flags(self, index: QModelIndex) -> Qt.ItemFlag:
|
||||
itemFlags = super(ModelOverride, self).flags(index)
|
||||
if self.enableFlag:
|
||||
return itemFlags
|
||||
value = index.siblingAtColumn(3).data()
|
||||
if not value or value < 1:
|
||||
itemFlags &= ~(Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled)
|
||||
return itemFlags
|
||||
|
||||
@pyqtSlot()
|
||||
def toggle(self) -> None:
|
||||
self.enableFlag = not self.enableFlag
|
||||
return
|
||||
|
||||
|
||||
class MainWindow(QMainWindow, Ui_MainWindow):
|
||||
def __init__(self) -> None:
|
||||
super(MainWindow, self).__init__()
|
||||
self.setupUi(self)
|
||||
# model = QSqlQueryModel()
|
||||
model = ModelOverride()
|
||||
query = QSqlQuery("SELECT * FROM people ORDER BY name")
|
||||
model.setQuery(query)
|
||||
self.peopleView.setModel(model)
|
||||
self.peopleView.setModelColumn(1)
|
||||
self.ReadButton.clicked.connect(self.readAction)
|
||||
self.ReadButton.clicked.connect(model.toggle)
|
||||
self.bookBtn.clicked.connect(self.bookAction)
|
||||
self.peopleView.doubleClicked.connect(self.readAction)
|
||||
self.actionQuit.triggered.connect(self.close)
|
||||
self.createActions()
|
||||
self.show()
|
||||
return
|
||||
@@ -78,15 +87,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
|
||||
if len(indexes) < 1:
|
||||
return
|
||||
person_id = indexes[0].siblingAtColumn(0).data()
|
||||
print(person_id)
|
||||
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(
|
||||
"SELECT * FROM person_book "
|
||||
"WHERE person_id = :person_id "
|
||||
@@ -96,25 +97,53 @@ class MainWindow(QMainWindow, Ui_MainWindow):
|
||||
query.bindValue(":book_id", book_id)
|
||||
if not query.exec():
|
||||
query_error(query)
|
||||
if query.next():
|
||||
return
|
||||
if not query.next():
|
||||
query.prepare(
|
||||
"SELECT * FROM sections WHERE sequence = 0 " "AND book_id = :book_id"
|
||||
)
|
||||
query.bindValue(":book_id", book_id)
|
||||
if not query.exec():
|
||||
query_error(query)
|
||||
if not query.next():
|
||||
raise Exception(f"book_id: {book_id} has no section 0!")
|
||||
section_id = query.value("section_id")
|
||||
query.prepare(
|
||||
"INSERT INTO person_book "
|
||||
"VALUES (:person_id, :book_id, :section_id, 0)"
|
||||
)
|
||||
query.bindValue(":person_id", person_id)
|
||||
query.bindValue(":book_id", book_id)
|
||||
query.bindValue(":section_id", section_id)
|
||||
if not query.exec():
|
||||
query_error(query)
|
||||
query.prepare(
|
||||
"SELECT * FROM sections WHERE sequence = 0 " "AND book_id = :book_id"
|
||||
"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)
|
||||
if not query.next():
|
||||
raise Exception(f"book_id: {book_id} has no section 0!")
|
||||
section_id = query.value("section_id")
|
||||
query.prepare(
|
||||
"INSERT INTO person_book " "VALUES (:person_id, :book_id, :section_id, 0)"
|
||||
"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)
|
||||
query.bindValue(":book_id", book_id)
|
||||
query.bindValue(":section_id", section_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
|
||||
|
||||
@pyqtSlot()
|
||||
@@ -181,10 +210,10 @@ SQL_CMDS = [
|
||||
"CREATE TABLE IF NOT EXISTS sections "
|
||||
"(section_id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
||||
"sequence INTEGER, content TEXT, "
|
||||
"book_id INTEGER REFERENCES books ON DELETE CASCADE) ",
|
||||
"book_id INTEGER REFERENCES books ON DELETE CASCADE)",
|
||||
"CREATE TABLE IF NOT EXISTS people "
|
||||
"(person_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, "
|
||||
"organization TEXT, book_id INTEGER REFERENCES books ON DELETE CASCADE) ",
|
||||
"organization TEXT, book_id INTEGER REFERENCES books ON DELETE CASCADE)",
|
||||
"CREATE TABLE IF NOT EXISTS person_book "
|
||||
"(person_id INTEGER REFERENCES people ON DELETE CASCADE, "
|
||||
"book_id INTEGER REFERENCES books ON DELETE CASCADE, "
|
||||
@@ -201,13 +230,12 @@ def schema_update(db: QSqlDatabase) -> None:
|
||||
query = QSqlQuery()
|
||||
|
||||
for sql in SQL_CMDS:
|
||||
print(sql)
|
||||
inlower = sql.lower()
|
||||
inlower = sql.lower().strip()
|
||||
if not inlower.startswith("create table "):
|
||||
if not query.exec(sql):
|
||||
query_error(query)
|
||||
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)
|
||||
matches = re.search(r"^(CREATE TABLE )([^ ]+)( \(.+)$", create_cmd)
|
||||
if matches:
|
||||
|
||||
Reference in New Issue
Block a user