From 3ded989fee52f6804d0f8b5b5c0847a9030c7f04 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Thu, 2 Dec 2010 11:34:54 +0530 Subject: [PATCH] Added some error checks for sanity Added some error checks for sanity. * Check whether database is valid or not. If not valid, then simply reset current database to None so that no other dialog can be invoked. * Check for articles existing before exporting. Makes no sense to create a website without any articles. * Other checks to avoid exceptions when invalid database is loaded. --- biaweb_exporter.py | 6 ++++++ generate_dialog.py | 9 +++++++++ main_window.py | 26 +++++++++++++++++++++----- templates_dialog.py | 14 +++++++++++--- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/biaweb_exporter.py b/biaweb_exporter.py index 9531159..52d9a02 100644 --- a/biaweb_exporter.py +++ b/biaweb_exporter.py @@ -416,8 +416,14 @@ def generate_home_page (dbname, conf, templates, category_str, bestrated_str): def generate_site (dbname, files_to_copy, folders_to_copy, search_type_full=True): # get the configuration conf = biaweb_db.get_configuration (dbname) + # if cannot get configuration + if conf == False: + return False + # get the templates tpls = biaweb_db.get_templates (dbname) + if tpls == False: + return False # get the list of categories cats = biaweb_db.get_categories (dbname) diff --git a/generate_dialog.py b/generate_dialog.py index e236451..bd20db8 100644 --- a/generate_dialog.py +++ b/generate_dialog.py @@ -6,6 +6,7 @@ import sys import os import os.path import ui_generate_dialog +import biaweb_db import biaweb_exporter class GenerateDialog (PyQt4.QtGui.QDialog, ui_generate_dialog.Ui_SiteGenerateDialog): @@ -74,6 +75,14 @@ class GenerateDialog (PyQt4.QtGui.QDialog, ui_generate_dialog.Ui_SiteGenerateDia # when site generate button is clicked def onSiteGenerate (self): + # first check whether there are any articles in the website. If no articles + # are found, then website cannot be generated + arts = biaweb_db.get_articles (self.current_db) + # if no articles are found or cannot be retrieved + if arts == False or len (arts) == 0: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "Cannot create website with no articles") + return + files_list = self.get_list_from_tree (self.additional_files) folder_list = self.get_list_from_tree (self.additional_folders) if self.fulltextindex.isChecked (): diff --git a/main_window.py b/main_window.py index 86e9b12..8963536 100644 --- a/main_window.py +++ b/main_window.py @@ -25,13 +25,16 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # check with False because None is returned when no records exist if recs == False: PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories") - return + return False self.categories.clear () for (id, name, desc, stub) in recs: qrow = PyQt4.QtGui.QTreeWidgetItem ([str(id), str(name)]) self.categories.addTopLevelItem (qrow) + # return true when successful + return True + # refresh the articles list def repopulate_articles (self, catid=None): recs = biaweb_db.get_articles (self.current_db, catid) @@ -39,12 +42,13 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): # check with False because None is returned when no records exist if recs == False: PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles") - return + return False self.articles.clear () for item in recs: qrow = PyQt4.QtGui.QTreeWidgetItem ([str(item[0]), str(item[1])]) self.articles.addTopLevelItem (qrow) + # return true when successful # when the view is refreshed def onViewRefresh (self): @@ -330,9 +334,21 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database") if filename: self.current_db = str (filename) - self.setWindowTitle ("BiaWeb - " + self.current_db) - self.repopulate_categories () - self.repopulate_articles () + + # added to check whether categories are loaded successfully or not + # if not, then reset the current_db to None + loaded_cats = self.repopulate_categories () + loaded_arts = self.repopulate_articles () + # if failed in loading either categories or articles (note: checking against + # False and not None) + if loaded_cats == False or loaded_arts == False: + self.current_db = None + PyQt4.QtGui.QMessageBox.critical (self, "Error", + "SQLite 3 error in loading site database. This appears to be an invalid BiaWeb database") + else: + # set the window title to the database + self.setWindowTitle ("BiaWeb - " + self.current_db) + # file new menu is clicked def onFileNew (self): diff --git a/templates_dialog.py b/templates_dialog.py index 64cc70c..20f2e18 100644 --- a/templates_dialog.py +++ b/templates_dialog.py @@ -16,12 +16,20 @@ class TemplatesDialog (PyQt4.QtGui.QDialog, ui_templates_dialog.Ui_TemplatesDial # populate the templates tpls = biaweb_db.get_templates (self.current_db) - for tpl_name, tpl_content in tpls: - self.templates.addItem (str (tpl_name)) + # if templates populated + if tpls: + for tpl_name, tpl_content in tpls: + self.templates.addItem (str (tpl_name)) + else: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in retrieving templates") def onEdit (self): # get the currently selected list item - tpl_name = str (self.templates.currentItem ().data (0).toString ()) + sel_item = self.templates.currentItem () + # if no item is selected return + if sel_item is None: + return + tpl_name = str (sel_item.data (0).toString ()) # get the template content from the database tpl_str = biaweb_db.get_template_text (self.current_db, tpl_name) -- 2.20.1