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