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
14 class MainWindow (PyQt4
.QtGui
.QMainWindow
, ui_main_window
.Ui_MainWindow
):
16 PyQt4
.QtGui
.QMainWindow
.__init
__ (self
)
18 self
.current_db
= None
20 # refresh the category list
21 def repopulate_categories (self
):
22 recs
= biaweb_db
.get_categories (self
.current_db
)
24 # check with False because None is returned when no records exist
26 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the categories")
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
)
34 # refresh the articles list
35 def repopulate_articles (self
, catid
=None):
36 recs
= biaweb_db
.get_articles (self
.current_db
, catid
)
38 # check with False because None is returned when no records exist
40 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the articles")
43 self
.articles
.clear ()
45 qrow
= PyQt4
.QtGui
.QTreeWidgetItem ([str(item
[0]), str(item
[1])])
46 self
.articles
.addTopLevelItem (qrow
)
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 ()
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")
60 catid
= self
.get_selected_item_id (self
.categories
)
61 # if no category is selected
63 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected for article")
65 artdlg
= artd
.ArticleDialog (self
)
66 cats
= biaweb_db
.get_categories (self
.current_db
)
68 artdlg
.populate_categories (cats
, catid
)
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
80 rating
= artdlg
.rating
.value ()
81 stub
= str (artdlg
.stub
.text ()).strip ()
83 ret
= biaweb_db
.create_article (self
.current_db
, title
, summary
, keywords
, content
, cid
,
86 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Article successfully created")
87 self
.repopulate_categories ()
88 self
.repopulate_articles (catid
)
90 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating article")
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")
98 # get the selected article
99 artid
= self
.get_selected_item_id (self
.articles
)
100 catid
= self
.get_selected_item_id (self
.categories
)
102 # no article is selected
104 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No article selected")
106 article
= biaweb_db
.get_article (self
.current_db
, artid
)
107 # if article cannot be read
109 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading article")
111 artdlg
= artd
.ArticleDialog (self
)
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])
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
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
)
138 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Article successfully updated")
139 self
.repopulate_articles (catid
)
141 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating article")
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")
149 # get the selected article
150 artid
= self
.get_selected_item_id (self
.articles
)
151 catid
= self
.get_selected_item_id (self
.categories
)
153 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No article selected")
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
)
160 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
161 ret
= biaweb_db
.delete_article (self
.current_db
, artid
)
163 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting article")
165 self
.repopulate_articles (catid
)
167 # when category item is activated
168 def onCategoryItemActivated (self
):
169 self
.onCategoryEdit ()
171 # when article item is activated
172 def onArticleItemActivated (self
):
173 self
.onArticleEdit ()
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
)
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")
187 dlg
= scd
.SiteConfigDialog (self
)
188 configs
= biaweb_db
.get_configuration (self
.current_db
)
191 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading configuration")
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])
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 ()
213 flag
= biaweb_db
.set_configuration (self
.current_db
, site_title
, site_url
, keywords
, description
,
214 copyright
, num_rss
, destination
)
216 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
217 "SQLite 3 error in updating configuration")
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")
225 tdlg
= tpld
.TemplatesDialog (self
, self
.current_db
)
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 ()
232 # get the first column data as integer
233 selindex
= selitems
[0].data(0, 0).toInt ()[0]
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")
246 catid
= self
.get_selected_item_id (self
.categories
)
247 # if no category is selected, display an error
249 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
250 # category is selected
252 cat
= biaweb_db
.get_category (self
.current_db
, catid
)
253 # if the category cannot be retrieved from database
255 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading category")
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])
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 ()
270 ret
= biaweb_db
.update_category (self
.current_db
,
271 catid
, category_name
, category_desc
, category_stub
)
273 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully updated")
274 self
.repopulate_categories ()
276 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating category")
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")
286 # get the selected category
287 catid
= self
.get_selected_item_id (self
.categories
)
289 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
290 # category is selected
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
)
297 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
298 ret
= biaweb_db
.remove_category (self
.current_db
, catid
)
300 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting category")
302 self
.repopulate_categories ()
303 self
.repopulate_articles ()
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")
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
)
321 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully created")
322 self
.repopulate_categories ()
323 self
.repopulate_articles ()
325 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating category")
327 # file open menu is clicked
328 def onFileOpen (self
):
329 filename
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "Open Site Database")
331 self
.current_db
= str (filename
)
332 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
333 self
.repopulate_categories ()
334 self
.repopulate_articles ()
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 ()
350 savefilename
= PyQt4
.QtGui
.QFileDialog
.getSaveFileName (self
, "Save site database to")
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
)
358 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
359 "New site db successfully created")
360 self
.articles
.clear ()
361 self
.categories
.clear ()
363 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "System or SQLite 3 error in creating database")
365 # file quit is clicked
366 def onFileQuit (self
):