From 51623bd23d5809fe87590711218a94dd2be582d9 Mon Sep 17 00:00:00 2001 From: Louie S Date: Sat, 16 Sep 2023 09:36:14 -0400 Subject: Remove Entry working --- assignment-list.py | 34 +++++++++++++++++++++++++++------- db_sqlite.py | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/assignment-list.py b/assignment-list.py index a330956..059da14 100755 --- a/assignment-list.py +++ b/assignment-list.py @@ -98,10 +98,11 @@ class AssignmentList(QMainWindow): """ # TODO might want to add a warning # TODO might want to make part of the a destructor in the Group class - DB.removeGroup(id) - Globals.entries = list(filter(lambda e: e.parent_id != id, Globals.entries)) - Globals.groups = list(filter(lambda g: g.id != id, Globals.groups)) - self.drawGroups() + removed = DB.removeGroup(id) + if removed > 0: + Globals.entries = list(filter(lambda e: e.parent_id != id, Globals.entries)) + Globals.groups = list(filter(lambda g: g.id != id, Globals.groups)) + self.drawGroups() def addEntry(self, parent): """ @@ -116,8 +117,12 @@ class AssignmentList(QMainWindow): """ Delete an entry with a given id """ - # TODO implement when db is implemented, and entries have ids - pass + # TODO might want to add a warning + # TODO might want to make part of the a destructor in the Entry class + removed = DB.removeEntry(id) + if removed > 0: + Globals.entries = list(filter(lambda e: e.id != id, Globals.entries)) + self.drawGroups() def drawGroups(self): """ @@ -186,13 +191,28 @@ class AssignmentList(QMainWindow): """ Redraw the entries of a specific group """ + # TODO consider having code to remove existing widgets to make this function more modular entries = list(filter(lambda e: e.parent_id == group_id, Globals.entries)) entries_vbox = QVBoxLayout() entries_vbox.setContentsMargins(5, 0, 0, 0) for e in entries: + # skip if this entry is set to hidden + if e.hidden: + continue + entries_vbox.addWidget(e.buildLayout()) - # TODO find a good way to add modifier buttons + + # entry modifier buttons + buttons_hbox = QHBoxLayout() + + del_entry_button = QPushButton() + del_entry_button.setText("Remove Entry") + del_entry_button.clicked.connect((lambda id: lambda: self.removeEntry(id))(e.id)) + buttons_hbox.addWidget(del_entry_button) + + buttons_hbox.addStretch() + entries_vbox.addLayout(buttons_hbox) return entries_vbox diff --git a/db_sqlite.py b/db_sqlite.py index deef984..01f771d 100644 --- a/db_sqlite.py +++ b/db_sqlite.py @@ -223,4 +223,31 @@ def removeGroup(group_id): query.addBindValue(group_id) query.exec_() + output = query.numRowsAffected() database.close() + return output + +def removeEntry(entry_id): + """ + Remove a group by id from the database + (actually set hidden to true, don't permanently delete it) + """ + 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() + + # Set entry to hidden + query.prepare(""" + UPDATE entries SET hidden = 1 WHERE id = ? + """) + query.addBindValue(entry_id) + query.exec_() + + output = query.numRowsAffected() + database.close() + return output -- cgit