1 # BiaWeb Website content manager (c) 2010 V.Harishankar
6 import site_configuration_dialog
as scd
7 import category_dialog
as catd
8 import article_dialog
as artd
12 class MainWindow (PyQt4
.QtGui
.QMainWindow
, ui_main_window
.Ui_MainWindow
):
14 PyQt4
.QtGui
.QMainWindow
.__init
__ (self
)
16 self
.current_db
= None
18 # refresh the category list
19 def repopulate_categories (self
):
20 recs
= biaweb_db
.get_categories (self
.current_db
)
22 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the categories")
25 self
.categories
.clear ()
26 for (id, name
, desc
, stub
) in recs
:
27 qrow
= PyQt4
.QtGui
.QTreeWidgetItem ([str(id), str(name
)])
28 self
.categories
.addTopLevelItem (qrow
)
30 # refresh the articles list
31 def repopulate_articles (self
, catid
=None):
32 recs
= biaweb_db
.get_articles (self
.current_db
, catid
)
34 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the articles")
37 self
.articles
.clear ()
39 qrow
= PyQt4
.QtGui
.QTreeWidgetItem ([str(item
[0]), str(item
[1])])
40 self
.articles
.addTopLevelItem (qrow
)
42 # when the view is refreshed
43 def onViewRefresh (self
):
44 if self
.current_db
is not None:
45 self
.repopulate_categories ()
46 self
.repopulate_articles ()
48 # when add article is triggered
49 def onArticleAdd (self
):
50 if self
.current_db
is None:
51 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
52 "Cannot create article. You need to create or open a website first")
54 catid
= self
.get_selected_item_id (self
.categories
)
55 # if no category is selected
57 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected for article")
59 artdlg
= artd
.ArticleDialog (self
)
60 cats
= biaweb_db
.get_categories (self
.current_db
)
62 artdlg
.populate_categories (cats
, catid
)
65 if artdlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
66 title
= str (artdlg
.article_title
.text ()).strip ()
67 keywords
= str (artdlg
.keywords
.text ()).strip ()
68 summary
= str (artdlg
.summary
.toPlainText ()).strip ()
69 content
= str (artdlg
.content
.toPlainText ()).strip ()
70 cid
, ok
= artdlg
.category
.itemData (artdlg
.category
.currentIndex ()).toInt ()
71 # if catid is not an integer then abort
74 rating
= artdlg
.rating
.value ()
75 stub
= str (artdlg
.stub
.text ()).strip ()
77 ret
= biaweb_db
.create_article (self
.current_db
, title
, summary
, keywords
, content
, cid
,
80 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Article successfully created")
81 self
.repopulate_categories ()
82 self
.repopulate_articles (catid
)
84 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating article")
86 # when edit article is triggered
87 def onArticleEdit (self
):
88 if self
.current_db
is None:
89 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
90 "Cannot edit article. You need to create or open a website first")
92 # get the selected article
93 artid
= self
.get_selected_item_id (self
.articles
)
94 catid
= self
.get_selected_item_id (self
.categories
)
96 # no article is selected
98 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No article selected")
100 article
= biaweb_db
.get_article (self
.current_db
, artid
)
101 # if article cannot be read
103 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading article")
105 artdlg
= artd
.ArticleDialog (self
)
107 artdlg
.article_title
.setText (article
[1])
108 artdlg
.summary
.setPlainText (article
[2])
109 artdlg
.keywords
.setText (article
[3])
110 artdlg
.content
.setPlainText (article
[4])
111 # populate category combo box and set active category to the article's category
112 cats
= biaweb_db
.get_categories (self
.current_db
)
113 artdlg
.populate_categories (cats
, article
[7])
114 artdlg
.stub
.setText (article
[8])
115 artdlg
.rating
.setValue (article
[9])
118 if artdlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
119 title
= str (artdlg
.article_title
.text ()).strip ()
120 summary
= str (artdlg
.summary
.toPlainText ()).strip ()
121 keywords
= str (artdlg
.keywords
.text ()).strip ()
122 content
= str (artdlg
.content
.toPlainText ()).strip ()
123 cid
, ok
= artdlg
.category
.itemData (artdlg
.category
.currentIndex()).toInt ()
124 # if cat id is not an integer
127 rating
= artdlg
.rating
.value ()
128 stub
= str (artdlg
.stub
.text ()).strip ()
129 ret
= biaweb_db
.update_article (self
.current_db
, artid
, title
,
130 summary
, keywords
, content
, cid
, stub
, rating
)
132 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Article successfully updated")
133 self
.repopulate_articles (catid
)
135 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating article")
137 # when delete article is triggered
138 def onArticleDelete (self
):
139 if self
.current_db
is None:
140 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
141 "Cannot delete article. You need to create or open a website first")
143 # get the selected article
144 artid
= self
.get_selected_item_id (self
.articles
)
145 catid
= self
.get_selected_item_id (self
.categories
)
147 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No article selected")
149 # get confirmation on delete
150 flag
= PyQt4
.QtGui
.QMessageBox
.question (self
, "Confirm",
151 "Are you sure you wish to delete the selected article?",
152 PyQt4
.QtGui
.QMessageBox
.Yes
, PyQt4
.QtGui
.QMessageBox
.No
)
154 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
155 ret
= biaweb_db
.delete_article (self
.current_db
, artid
)
157 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting article")
159 self
.repopulate_articles (catid
)
161 # when category item is activated
162 def onCategoryItemActivated (self
):
163 self
.onCategoryEdit ()
165 # when article item is activated
166 def onArticleItemActivated (self
):
167 self
.onArticleEdit ()
169 # when item selection is changed in categories tree widget
170 def onCategorySelectionChanged (self
):
171 if self
.current_db
is not None:
172 catid
= self
.get_selected_item_id (self
.categories
)
173 self
.repopulate_articles (catid
)
175 # when configuration menu is activated
176 def onConfiguration (self
):
177 if self
.current_db
is None:
178 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
179 "Cannot edit configuration. You need to create or open a website first")
181 dlg
= scd
.SiteConfigDialog (self
)
182 configs
= biaweb_db
.get_configuration (self
.current_db
)
185 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading configuration")
189 dlg
.site_url
.setText (configs
[0])
190 dlg
.site_title
.setText (configs
[1])
191 dlg
.keywords
.setText (configs
[2])
192 dlg
.description
.setPlainText (configs
[3])
193 dlg
.num_rss_items
.setValue (configs
[4])
194 dlg
.destination
.setText (configs
[5])
195 dlg
.copyright
.setText (configs
[6])
197 if (dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
):
198 # if accepted, get the data and store in database
199 site_title
= str (dlg
.site_title
.text ()).strip ()
200 site_url
= str (dlg
.site_url
.text ()).strip ()
201 keywords
= str (dlg
.keywords
.text ()).strip ()
202 destination
= str (dlg
.destination
.text ()).strip ()
203 description
= str (dlg
.description
.toPlainText ()).strip ()
204 num_rss
= dlg
.num_rss_items
.value ()
205 copyright
= str (dlg
.copyright
.text ()).strip ()
207 flag
= biaweb_db
.set_configuration (self
.current_db
, site_title
, site_url
, keywords
, description
,
208 copyright
, num_rss
, destination
)
210 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
211 "SQLite 3 error in updating configuration")
213 # function to get the category or article ID from current selected item in a tree widget
214 def get_selected_item_id (self
, twidget
):
215 selitems
= twidget
.selectedItems ()
217 # get the first column data as integer
218 selindex
= selitems
[0].data(0, 0).toInt ()[0]
223 # category edit action
224 def onCategoryEdit (self
):
225 # if no database is open
226 if self
.current_db
is None:
227 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
228 "Cannot edit category. You need to create or open a website first")
231 catid
= self
.get_selected_item_id (self
.categories
)
232 # if no category is selected, display an error
234 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
235 # category is selected
237 cat
= biaweb_db
.get_category (self
.current_db
, catid
)
238 # if the category cannot be retrieved from database
240 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading category")
243 # set the data in the dialog
244 dlg
= catd
.CategoryDialog (self
)
245 dlg
.category_name
.setText (cat
[1])
246 dlg
.category_desc
.setText (cat
[2])
247 dlg
.category_stub
.setText (cat
[3])
250 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
251 category_name
= str (dlg
.category_name
.text ()).strip ()
252 category_desc
= str (dlg
.category_desc
.text ()).strip ()
253 category_stub
= str (dlg
.category_stub
.text ()).strip ()
255 ret
= biaweb_db
.update_category (self
.current_db
,
256 catid
, category_name
, category_desc
, category_stub
)
258 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully updated")
259 self
.repopulate_categories ()
261 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating category")
264 # category delete action
265 def onCategoryDelete (self
):
266 # if there is no database
267 if self
.current_db
is None:
268 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
269 "Cannot delete category. You need to create or open a website first")
271 # get the selected category
272 catid
= self
.get_selected_item_id (self
.categories
)
274 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
275 # category is selected
277 # get confirmation first
278 flag
= PyQt4
.QtGui
.QMessageBox
.question (self
, "Confirm",
279 "This will delete the category and all associated articles. Are you sure you wish to continue?",
280 PyQt4
.QtGui
.QMessageBox
.Yes
, PyQt4
.QtGui
.QMessageBox
.No
)
282 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
283 ret
= biaweb_db
.remove_category (self
.current_db
, catid
)
285 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting category")
287 self
.repopulate_categories ()
288 self
.repopulate_articles ()
290 # category add action
291 def onCategoryAdd (self
):
292 # if there is no database
293 if self
.current_db
is None:
294 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
295 "Cannot add category. You need to create or open a website first")
297 # show the category add dialog
298 dlg
= catd
.CategoryDialog (self
)
299 # if OK button is pressed
300 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
301 cat_name
= str (dlg
.category_name
.text ()).strip ()
302 cat_desc
= str (dlg
.category_desc
.text ()).strip ()
303 cat_stub
= str (dlg
.category_stub
.text ()).strip ()
304 ret
= biaweb_db
.create_category (self
.current_db
, cat_name
, cat_desc
, cat_stub
)
306 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully created")
307 self
.repopulate_categories ()
308 self
.repopulate_articles ()
310 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating category")
312 # file open menu is clicked
313 def onFileOpen (self
):
314 filename
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "Open Site Database")
316 self
.current_db
= str (filename
)
317 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
318 self
.repopulate_categories ()
319 self
.repopulate_articles ()
321 # file new menu is clicked
322 def onFileNew (self
):
323 # show the site configuration dialog to get the new site details
324 dlg
= scd
.SiteConfigDialog (self
)
325 # if OK button is pressed
326 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
327 site_title
= str (dlg
.site_title
.text ()).strip ()
328 site_url
= str (dlg
.site_url
.text ()).strip ()
329 keywords
= str (dlg
.keywords
.text ()).strip ()
330 destination
= str (dlg
.destination
.text ()).strip ()
331 description
= str (dlg
.description
.toPlainText ()).strip ()
332 num_rss
= dlg
.num_rss_items
.value ()
333 copyright
= str (dlg
.copyright
.text ()).strip ()
335 savefilename
= PyQt4
.QtGui
.QFileDialog
.getSaveFileName (self
, "Save site database to")
338 self
.current_db
= str (savefilename
)
339 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
340 flag
= biaweb_db
.create_db (self
.current_db
, site_title
, site_url
, keywords
, description
,
341 copyright
, num_rss
, destination
)
343 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
344 "New site db successfully created")
345 self
.articles
.clear ()
346 self
.categories
.clear ()
348 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "System or SQLite 3 error in creating database")
350 # file quit is clicked
351 def onFileQuit (self
):