32e39f9910728c616659a388c362032407e1d18b
[biaweb_qt.git] / main_window.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Main Window class
3
4 import PyQt4
5 import sys
6
7 import ui_main_window
8 import site_configuration_dialog as scd
9 import category_dialog as catd
10 import article_dialog as artd
11 import templates_dialog as tpld
12 import biaweb_db
13
14 class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
15 def __init__ (self):
16 PyQt4.QtGui.QMainWindow.__init__ (self)
17 self.setupUi (self)
18 self.current_db = None
19
20 # refresh the category list
21 def repopulate_categories (self):
22 recs = biaweb_db.get_categories (self.current_db)
23 if not recs:
24 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories")
25 return
26
27 self.categories.clear ()
28 for (id, name, desc, stub) in recs:
29 qrow = PyQt4.QtGui.QTreeWidgetItem ([str(id), str(name)])
30 self.categories.addTopLevelItem (qrow)
31
32 # refresh the articles list
33 def repopulate_articles (self, catid=None):
34 recs = biaweb_db.get_articles (self.current_db, catid)
35 if not recs:
36 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles")
37 return
38
39 self.articles.clear ()
40 for item in recs:
41 qrow = PyQt4.QtGui.QTreeWidgetItem ([str(item[0]), str(item[1])])
42 self.articles.addTopLevelItem (qrow)
43
44 # when the view is refreshed
45 def onViewRefresh (self):
46 if self.current_db is not None:
47 self.repopulate_categories ()
48 self.repopulate_articles ()
49
50 # when add article is triggered
51 def onArticleAdd (self):
52 if self.current_db is None:
53 PyQt4.QtGui.QMessageBox.critical (self, "Error",
54 "Cannot create article. You need to create or open a website first")
55 else:
56 catid = self.get_selected_item_id (self.categories)
57 # if no category is selected
58 if catid is None:
59 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected for article")
60 return
61 artdlg = artd.ArticleDialog (self)
62 cats = biaweb_db.get_categories (self.current_db)
63
64 artdlg.populate_categories (cats, catid)
65
66 # if OK is pressed
67 if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
68 title = str (artdlg.article_title.text ()).strip ()
69 keywords = str (artdlg.keywords.text ()).strip ()
70 summary = str (artdlg.summary.toPlainText ()).strip ()
71 content = str (artdlg.content.toPlainText ()).strip ()
72 cid, ok = artdlg.category.itemData (artdlg.category.currentIndex ()).toInt ()
73 # if catid is not an integer then abort
74 if not ok:
75 return
76 rating = artdlg.rating.value ()
77 stub = str (artdlg.stub.text ()).strip ()
78
79 ret = biaweb_db.create_article (self.current_db, title, summary, keywords, content, cid,
80 stub, rating)
81 if ret:
82 PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully created")
83 self.repopulate_categories ()
84 self.repopulate_articles (catid)
85 else:
86 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating article")
87
88 # when edit article is triggered
89 def onArticleEdit (self):
90 if self.current_db is None:
91 PyQt4.QtGui.QMessageBox.critical (self, "Error",
92 "Cannot edit article. You need to create or open a website first")
93 else:
94 # get the selected article
95 artid = self.get_selected_item_id (self.articles)
96 catid = self.get_selected_item_id (self.categories)
97
98 # no article is selected
99 if artid is None:
100 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
101 return
102 article = biaweb_db.get_article (self.current_db, artid)
103 # if article cannot be read
104 if not article:
105 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading article")
106 return
107 artdlg = artd.ArticleDialog (self)
108
109 artdlg.article_title.setText (article[1])
110 artdlg.summary.setPlainText (article[2])
111 artdlg.keywords.setText (article[3])
112 artdlg.content.setPlainText (article[4])
113 # populate category combo box and set active category to the article's category
114 cats = biaweb_db.get_categories (self.current_db)
115 artdlg.populate_categories (cats, article[7])
116 artdlg.stub.setText (article[8])
117 artdlg.rating.setValue (article[9])
118
119 # ok is pressed
120 if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
121 title = str (artdlg.article_title.text ()).strip ()
122 summary = str (artdlg.summary.toPlainText ()).strip ()
123 keywords = str (artdlg.keywords.text ()).strip ()
124 content = str (artdlg.content.toPlainText ()).strip ()
125 cid, ok = artdlg.category.itemData (artdlg.category.currentIndex()).toInt ()
126 # if cat id is not an integer
127 if not ok:
128 return
129 rating = artdlg.rating.value ()
130 stub = str (artdlg.stub.text ()).strip ()
131 ret = biaweb_db.update_article (self.current_db, artid, title,
132 summary, keywords, content, cid, stub, rating)
133 if ret:
134 PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully updated")
135 self.repopulate_articles (catid)
136 else:
137 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating article")
138
139 # when delete article is triggered
140 def onArticleDelete (self):
141 if self.current_db is None:
142 PyQt4.QtGui.QMessageBox.critical (self, "Error",
143 "Cannot delete article. You need to create or open a website first")
144 else:
145 # get the selected article
146 artid = self.get_selected_item_id (self.articles)
147 catid = self.get_selected_item_id (self.categories)
148 if artid is None:
149 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
150 return
151 # get confirmation on delete
152 flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
153 "Are you sure you wish to delete the selected article?",
154 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
155 # confirmed
156 if flag == PyQt4.QtGui.QMessageBox.Yes:
157 ret = biaweb_db.delete_article (self.current_db, artid)
158 if not ret:
159 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting article")
160 else:
161 self.repopulate_articles (catid)
162
163 # when category item is activated
164 def onCategoryItemActivated (self):
165 self.onCategoryEdit ()
166
167 # when article item is activated
168 def onArticleItemActivated (self):
169 self.onArticleEdit ()
170
171 # when item selection is changed in categories tree widget
172 def onCategorySelectionChanged (self):
173 if self.current_db is not None:
174 catid = self.get_selected_item_id (self.categories)
175 self.repopulate_articles (catid)
176
177 # when configuration menu is triggered
178 def onConfiguration (self):
179 if self.current_db is None:
180 PyQt4.QtGui.QMessageBox.critical (self, "Error",
181 "Cannot edit configuration. You need to create or open a website first")
182 else:
183 dlg = scd.SiteConfigDialog (self)
184 configs = biaweb_db.get_configuration (self.current_db)
185
186 if not configs:
187 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration")
188 return
189
190 # set the data
191 dlg.site_url.setText (configs[0])
192 dlg.site_title.setText (configs[1])
193 dlg.keywords.setText (configs[2])
194 dlg.description.setPlainText (configs[3])
195 dlg.num_rss_items.setValue (configs[4])
196 dlg.destination.setText (configs[5])
197 dlg.copyright.setText (configs[6])
198
199 if (dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted):
200 # if accepted, get the data and store in database
201 site_title = str (dlg.site_title.text ()).strip ()
202 site_url = str (dlg.site_url.text ()).strip ()
203 keywords = str (dlg.keywords.text ()).strip ()
204 destination = str (dlg.destination.text ()).strip ()
205 description = str (dlg.description.toPlainText ()).strip ()
206 num_rss = dlg.num_rss_items.value ()
207 copyright = str (dlg.copyright.text ()).strip ()
208 # database update
209 flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
210 copyright, num_rss, destination)
211 if not flag:
212 PyQt4.QtGui.QMessageBox.critical (self, "Error",
213 "SQLite 3 error in updating configuration")
214
215 # when templates menu is triggered
216 def onTemplates (self):
217 if self.current_db is None:
218 PyQt4.QtGui.QMessageBox.critical (self, "Error",
219 "Cannot edit templates. You need to create or open a website first")
220 else:
221 tdlg = tpld.TemplatesDialog (self)
222 tdlg.exec_ ()
223
224 # function to get the category or article ID from current selected item in a tree widget
225 def get_selected_item_id (self, twidget):
226 selitems = twidget.selectedItems ()
227 if selitems:
228 # get the first column data as integer
229 selindex = selitems[0].data(0, 0).toInt ()[0]
230 return selindex
231 else:
232 return None
233
234 # category edit action
235 def onCategoryEdit (self):
236 # if no database is open
237 if self.current_db is None:
238 PyQt4.QtGui.QMessageBox.critical (self, "Error",
239 "Cannot edit category. You need to create or open a website first")
240 # database is open
241 else:
242 catid = self.get_selected_item_id (self.categories)
243 # if no category is selected, display an error
244 if catid is None:
245 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
246 # category is selected
247 else:
248 cat = biaweb_db.get_category (self.current_db, catid)
249 # if the category cannot be retrieved from database
250 if not cat:
251 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading category")
252 return
253
254 # set the data in the dialog
255 dlg = catd.CategoryDialog (self)
256 dlg.category_name.setText (cat[1])
257 dlg.category_desc.setText (cat[2])
258 dlg.category_stub.setText (cat[3])
259
260 # if accepted
261 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
262 category_name = str (dlg.category_name.text ()).strip ()
263 category_desc = str (dlg.category_desc.text ()).strip ()
264 category_stub = str (dlg.category_stub.text ()).strip ()
265
266 ret = biaweb_db.update_category (self.current_db,
267 catid, category_name, category_desc, category_stub)
268 if ret:
269 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated")
270 self.repopulate_categories ()
271 else:
272 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category")
273
274
275 # category delete action
276 def onCategoryDelete (self):
277 # if there is no database
278 if self.current_db is None:
279 PyQt4.QtGui.QMessageBox.critical (self, "Error",
280 "Cannot delete category. You need to create or open a website first")
281 else:
282 # get the selected category
283 catid = self.get_selected_item_id (self.categories)
284 if catid is None:
285 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
286 # category is selected
287 else:
288 # get confirmation first
289 flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
290 "This will delete the category and all associated articles. Are you sure you wish to continue?",
291 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
292 # if confirmed
293 if flag == PyQt4.QtGui.QMessageBox.Yes:
294 ret = biaweb_db.remove_category (self.current_db, catid)
295 if not ret:
296 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting category")
297 else:
298 self.repopulate_categories ()
299 self.repopulate_articles ()
300
301 # category add action
302 def onCategoryAdd (self):
303 # if there is no database
304 if self.current_db is None:
305 PyQt4.QtGui.QMessageBox.critical (self, "Error",
306 "Cannot add category. You need to create or open a website first")
307 else:
308 # show the category add dialog
309 dlg = catd.CategoryDialog (self)
310 # if OK button is pressed
311 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
312 cat_name = str (dlg.category_name.text ()).strip ()
313 cat_desc = str (dlg.category_desc.text ()).strip ()
314 cat_stub = str (dlg.category_stub.text ()).strip ()
315 ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub)
316 if ret:
317 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
318 self.repopulate_categories ()
319 self.repopulate_articles ()
320 else:
321 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
322
323 # file open menu is clicked
324 def onFileOpen (self):
325 filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
326 if filename:
327 self.current_db = str (filename)
328 self.setWindowTitle ("BiaWeb - " + self.current_db)
329 self.repopulate_categories ()
330 self.repopulate_articles ()
331
332 # file new menu is clicked
333 def onFileNew (self):
334 # show the site configuration dialog to get the new site details
335 dlg = scd.SiteConfigDialog (self)
336 # if OK button is pressed
337 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
338 site_title = str (dlg.site_title.text ()).strip ()
339 site_url = str (dlg.site_url.text ()).strip ()
340 keywords = str (dlg.keywords.text ()).strip ()
341 destination = str (dlg.destination.text ()).strip ()
342 description = str (dlg.description.toPlainText ()).strip ()
343 num_rss = dlg.num_rss_items.value ()
344 copyright = str (dlg.copyright.text ()).strip ()
345
346 savefilename = PyQt4.QtGui.QFileDialog.getSaveFileName (self, "Save site database to")
347
348 if savefilename:
349 self.current_db = str (savefilename)
350 self.setWindowTitle ("BiaWeb - " + self.current_db)
351 flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
352 copyright, num_rss, destination)
353 if flag:
354 PyQt4.QtGui.QMessageBox.information (self, "Success",
355 "New site db successfully created")
356 self.articles.clear ()
357 self.categories.clear ()
358 else:
359 PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
360
361 # file quit is clicked
362 def onFileQuit (self):
363 sys.exit (0)