Added some error checks for sanity
[biaweb_qt.git] / generate_dialog.py
index d1882d2..bd20db8 100644 (file)
@@ -2,18 +2,57 @@
 # Generate site dialog
 
 import PyQt4
+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):
        def __init__ (self, master, currentdb):
                PyQt4.QtGui.QDialog.__init__ (self, master)
                self.setupUi (self)
 
+               # update the status of required additional files to be copied to
+               # the destination directory and warn the user accordingly
+               self.set_required_files_status ()
+
                # set the database
                self.current_db = currentdb
 
+       # function to set the status message of the required additional files to be copied to
+       # the destination directory. If any required additional file is missing in the script
+       # path then warn the user to copy those files manually to the destination directory
+       def set_required_files_status (self):
+               search_script_path = os.path.join (sys.path[0], "search.py")
+               star_image_path = os.path.join (sys.path[0], "star.gif")
+               stargrey_image_path = os.path.join (sys.path[0], "star-grey.gif")
+
+               # if search.py exists in script directory
+               if os.path.exists (search_script_path):
+                       self.status_search_py.setText ('<span style="color:darkgreen;">will be \
+                       automatically copied to destination dir</span>')
+               else:
+                       self.status_search_py.setText ('<span style="color:darkred;">cannot be found.\
+                       You must copy it manually to cgi-bin</span>')
+
+               # if star.gif exists in script directory
+               if os.path.exists (star_image_path):
+                       self.status_star_gif.setText ('<span style="color:darkgreen;">will be \
+                       automatically copied to destination dir</span>')
+               else:
+                       self.status_star_gif.setText ('<span style="color:darkred;">Cannot be found. \
+                       You must copy it manually to destination dir</span')
+
+               # if star-grey.gif exists in script directory
+               if os.path.exists (stargrey_image_path):
+                       self.status_stargrey_gif.setText ('<span style="color:darkgreen;">will be \
+                       automatically copied to destination dir</span>')
+               else:
+                       self.status_stargrey_gif.setText ('<span style="color:darkred;">cannot be found. \
+                       You must copy it manually to destination dir</span')
+
        # to return the list of items from a tree widget
        def get_list_from_tree (self, treewidget):
                lstfiles = []
@@ -36,8 +75,39 @@ 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 ():
+                       searchtype = True
+               else:
+                       searchtype = False
+
+               # confirm whether to delete the destination tree and work afresh
+               ans = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
+                                       "This will delete the destination tree completely \
+and recreate the website. Are you sure you wish to proceed?",
+                               PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
+
+               # if confirmed
+               if ans == PyQt4.QtGui.QMessageBox.Yes:
+                       # call the main exporter to do our work
+                       ret = biaweb_exporter.generate_site (self.current_db, files_list, folder_list, searchtype)
+
+                       # if failed to generate site or any part thereof
+                       if ret == False:
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                                       "System or SQLite 3 error in generating website or parts thereof")
+                       else:
+                               PyQt4.QtGui.QMessageBox.information (self, "Success",
+                                                       "Successfully generated website in destination path!")
 
        # when folder add is clicked
        def onFolderAdd (self):