1 # BiaWeb Website content manager (c) 2010 V.Harishankar
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 generate_dialog
as gend
15 class MainWindow (PyQt4
.QtGui
.QMainWindow
, ui_main_window
.Ui_MainWindow
):
17 PyQt4
.QtGui
.QMainWindow
.__init
__ (self
)
19 self
.current_db
= None
21 # refresh the category list
22 def repopulate_categories (self
):
23 recs
= biaweb_db
.get_categories (self
.current_db
)
25 # check with False because None is returned when no records exist
27 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the categories")
30 self
.categories
.clear ()
31 for (id, name
, desc
, stub
) in recs
:
32 qrow
= PyQt4
.QtGui
.QTreeWidgetItem ([str(id), str(name
)])
33 self
.categories
.addTopLevelItem (qrow
)
35 # return true when successful
38 # refresh the articles list
39 def repopulate_articles (self
, catid
=None):
40 recs
= biaweb_db
.get_articles (self
.current_db
, catid
)
42 # check with False because None is returned when no records exist
44 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the articles")
47 self
.articles
.clear ()
49 qrow
= PyQt4
.QtGui
.QTreeWidgetItem ([str(item
[0]), str(item
[1])])
50 self
.articles
.addTopLevelItem (qrow
)
51 # return true when successful
53 # when the view is refreshed
54 def onViewRefresh (self
):
55 if self
.current_db
is not None:
56 self
.repopulate_categories ()
57 self
.repopulate_articles ()
59 # when add article is triggered
60 def onArticleAdd (self
):
61 if self
.current_db
is None:
62 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
63 "Cannot create article. You need to create or open a website first")
65 catid
= self
.get_selected_item_id (self
.categories
)
66 # if no category is selected
68 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected for article")
70 artdlg
= artd
.ArticleDialog (self
)
71 cats
= biaweb_db
.get_categories (self
.current_db
)
73 artdlg
.populate_categories (cats
, catid
)
76 if artdlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
77 title
= str (artdlg
.article_title
.text ()).strip ()
78 keywords
= str (artdlg
.keywords
.text ()).strip ()
79 summary
= str (artdlg
.summary
.toPlainText ()).strip ()
80 content
= str (artdlg
.content
.toPlainText ()).strip ()
81 cid
, ok
= artdlg
.category
.itemData (artdlg
.category
.currentIndex ()).toInt ()
82 # if catid is not an integer then abort
85 rating
= artdlg
.rating
.value ()
86 stub
= str (artdlg
.stub
.text ()).strip ()
88 ret
= biaweb_db
.create_article (self
.current_db
, title
, summary
, keywords
, content
, cid
,
91 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Article successfully created")
92 self
.repopulate_categories ()
93 self
.repopulate_articles (catid
)
95 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating article")
97 # when edit article is triggered
98 def onArticleEdit (self
):
99 if self
.current_db
is None:
100 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
101 "Cannot edit article. You need to create or open a website first")
103 # get the selected article
104 artid
= self
.get_selected_item_id (self
.articles
)
105 catid
= self
.get_selected_item_id (self
.categories
)
107 # no article is selected
109 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No article selected")
111 article
= biaweb_db
.get_article (self
.current_db
, artid
)
112 # if article cannot be read
114 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading article")
116 artdlg
= artd
.ArticleDialog (self
)
118 artdlg
.article_title
.setText (article
[1])
119 artdlg
.summary
.setPlainText (article
[2])
120 artdlg
.keywords
.setText (article
[3])
121 artdlg
.content
.setPlainText (article
[4])
122 # populate category combo box and set active category to the article's category
123 cats
= biaweb_db
.get_categories (self
.current_db
)
124 artdlg
.populate_categories (cats
, article
[7])
125 artdlg
.stub
.setText (article
[8])
126 artdlg
.rating
.setValue (article
[9])
129 if artdlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
130 title
= str (artdlg
.article_title
.text ()).strip ()
131 summary
= str (artdlg
.summary
.toPlainText ()).strip ()
132 keywords
= str (artdlg
.keywords
.text ()).strip ()
133 content
= str (artdlg
.content
.toPlainText ()).strip ()
134 cid
, ok
= artdlg
.category
.itemData (artdlg
.category
.currentIndex()).toInt ()
135 # if cat id is not an integer
138 rating
= artdlg
.rating
.value ()
139 stub
= str (artdlg
.stub
.text ()).strip ()
140 ret
= biaweb_db
.update_article (self
.current_db
, artid
, title
,
141 summary
, keywords
, content
, cid
, stub
, rating
)
143 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Article successfully updated")
144 self
.repopulate_articles (catid
)
146 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating article")
148 # when delete article is triggered
149 def onArticleDelete (self
):
150 if self
.current_db
is None:
151 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
152 "Cannot delete article. You need to create or open a website first")
154 # get the selected article
155 artid
= self
.get_selected_item_id (self
.articles
)
156 catid
= self
.get_selected_item_id (self
.categories
)
158 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No article selected")
160 # get confirmation on delete
161 flag
= PyQt4
.QtGui
.QMessageBox
.question (self
, "Confirm",
162 "Are you sure you wish to delete the selected article?",
163 PyQt4
.QtGui
.QMessageBox
.Yes
, PyQt4
.QtGui
.QMessageBox
.No
)
165 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
166 ret
= biaweb_db
.delete_article (self
.current_db
, artid
)
168 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting article")
170 self
.repopulate_articles (catid
)
172 # when category item is activated
173 def onCategoryItemActivated (self
):
174 self
.onCategoryEdit ()
176 # when article item is activated
177 def onArticleItemActivated (self
):
178 self
.onArticleEdit ()
180 # when item selection is changed in categories tree widget
181 def onCategorySelectionChanged (self
):
182 if self
.current_db
is not None:
183 catid
= self
.get_selected_item_id (self
.categories
)
184 self
.repopulate_articles (catid
)
186 # when configuration menu is triggered
187 def onConfiguration (self
):
188 if self
.current_db
is None:
189 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
190 "Cannot edit configuration. You need to create or open a website first")
192 dlg
= scd
.SiteConfigDialog (self
)
193 configs
= biaweb_db
.get_configuration (self
.current_db
)
196 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading configuration")
200 dlg
.site_url
.setText (configs
[0])
201 dlg
.site_title
.setText (configs
[1])
202 dlg
.keywords
.setText (configs
[2])
203 dlg
.description
.setPlainText (configs
[3])
204 dlg
.num_rss_items
.setValue (configs
[4])
205 dlg
.destination
.setText (configs
[5])
206 dlg
.copyright
.setText (configs
[6])
208 if (dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
):
209 # if accepted, get the data and store in database
210 site_title
= str (dlg
.site_title
.text ()).strip ()
211 site_url
= str (dlg
.site_url
.text ()).strip ()
212 keywords
= str (dlg
.keywords
.text ()).strip ()
213 destination
= str (dlg
.destination
.text ()).strip ()
214 description
= str (dlg
.description
.toPlainText ()).strip ()
215 num_rss
= dlg
.num_rss_items
.value ()
216 copyright
= str (dlg
.copyright
.text ()).strip ()
218 flag
= biaweb_db
.set_configuration (self
.current_db
, site_title
, site_url
, keywords
, description
,
219 copyright
, num_rss
, destination
)
221 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
222 "SQLite 3 error in updating configuration")
224 # when templates menu is triggered
225 def onTemplates (self
):
226 if self
.current_db
is None:
227 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
228 "Cannot edit templates. You need to create or open a website first")
230 tdlg
= tpld
.TemplatesDialog (self
, self
.current_db
)
233 # function to get the category or article ID from current selected item in a tree widget
234 def get_selected_item_id (self
, twidget
):
235 selitems
= twidget
.selectedItems ()
237 # get the first column data as integer
238 selindex
= selitems
[0].data(0, 0).toInt ()[0]
243 # category edit action
244 def onCategoryEdit (self
):
245 # if no database is open
246 if self
.current_db
is None:
247 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
248 "Cannot edit category. You need to create or open a website first")
251 catid
= self
.get_selected_item_id (self
.categories
)
252 # if no category is selected, display an error
254 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
255 # category is selected
257 cat
= biaweb_db
.get_category (self
.current_db
, catid
)
258 # if the category cannot be retrieved from database
260 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading category")
263 # set the data in the dialog
264 dlg
= catd
.CategoryDialog (self
)
265 dlg
.category_name
.setText (cat
[1])
266 dlg
.category_desc
.setText (cat
[2])
267 dlg
.category_stub
.setText (cat
[3])
270 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
271 category_name
= str (dlg
.category_name
.text ()).strip ()
272 category_desc
= str (dlg
.category_desc
.text ()).strip ()
273 category_stub
= str (dlg
.category_stub
.text ()).strip ()
275 ret
= biaweb_db
.update_category (self
.current_db
,
276 catid
, category_name
, category_desc
, category_stub
)
278 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully updated")
279 self
.repopulate_categories ()
281 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating category")
284 # category delete action
285 def onCategoryDelete (self
):
286 # if there is no database
287 if self
.current_db
is None:
288 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
289 "Cannot delete category. You need to create or open a website first")
291 # get the selected category
292 catid
= self
.get_selected_item_id (self
.categories
)
294 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
295 # category is selected
297 # get confirmation first
298 flag
= PyQt4
.QtGui
.QMessageBox
.question (self
, "Confirm",
299 "This will delete the category and all associated articles. Are you sure you wish to continue?",
300 PyQt4
.QtGui
.QMessageBox
.Yes
, PyQt4
.QtGui
.QMessageBox
.No
)
302 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
303 ret
= biaweb_db
.remove_category (self
.current_db
, catid
)
305 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting category")
307 self
.repopulate_categories ()
308 self
.repopulate_articles ()
310 # category add action
311 def onCategoryAdd (self
):
312 # if there is no database
313 if self
.current_db
is None:
314 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
315 "Cannot add category. You need to create or open a website first")
317 # show the category add dialog
318 dlg
= catd
.CategoryDialog (self
)
319 # if OK button is pressed
320 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
321 cat_name
= str (dlg
.category_name
.text ()).strip ()
322 cat_desc
= str (dlg
.category_desc
.text ()).strip ()
323 cat_stub
= str (dlg
.category_stub
.text ()).strip ()
324 ret
= biaweb_db
.create_category (self
.current_db
, cat_name
, cat_desc
, cat_stub
)
326 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully created")
327 self
.repopulate_categories ()
328 self
.repopulate_articles ()
330 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating category")
332 # file open menu is clicked
333 def onFileOpen (self
):
334 filename
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "Open Site Database")
336 self
.current_db
= str (filename
)
338 # added to check whether categories are loaded successfully or not
339 # if not, then reset the current_db to None
340 loaded_cats
= self
.repopulate_categories ()
341 loaded_arts
= self
.repopulate_articles ()
342 # if failed in loading either categories or articles (note: checking against
343 # False and not None)
344 if loaded_cats
== False or loaded_arts
== False:
345 self
.current_db
= None
346 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
347 "SQLite 3 error in loading site database. This appears to be an invalid BiaWeb database")
349 # set the window title to the database
350 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
353 # file new menu is clicked
354 def onFileNew (self
):
355 # show the site configuration dialog to get the new site details
356 dlg
= scd
.SiteConfigDialog (self
)
357 # if OK button is pressed
358 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
359 site_title
= str (dlg
.site_title
.text ()).strip ()
360 site_url
= str (dlg
.site_url
.text ()).strip ()
361 keywords
= str (dlg
.keywords
.text ()).strip ()
362 destination
= str (dlg
.destination
.text ()).strip ()
363 description
= str (dlg
.description
.toPlainText ()).strip ()
364 num_rss
= dlg
.num_rss_items
.value ()
365 copyright
= str (dlg
.copyright
.text ()).strip ()
367 savefilename
= PyQt4
.QtGui
.QFileDialog
.getSaveFileName (self
, "Save site database to")
370 self
.current_db
= str (savefilename
)
371 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
372 flag
= biaweb_db
.create_db (self
.current_db
, site_title
, site_url
, keywords
, description
,
373 copyright
, num_rss
, destination
)
375 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
376 "New site db successfully created")
377 self
.articles
.clear ()
378 self
.categories
.clear ()
380 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "System or SQLite 3 error in creating database")
382 # when generate site menu is triggered
383 def onGenerateSite (self
):
384 if self
.current_db
is None:
385 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
386 "Cannot generate site. You need to create or open a website first")
388 # run the generate site dialog
389 gendlg
= gend
.GenerateDialog (self
, self
.current_db
)
392 # about menu is triggered
394 PyQt4
.QtGui
.QMessageBox
.about (self
, "BiaWeb Qt",
395 "<b>A static website/weblog content management system</b><br /><br />\
396 Copyright © 2010 <a href=\"http://www.harishankar.org\">Harishankar</a><br />\
397 Licensed under GNU/GPL v3")
399 # file quit is clicked
400 def onFileQuit (self
):