From f2ca02a9230621d0968b8df129051e332339b768 Mon Sep 17 00:00:00 2001 From: Louie S Date: Sat, 16 Sep 2023 09:36:14 -0400 Subject: Saving and loading entries working --- add_entry_form.py | 7 +++--- add_group_form.py | 1 - assignment-list.py | 23 -------------------- db_sqlite.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++- entry.py | 8 ++++--- 5 files changed, 71 insertions(+), 31 deletions(-) diff --git a/add_entry_form.py b/add_entry_form.py index cedaaf6..46a0c61 100644 --- a/add_entry_form.py +++ b/add_entry_form.py @@ -4,6 +4,7 @@ from PyQt5.QtGui import QFont from PyQt5.QtCore import QDate, Qt from entry import Entry Globals = __import__("globals") +DB = __import__("db_sqlite") class addEntryForm(QDialog): def __init__(self, parent): @@ -60,7 +61,7 @@ class addEntryForm(QDialog): def handleSubmit(self, parent): # Check that the new entry is not blank desc_text = self.new_entry_desc.text() - due_text = self.new_entry_due.text() + due_text = self.new_entry_due.date() # due_text is a QDate due_alt_text = self.new_entry_due_alt.text() link_text = self.new_entry_link.text() @@ -71,8 +72,8 @@ class addEntryForm(QDialog): QMessageBox.Close) return - # TODO do the database stuff (this will allow us to get the id) - Globals.entries.append(Entry(parent, desc_text, due_text, due_alt_text, link_text)) + new_id = DB.insertEntry(Entry(0, parent, desc_text, due_text, due_alt_text, link_text)) + Globals.entries.append(Entry(new_id, parent, desc_text, due_text, due_alt_text, link_text)) self.close() if __name__ == "__main__": diff --git a/add_group_form.py b/add_group_form.py index dc83b89..f667b66 100644 --- a/add_group_form.py +++ b/add_group_form.py @@ -67,7 +67,6 @@ class addGroupForm(QDialog): return new_id = DB.insertGroup(Group(0, name_text, column_text, link_text)) - # TODO do the database stuff (this will allow us to get the id) Globals.groups.append(Group(new_id, name_text, column_text, link_text)) self.close() diff --git a/assignment-list.py b/assignment-list.py index 0fac895..a09a448 100755 --- a/assignment-list.py +++ b/assignment-list.py @@ -50,7 +50,6 @@ class AssignmentList(QMainWindow): tool_bar.addAction(self.add_group_act) def setupDB(self): - # TODO should check for existing db instead of blindly creating it DB.initDB() def displayWidgets(self): @@ -66,8 +65,6 @@ class AssignmentList(QMainWindow): title_h_box.addWidget(title) title_h_box.addStretch() - self.load_groups() - self.load_entries() # TODO placeholder, this will eventually be moved to group.py self.groups_hbox = QHBoxLayout() self.groups_hbox.setContentsMargins(20, 5, 20, 5) self.drawGroups() @@ -186,26 +183,6 @@ class AssignmentList(QMainWindow): QMessageBox.about(self, "About Assignment List", "Created by Louie S. - 2023") - def load_groups(self): - """ - Load groups data - """ - # TODO - #Globals.groups.append(Group(1, "test1", "left")) - #Globals.groups.append(Group(2, "test2", "left")) - #Globals.groups.append(Group(3, "test3", "right")) - #Globals.groups.append(Group(4, "test4", "right")) - pass - - def load_entries(self): - """ - Load entries data - """ - # TODO - #Globals.entries.append(Entry(1, "test1-task1")) - #Globals.entries.append(Entry(2, "test2-task1")) - pass - if __name__ == "__main__": app = QApplication(sys.argv) window = AssignmentList() diff --git a/db_sqlite.py b/db_sqlite.py index 99f412b..2f20c91 100644 --- a/db_sqlite.py +++ b/db_sqlite.py @@ -1,8 +1,11 @@ import os import sys +from time import strptime +from PyQt5.QtCore import QDate from PyQt5.QtSql import QSqlDatabase, QSqlQuery Globals = __import__("globals") from group import Group +from entry import Entry def initDB(): """ @@ -67,6 +70,7 @@ def loadFromTables(): query = QSqlQuery() + # Load groups query.exec_("SELECT * FROM groups") while query.next(): record = query.record() @@ -77,11 +81,27 @@ def loadFromTables(): record.field("column").value(), record.field("link").value())) + # Load entries + query.exec_("SELECT * FROM entries") + while query.next(): + record = query.record() + due_date_struct = strptime(record.field("due_date").value(), "%Y-%m-%d") + Globals.entries.append( + Entry( + record.field("id").value(), + record.field("parent_id").value(), + record.field("description").value(), + QDate(due_date_struct.tm_year, due_date_struct.tm_mon, due_date_struct.tm_mday), + record.field("alt_due_date").value(), + record.field("link").value(), + record.field("done").value(), + record.field("hidden").value())) + database.close() def insertGroup(new_group): """ - Save groups and entries to the database at Globals.db_path + Insert group to the database at Globals.db_path """ output = -1 @@ -107,3 +127,44 @@ def insertGroup(new_group): database.close() return output + +def insertEntry(new_entry): + """ + Insert entry to the database at Globals.db_path + """ + output = -1 + + database = QSqlDatabase.addDatabase("QSQLITE") # SQlite version 3 + database.setDatabaseName(Globals.db_path) + + if not database.open(): + print("Unable to open data source file.") + sys.exit(1) # Error out. TODO consider throwing a dialog instead + + query = QSqlQuery() + + query.prepare(""" + INSERT INTO entries (parent_id, description, due_date, alt_due_date, link) VALUES (:p_id, :desc, :due, :alt_due, :link) + """) + query.bindValue(":p_id", new_entry.parent_id) + query.bindValue(":desc", new_entry.desc) + query.bindValue(":due", "{0}-{1}-{2}".format( + new_entry.due.year(), + new_entry.due.month(), + new_entry.due.day())) + query.bindValue(":alt_due", new_entry.due_alt) + query.bindValue(":link", new_entry.link) + success = query.exec_() + # DEBUG + #print(query.lastError().text()) + #print(query.boundValues()) + #if success: + # print("Query succeeded") + #else: + # print("Query failed") + + output = query.lastInsertId() + + database.close() + + return output diff --git a/entry.py b/entry.py index 2c01b3f..df3f566 100644 --- a/entry.py +++ b/entry.py @@ -4,13 +4,15 @@ from PyQt5.QtGui import QFont from PyQt5.QtWidgets import QLabel class Entry: - def __init__(self, parent_id, desc, due = "", due_alt = "", link = ""): + def __init__(self, id, parent_id, desc, due = "", due_alt = "", link = "", done = False, hidden = False): + self.id = id self.parent_id = parent_id self.desc = desc self.due = due self.due_alt = due_alt self.link = link - self.done = False + self.done = done + self.hidden = hidden def buildLayout(self): output = QLabel() @@ -19,7 +21,7 @@ class Entry: output.setText("- ") if(self.due): - output.setText(output.text() + time.strftime("%m/%d/%y: ")) + output.setText(output.text() + "{0}/{1}/{2}: ".format(self.due.month(), self.due.day(), self.due.year())) output.setText(output.text() + self.desc) return output -- cgit