X-Git-Url: https://harishankar.org/repos/?p=biaweb_qt.git;a=blobdiff_plain;f=main_window.py;h=6f033d6af2cb88fd19500cc268dc41f5b3aa6f6b;hp=0d6c91a5d96036ed4690a2091e88e624cc3be40a;hb=e897998f0634d3518f069846c62592fe43dc63bf;hpb=e14aab0777f4dcc7200894ee75fab329007adb53 diff --git a/main_window.py b/main_window.py index 0d6c91a..6f033d6 100644 --- a/main_window.py +++ b/main_window.py @@ -5,6 +5,7 @@ import PyQt4 import ui_main_window import site_configuration_dialog as scd import category_dialog as catd +import article_dialog as artd import biaweb_db import sys @@ -17,7 +18,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # refresh the category list def repopulate_categories (self): recs = biaweb_db.get_categories (self.current_db) - if recs == False: + if not recs: PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories") return @@ -29,7 +30,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # refresh the articles list def repopulate_articles (self, catid=None): recs = biaweb_db.get_articles (self.current_db, catid) - if recs == False: + if not recs: PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles") return @@ -44,6 +45,127 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): self.repopulate_categories () self.repopulate_articles () + # when add article is triggered + def onArticleAdd (self): + if self.current_db is None: + PyQt4.QtGui.QMessageBox.critical (self, "Error", + "Cannot create article. You need to create or open a website first") + else: + catid = self.get_selected_item_id (self.categories) + # if no category is selected + if catid is None: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected for article") + return + artdlg = artd.ArticleDialog (self) + cats = biaweb_db.get_categories (self.current_db) + + artdlg.populate_categories (cats, catid) + + # if OK is pressed + if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted: + title = str (artdlg.article_title.text ()).strip () + keywords = str (artdlg.keywords.text ()).strip () + summary = str (artdlg.summary.toPlainText ()).strip () + content = str (artdlg.content.toPlainText ()).strip () + cid, ok = artdlg.category.itemData (artdlg.category.currentIndex ()).toInt () + # if catid is not an integer then abort + if not ok: + return + rating = artdlg.rating.value () + stub = str (artdlg.stub.text ()).strip () + + ret = biaweb_db.create_article (self.current_db, title, summary, keywords, content, cid, + stub, rating) + if ret: + PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully created") + self.repopulate_categories () + self.repopulate_articles (catid) + else: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating article") + + # when edit article is triggered + def onArticleEdit (self): + if self.current_db is None: + PyQt4.QtGui.QMessageBox.critical (self, "Error", + "Cannot edit article. You need to create or open a website first") + else: + # get the selected article + artid = self.get_selected_item_id (self.articles) + catid = self.get_selected_item_id (self.categories) + + # no article is selected + if artid is None: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected") + return + article = biaweb_db.get_article (self.current_db, artid) + # if article cannot be read + if not article: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading article") + return + artdlg = artd.ArticleDialog (self) + + artdlg.article_title.setText (article[1]) + artdlg.summary.setPlainText (article[2]) + artdlg.keywords.setText (article[3]) + artdlg.content.setPlainText (article[4]) + # populate category combo box and set active category to the article's category + cats = biaweb_db.get_categories (self.current_db) + artdlg.populate_categories (cats, article[7]) + artdlg.stub.setText (article[8]) + artdlg.rating.setValue (article[9]) + + # ok is pressed + if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted: + title = str (artdlg.article_title.text ()).strip () + summary = str (artdlg.summary.toPlainText ()).strip () + keywords = str (artdlg.keywords.text ()).strip () + content = str (artdlg.content.toPlainText ()).strip () + cid, ok = artdlg.category.itemData (artdlg.category.currentIndex()).toInt () + # if cat id is not an integer + if not ok: + return + rating = artdlg.rating.value () + stub = str (artdlg.stub.text ()).strip () + ret = biaweb_db.update_article (self.current_db, artid, title, + summary, keywords, content, cid, stub, rating) + if ret: + PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully updated") + self.repopulate_articles (catid) + else: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating article") + + # when delete article is triggered + def onArticleDelete (self): + if self.current_db is None: + PyQt4.QtGui.QMessageBox.critical (self, "Error", + "Cannot delete article. You need to create or open a website first") + else: + # get the selected article + artid = self.get_selected_item_id (self.articles) + catid = self.get_selected_item_id (self.categories) + if artid is None: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected") + return + # get confirmation on delete + flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm", + "Are you sure you wish to delete the selected article?", + PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No) + # confirmed + if flag == PyQt4.QtGui.QMessageBox.Yes: + ret = biaweb_db.delete_article (self.current_db, artid) + if not ret: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting article") + else: + self.repopulate_articles (catid) + + # when category item is activated + def onCategoryItemActivated (self): + self.onCategoryEdit () + + # when article item is activated + def onArticleItemActivated (self): + self.onArticleEdit () + # when item selection is changed in categories tree widget def onCategorySelectionChanged (self): if self.current_db is not None: @@ -52,14 +174,14 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # when configuration menu is activated def onConfiguration (self): - if self.current_db == None: + if self.current_db is None: PyQt4.QtGui.QMessageBox.critical (self, "Error", "Cannot edit configuration. You need to create or open a website first") else: dlg = scd.SiteConfigDialog (self) configs = biaweb_db.get_configuration (self.current_db) - if configs == False: + if not configs: PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration") return @@ -84,7 +206,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # database update flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description, copyright, num_rss, destination) - if flag == False: + if not flag: PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating configuration") @@ -101,7 +223,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # category edit action def onCategoryEdit (self): # if no database is open - if self.current_db == None: + if self.current_db is None: PyQt4.QtGui.QMessageBox.critical (self, "Error", "Cannot edit category. You need to create or open a website first") # database is open @@ -114,8 +236,8 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): else: cat = biaweb_db.get_category (self.current_db, catid) # if the category cannot be retrieved from database - if cat == False: - PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error reading category") + if not cat: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading category") return # set the data in the dialog @@ -132,8 +254,9 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): ret = biaweb_db.update_category (self.current_db, catid, category_name, category_desc, category_stub) - if ret == True: + if ret: PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated") + self.repopulate_categories () else: PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category") @@ -141,9 +264,9 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # category delete action def onCategoryDelete (self): # if there is no database - if self.current_db == None: + if self.current_db is None: PyQt4.QtGui.QMessageBox.critical (self, "Error", - "Cannot edit category. You need to create or open a website first") + "Cannot delete category. You need to create or open a website first") else: # get the selected category catid = self.get_selected_item_id (self.categories) @@ -158,7 +281,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # if confirmed if flag == PyQt4.QtGui.QMessageBox.Yes: ret = biaweb_db.remove_category (self.current_db, catid) - if ret == False: + if not ret: PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting category") else: self.repopulate_categories () @@ -167,7 +290,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # category add action def onCategoryAdd (self): # if there is no database - if self.current_db == None: + if self.current_db is None: PyQt4.QtGui.QMessageBox.critical (self, "Error", "Cannot add category. You need to create or open a website first") else: @@ -179,7 +302,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): cat_desc = str (dlg.category_desc.text ()).strip () cat_stub = str (dlg.category_stub.text ()).strip () ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub) - if ret == True: + if ret: PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created") self.repopulate_categories () self.repopulate_articles () @@ -216,7 +339,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): self.setWindowTitle ("BiaWeb - " + self.current_db) flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description, copyright, num_rss, destination) - if flag == True: + if flag: PyQt4.QtGui.QMessageBox.information (self, "Success", "New site db successfully created") self.articles.clear ()