1 # BiaWeb Website content manager (c) 2010 V.Harishankar
8 import ui_generate_dialog
10 import biaweb_exporter
12 class GenerateDialog (PyQt4
.QtGui
.QDialog
, ui_generate_dialog
.Ui_SiteGenerateDialog
):
13 def __init__ (self
, master
, currentdb
):
14 PyQt4
.QtGui
.QDialog
.__init
__ (self
, master
)
17 # update the status of required additional files to be copied to
18 # the destination directory and warn the user accordingly
19 self
.set_required_files_status ()
22 self
.current_db
= currentdb
24 # function to set the status message of the required additional files to be copied to
25 # the destination directory. If any required additional file is missing in the script
26 # path then warn the user to copy those files manually to the destination directory
27 def set_required_files_status (self
):
28 search_script_path
= os
.path
.join (sys
.path
[0], "search.py")
29 star_image_path
= os
.path
.join (sys
.path
[0], "star.gif")
30 stargrey_image_path
= os
.path
.join (sys
.path
[0], "star-grey.gif")
32 # if search.py exists in script directory
33 if os
.path
.exists (search_script_path
):
34 self
.status_search_py
.setText ('<span style="color:darkgreen;">will be \
35 automatically copied to destination dir</span>')
37 self
.status_search_py
.setText ('<span style="color:darkred;">cannot be found.\
38 You must copy it manually to cgi-bin</span>')
40 # if star.gif exists in script directory
41 if os
.path
.exists (star_image_path
):
42 self
.status_star_gif
.setText ('<span style="color:darkgreen;">will be \
43 automatically copied to destination dir</span>')
45 self
.status_star_gif
.setText ('<span style="color:darkred;">Cannot be found. \
46 You must copy it manually to destination dir</span')
48 # if star-grey.gif exists in script directory
49 if os
.path
.exists (stargrey_image_path
):
50 self
.status_stargrey_gif
.setText ('<span style="color:darkgreen;">will be \
51 automatically copied to destination dir</span>')
53 self
.status_stargrey_gif
.setText ('<span style="color:darkred;">cannot be found. \
54 You must copy it manually to destination dir</span')
56 # to return the list of items from a tree widget
57 def get_list_from_tree (self
, treewidget
):
59 iter = PyQt4
.QtGui
.QTreeWidgetItemIterator (treewidget
)
61 # iterate through the list of files
63 # while there is still a valid item
65 # get the text in columns 0 and 1
66 src
= str (item
.text (0))
67 dst
= str (item
.text (1))
69 lstfiles
.append ( (src
, dst
) )
70 # increase iterator by 1
76 # when site generate button is clicked
77 def onSiteGenerate (self
):
78 # first check whether there are any articles in the website. If no articles
79 # are found, then website cannot be generated
80 arts
= biaweb_db
.get_articles (self
.current_db
)
81 # if no articles are found or cannot be retrieved
82 if arts
== False or len (arts
) == 0:
83 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "Cannot create website with no articles")
86 files_list
= self
.get_list_from_tree (self
.additional_files
)
87 folder_list
= self
.get_list_from_tree (self
.additional_folders
)
88 if self
.fulltextindex
.isChecked ():
93 # confirm whether to delete the destination tree and work afresh
94 ans
= PyQt4
.QtGui
.QMessageBox
.question (self
, "Confirm",
95 "This will delete the destination tree completely \
96 and recreate the website. Are you sure you wish to proceed?",
97 PyQt4
.QtGui
.QMessageBox
.Yes
, PyQt4
.QtGui
.QMessageBox
.No
)
100 if ans
== PyQt4
.QtGui
.QMessageBox
.Yes
:
101 # call the main exporter to do our work
102 ret
= biaweb_exporter
.generate_site (self
.current_db
, files_list
, folder_list
, searchtype
)
104 # if failed to generate site or any part thereof
106 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
107 "System or SQLite 3 error in generating website or parts thereof")
109 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
110 "Successfully generated website in destination path!")
112 # when folder add is clicked
113 def onFolderAdd (self
):
114 folder_to_add
= PyQt4
.QtGui
.QFileDialog
.getExistingDirectory (self
,
115 "Folder whose contents to add to destination", os
.path
.expanduser ("~"))
116 # if cancelled return
117 if not folder_to_add
:
120 dest_rel_path
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, "Desination",
121 "Copy to location (within destination path)")
125 folderitem
= PyQt4
.QtGui
.QTreeWidgetItem ([folder_to_add
, dest_rel_path
])
126 self
.additional_folders
.addTopLevelItem (folderitem
)
128 # when folder remove is clicked
129 def onFolderRemove (self
):
130 selitem
= self
.additional_folders
.currentItem ()
131 selindex
= self
.additional_folders
.indexOfTopLevelItem (selitem
)
135 self
.additional_folders
.takeTopLevelItem (selindex
)
138 # when file add is clicked
139 def onFileAdd (self
):
140 file_to_add
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "File to add to destination",
141 os
.path
.expanduser ("~"))
142 # if cancelled return
145 dest_rel_path
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, "Destination",
146 "Copy to location (within destination path)")
150 fileitem
= PyQt4
.QtGui
.QTreeWidgetItem ([file_to_add
, dest_rel_path
])
151 self
.additional_files
.addTopLevelItem (fileitem
)
153 # when file remove is clicked
154 def onFileRemove (self
):
155 selitem
= self
.additional_files
.currentItem ()
156 selindex
= self
.additional_files
.indexOfTopLevelItem (selitem
)
160 self
.additional_files
.takeTopLevelItem (selindex
)