Updated htaccess to Apache 2.4 compat
[biaweb_qt.git] / generate_dialog.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Generate site dialog
3
4 import PyQt4
5 import sys
6 import os
7 import os.path
8 import ui_generate_dialog
9 import biaweb_db
10 import biaweb_exporter
11
12 class GenerateDialog (PyQt4.QtGui.QDialog, ui_generate_dialog.Ui_SiteGenerateDialog):
13 def __init__ (self, master, currentdb):
14 PyQt4.QtGui.QDialog.__init__ (self, master)
15 self.setupUi (self)
16
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 ()
20
21 # set the database
22 self.current_db = currentdb
23
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")
31
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>')
36 else:
37 self.status_search_py.setText ('<span style="color:darkred;">cannot be found.\
38 You must copy it manually to cgi-bin</span>')
39
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>')
44 else:
45 self.status_star_gif.setText ('<span style="color:darkred;">Cannot be found. \
46 You must copy it manually to destination dir</span')
47
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>')
52 else:
53 self.status_stargrey_gif.setText ('<span style="color:darkred;">cannot be found. \
54 You must copy it manually to destination dir</span')
55
56 # to return the list of items from a tree widget
57 def get_list_from_tree (self, treewidget):
58 lstfiles = []
59 iter = PyQt4.QtGui.QTreeWidgetItemIterator (treewidget)
60
61 # iterate through the list of files
62 item = iter.value ()
63 # while there is still a valid item
64 while item:
65 # get the text in columns 0 and 1
66 src = str (item.text (0))
67 dst = str (item.text (1))
68 # add it to the list
69 lstfiles.append ( (src, dst) )
70 # increase iterator by 1
71 iter += 1
72 item = iter.value ()
73
74 return lstfiles
75
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")
84 return
85
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 ():
89 searchtype = True
90 else:
91 searchtype = False
92
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)
98
99 # if confirmed
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)
103
104 # if failed to generate site or any part thereof
105 if ret == False:
106 PyQt4.QtGui.QMessageBox.critical (self, "Error",
107 "System or SQLite 3 error in generating website or parts thereof")
108 else:
109 PyQt4.QtGui.QMessageBox.information (self, "Success",
110 "Successfully generated website in destination path!")
111
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:
118 return
119
120 dest_rel_path, ok = PyQt4.QtGui.QInputDialog.getText (self, "Desination",
121 "Copy to location (within destination path)")
122 if not ok:
123 return
124
125 folderitem = PyQt4.QtGui.QTreeWidgetItem ([folder_to_add, dest_rel_path])
126 self.additional_folders.addTopLevelItem (folderitem)
127
128 # when folder remove is clicked
129 def onFolderRemove (self):
130 selitem = self.additional_folders.currentItem ()
131 selindex = self.additional_folders.indexOfTopLevelItem (selitem)
132 # if none selected
133 if selindex == -1:
134 return
135 self.additional_folders.takeTopLevelItem (selindex)
136
137
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
143 if not file_to_add:
144 return
145 dest_rel_path, ok = PyQt4.QtGui.QInputDialog.getText (self, "Destination",
146 "Copy to location (within destination path)")
147 if not ok:
148 return
149
150 fileitem = PyQt4.QtGui.QTreeWidgetItem ([file_to_add, dest_rel_path])
151 self.additional_files.addTopLevelItem (fileitem)
152
153 # when file remove is clicked
154 def onFileRemove (self):
155 selitem = self.additional_files.currentItem ()
156 selindex = self.additional_files.indexOfTopLevelItem (selitem)
157 # if none selected
158 if selindex == -1:
159 return
160 self.additional_files.takeTopLevelItem (selindex)