From 8e288c4b141df0805f63450079f6a0f5cf9e8f37 Mon Sep 17 00:00:00 2001 From: Louie S Date: Sat, 16 Sep 2023 09:36:14 -0400 Subject: edit entries working --- assignment-list.py | 22 ++++++++++--- db_sqlite.py | 42 ++++++++++++++++++++++-- edit_entry_form.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ edit_group_form.py | 6 ++-- 4 files changed, 156 insertions(+), 10 deletions(-) create mode 100644 edit_entry_form.py diff --git a/assignment-list.py b/assignment-list.py index 059da14..6c9cfbd 100755 --- a/assignment-list.py +++ b/assignment-list.py @@ -7,6 +7,7 @@ from PyQt5.QtCore import Qt from add_group_form import addGroupForm from edit_group_form import editGroupForm from add_entry_form import addEntryForm +from edit_entry_form import editEntryForm Globals = __import__("globals") DB = __import__("db_sqlite") @@ -92,6 +93,13 @@ class AssignmentList(QMainWindow): self.create_edit_group_dialog = editGroupForm(id) self.drawGroups() + def editEntry(self, id): + """ + Open the 'editEntry' form + """ + self.create_edit_entry_dialog = editEntryForm(id) + self.drawGroups() + def removeGroup(self, id): """ Delete a group with a given id @@ -161,10 +169,10 @@ class AssignmentList(QMainWindow): add_entry_button.clicked.connect((lambda id: lambda: self.addEntry(id))(g.id)) buttons_hbox.addWidget(add_entry_button) - add_entry_button = QPushButton() - add_entry_button.setText("Edit Group") - add_entry_button.clicked.connect((lambda id: lambda: self.editGroup(id))(g.id)) - buttons_hbox.addWidget(add_entry_button) + edit_group_button = QPushButton() + edit_group_button.setText("Edit Group") + edit_group_button.clicked.connect((lambda id: lambda: self.editGroup(id))(g.id)) + buttons_hbox.addWidget(edit_group_button) del_group_button = QPushButton() del_group_button.setText("Remove Group") @@ -186,7 +194,6 @@ class AssignmentList(QMainWindow): self.groups_hbox.addLayout(column_right) self.groups_hbox.addStretch() - # Implementation should be moved here from group.py if possible def drawEntries(self, group_id): """ Redraw the entries of a specific group @@ -206,6 +213,11 @@ class AssignmentList(QMainWindow): # entry modifier buttons buttons_hbox = QHBoxLayout() + edit_entry_button = QPushButton() + edit_entry_button.setText("Edit Entry") + edit_entry_button.clicked.connect((lambda id: lambda: self.editEntry(id))(e.id)) + buttons_hbox.addWidget(edit_entry_button) + del_entry_button = QPushButton() del_entry_button.setText("Remove Entry") del_entry_button.clicked.connect((lambda id: lambda: self.removeEntry(id))(e.id)) diff --git a/db_sqlite.py b/db_sqlite.py index 01f771d..4c4cf0d 100644 --- a/db_sqlite.py +++ b/db_sqlite.py @@ -172,7 +172,7 @@ def insertEntry(new_entry): def updateGroup(group): """ - Update group by group_id + Update group by its id """ database = QSqlDatabase.addDatabase("QSQLITE") # SQlite version 3 database.setDatabaseName(Globals.db_path) @@ -184,16 +184,54 @@ def updateGroup(group): query = QSqlQuery() query.prepare(""" - UPDATE groups SET name = ?, column = ?, link = ? WHERE id = ? + UPDATE groups SET name = ?, column = ?, link = ?, hidden = ? WHERE id = ? """) query.addBindValue(group.name) query.addBindValue(group.column) query.addBindValue(group.link) + query.addBindValue(group.hidden) query.addBindValue(group.id) query.exec_() database.close() +def updateEntry(entry): + """ + Update entry by its id + """ + 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(""" + UPDATE entries SET + description = :desc, + due_date = :due, + alt_due_date = :alt_due, + link = :link, + done = :done, + hidden = :hidden + WHERE id = :id + """) + query.bindValue(":desc", entry.desc) + query.bindValue(":due", "{0}-{1}-{2}".format( + entry.due.year(), + entry.due.month(), + entry.due.day())) + query.bindValue(":alt_due", entry.due_alt) + query.bindValue(":link", entry.link) + query.bindValue(":done", entry.done) + query.bindValue(":hidden", entry.hidden) + query.bindValue(":id", entry.id) + query.exec_() + + database.close() + def removeGroup(group_id): """ Remove a group by id from the database diff --git a/edit_entry_form.py b/edit_entry_form.py new file mode 100644 index 0000000..7af95dd --- /dev/null +++ b/edit_entry_form.py @@ -0,0 +1,96 @@ +import sys +from PyQt5.QtWidgets import QApplication, QDateTimeEdit, QDialog, QFormLayout, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton +from PyQt5.QtGui import QFont +from PyQt5.QtCore import Qt + +Globals = __import__("globals") +from entry import Entry +DB = __import__("db_sqlite") + +class editEntryForm(QDialog): + """ + Form to edit/update an entry + """ + def __init__(self, id): + self.id = id + super().__init__() + self.initializeUI() + + def initializeUI(self): + self.resize(400, 1) + self.setWindowTitle("Edit Entry") + self.displayWidgets() + self.exec() + + def displayWidgets(self): + entry_form_layout = QFormLayout() + entry = list(filter(lambda e: e.id == self.id, Globals.entries))[0] + + title = QLabel("Edit Entry") + title.setFont(QFont("Arial", 18)) + title.setAlignment(Qt.AlignCenter) + entry_form_layout.addRow(title) + + self.entry_desc = QLineEdit() + self.entry_desc.setText(entry.desc) + entry_form_layout.addRow("Description:", self.entry_desc) + + self.entry_due = QDateTimeEdit() + self.entry_due.setDisplayFormat("MM/dd/yyyy") + self.entry_due.setDate(entry.due) + entry_form_layout.addRow("Due Date:", self.entry_due) + + self.entry_due_alt = QLineEdit() + self.entry_due_alt.setText(entry.due_alt) + entry_form_layout.addRow("Due Date (Alt):", self.entry_due_alt) + + self.entry_link = QLineEdit() # TODO see if there is a widget specifically for URLs + self.entry_link.setText(entry.link) + entry_form_layout.addRow("Link:", self.entry_link) + + # Submit and cancel buttons + buttons_h_box = QHBoxLayout() + buttons_h_box.addStretch() + close_button = QPushButton("Cancel") + close_button.clicked.connect(self.close) + buttons_h_box.addWidget(close_button) + submit_button = QPushButton("Submit") + submit_button.clicked.connect(self.handleSubmit) + buttons_h_box.addWidget(submit_button) + buttons_h_box.addStretch() + + entry_form_layout.addRow(buttons_h_box) + + self.setLayout(entry_form_layout) + + def handleSubmit(self): + desc_text = self.entry_desc.text() + due_text = self.entry_due.date() # due_text is a QDate + due_alt_text = self.entry_due_alt.text() + link_text = self.entry_link.text() + + if not desc_text: + QMessageBox.warning(self, "Error Message", + "Name cannot be blank", + QMessageBox.Close, + QMessageBox.Close) + return + + # Update DB + entry = list(filter(lambda e: e.id == self.id, Globals.entries))[0] + entry.desc = desc_text + entry.due = due_text + entry.due_alt = due_alt_text + entry.link = link_text + DB.updateEntry(entry) + + # Update global variables + Globals.entries = list(filter(lambda e: e.id != self.id, Globals.entries)) + Globals.entries.append(Entry(self.id, entry.parent_id, desc_text, due_text, due_alt_text, link_text, entry.done, entry.hidden)) + self.close() + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = editEntryForm() + sys.exit(app.exec_()) + diff --git a/edit_group_form.py b/edit_group_form.py index aba5559..faa676c 100644 --- a/edit_group_form.py +++ b/edit_group_form.py @@ -3,13 +3,13 @@ from PyQt5.QtWidgets import QApplication, QComboBox, QDialog, QFormLayout, QHBox from PyQt5.QtGui import QFont from PyQt5.QtCore import Qt -from add_entry_form import Globals +Globals = __import__("globals") from group import Group DB = __import__("db_sqlite") class editGroupForm(QDialog): """ - Implemented so that it can be used for adding and editing groups + Form to edit/update a group """ def __init__(self, id): self.id = id @@ -80,7 +80,7 @@ class editGroupForm(QDialog): # Update global variables Globals.groups = list(filter(lambda g: g.id != self.id, Globals.groups)) - Globals.groups.append(Group(self.id, name_text, column_text, link_text)) + Globals.groups.append(Group(self.id, name_text, column_text, link_text, group.hidden)) self.close() if __name__ == "__main__": -- cgit