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 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the categories")
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
)
32 # refresh the articles list
33 def repopulate_articles (self
, catid
=None):
34 recs
= biaweb_db
.get_articles (self
.current_db
, catid
)
36 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the articles")
39 self
.articles
.clear ()
41 qrow
= PyQt4
.QtGui
.QTreeWidgetItem ([str(item
[0]), str(item
[1])])
42 self
.articles
.addTopLevelItem (qrow
)
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 ()
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")
56 catid
= self
.get_selected_item_id (self
.categories
)
57 # if no category is selected
59 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected for article")
61 artdlg
= artd
.ArticleDialog (self
)
62 cats
= biaweb_db
.get_categories (self
.current_db
)
64 artdlg
.populate_categories (cats
, catid
)
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
76 rating
= artdlg
.rating
.value ()
77 stub
= str (artdlg
.stub
.text ()).strip ()
79 ret
= biaweb_db
.create_article (self
.current_db
, title
, summary
, keywords
, content
, cid
,
82 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Article successfully created")
83 self
.repopulate_categories ()
84 self
.repopulate_articles (catid
)
86 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating article")
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")
94 # get the selected article
95 artid
= self
.get_selected_item_id (self
.articles
)
96 catid
= self
.get_selected_item_id (self
.categories
)
98 # no article is selected
100 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No article selected")
102 article
= biaweb_db
.get_article (self
.current_db
, artid
)
103 # if article cannot be read
105 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading article")
107 artdlg
= artd
.ArticleDialog (self
)
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])
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
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
)
134 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Article successfully updated")
135 self
.repopulate_articles (catid
)
137 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating article")
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")
145 # get the selected article
146 artid
= self
.get_selected_item_id (self
.articles
)
147 catid
= self
.get_selected_item_id (self
.categories
)
149 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No article selected")
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
)
156 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
157 ret
= biaweb_db
.delete_article (self
.current_db
, artid
)
159 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting article")
161 self
.repopulate_articles (catid
)
163 # when category item is activated
164 def onCategoryItemActivated (self
):
165 self
.onCategoryEdit ()
167 # when article item is activated
168 def onArticleItemActivated (self
):
169 self
.onArticleEdit ()
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
)
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")
183 dlg
= scd
.SiteConfigDialog (self
)
184 configs
= biaweb_db
.get_configuration (self
.current_db
)
187 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading configuration")
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])
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 ()
209 flag
= biaweb_db
.set_configuration (self
.current_db
, site_title
, site_url
, keywords
, description
,
210 copyright
, num_rss
, destination
)
212 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
213 "SQLite 3 error in updating configuration")
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")
221 tdlg
= tpld
.TemplatesDialog (self
)
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 ()
228 # get the first column data as integer
229 selindex
= selitems
[0].data(0, 0).toInt ()[0]
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")
242 catid
= self
.get_selected_item_id (self
.categories
)
243 # if no category is selected, display an error
245 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
246 # category is selected
248 cat
= biaweb_db
.get_category (self
.current_db
, catid
)
249 # if the category cannot be retrieved from database
251 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading category")
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])
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 ()
266 ret
= biaweb_db
.update_category (self
.current_db
,
267 catid
, category_name
, category_desc
, category_stub
)
269 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully updated")
270 self
.repopulate_categories ()
272 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating category")
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")
282 # get the selected category
283 catid
= self
.get_selected_item_id (self
.categories
)
285 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
286 # category is selected
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
)
293 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
294 ret
= biaweb_db
.remove_category (self
.current_db
, catid
)
296 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting category")
298 self
.repopulate_categories ()
299 self
.repopulate_articles ()
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")
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
)
317 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully created")
318 self
.repopulate_categories ()
319 self
.repopulate_articles ()
321 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating category")
323 # file open menu is clicked
324 def onFileOpen (self
):
325 filename
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "Open Site Database")
327 self
.current_db
= str (filename
)
328 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
329 self
.repopulate_categories ()
330 self
.repopulate_articles ()
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 ()
346 savefilename
= PyQt4
.QtGui
.QFileDialog
.getSaveFileName (self
, "Save site database to")
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
)
354 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
355 "New site db successfully created")
356 self
.articles
.clear ()
357 self
.categories
.clear ()
359 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "System or SQLite 3 error in creating database")
361 # file quit is clicked
362 def onFileQuit (self
):