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 catid
, 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
, catid
,
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
):
90 # when delete article is triggered
91 def onArticleDelete (self
):
92 if self
.current_db
is None:
93 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
94 "Cannot delete article. You need to create or open a website first")
96 # get the selected article
97 artid
= self
.get_selected_item_id (self
.articles
)
98 catid
= self
.get_selected_item_id (self
.categories
)
100 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No article selected")
102 # get confirmation on delete
103 conf
= PyQt4
.QtGui
.QMessageBox
.question (self
, "Confirm",
104 "Are you sure you wish to delete the selected article?",
105 PyQt4
.QtGui
.QMessageBox
.Yes
, PyQt4
.QtGui
.QMessageBox
.No
)
107 if conf
== PyQt4
.QtGui
.QMessageBox
.Yes
:
108 ret
= biaweb_db
.delete_article (self
.current_db
, artid
)
110 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting article")
112 self
.repopulate_articles (catid
)
114 # when category item is activated
115 def onCategoryItemActivated (self
):
116 self
.onCategoryEdit ()
118 # when article item is activated
119 def onArticleItemActivated (self
):
120 self
.onArticleEdit ()
122 # when item selection is changed in categories tree widget
123 def onCategorySelectionChanged (self
):
124 if self
.current_db
is not None:
125 catid
= self
.get_selected_item_id (self
.categories
)
126 self
.repopulate_articles (catid
)
128 # when configuration menu is activated
129 def onConfiguration (self
):
130 if self
.current_db
is None:
131 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
132 "Cannot edit configuration. You need to create or open a website first")
134 dlg
= scd
.SiteConfigDialog (self
)
135 configs
= biaweb_db
.get_configuration (self
.current_db
)
138 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading configuration")
142 dlg
.site_url
.setText (configs
[0])
143 dlg
.site_title
.setText (configs
[1])
144 dlg
.keywords
.setText (configs
[2])
145 dlg
.description
.setPlainText (configs
[3])
146 dlg
.num_rss_items
.setValue (configs
[4])
147 dlg
.destination
.setText (configs
[5])
148 dlg
.copyright
.setText (configs
[6])
150 if (dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
):
151 # if accepted, get the data and store in database
152 site_title
= str (dlg
.site_title
.text ()).strip ()
153 site_url
= str (dlg
.site_url
.text ()).strip ()
154 keywords
= str (dlg
.keywords
.text ()).strip ()
155 destination
= str (dlg
.destination
.text ()).strip ()
156 description
= str (dlg
.description
.toPlainText ()).strip ()
157 num_rss
= dlg
.num_rss_items
.value ()
158 copyright
= str (dlg
.copyright
.text ()).strip ()
160 flag
= biaweb_db
.set_configuration (self
.current_db
, site_title
, site_url
, keywords
, description
,
161 copyright
, num_rss
, destination
)
163 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
164 "SQLite 3 error in updating configuration")
166 # function to get the category or article ID from current selected item in a tree widget
167 def get_selected_item_id (self
, twidget
):
168 selitems
= twidget
.selectedItems ()
170 # get the first column data as integer
171 selindex
= selitems
[0].data(0, 0).toInt ()[0]
176 # category edit action
177 def onCategoryEdit (self
):
178 # if no database is open
179 if self
.current_db
is None:
180 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
181 "Cannot edit category. You need to create or open a website first")
184 catid
= self
.get_selected_item_id (self
.categories
)
185 # if no category is selected, display an error
187 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
188 # category is selected
190 cat
= biaweb_db
.get_category (self
.current_db
, catid
)
191 # if the category cannot be retrieved from database
193 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error reading category")
196 # set the data in the dialog
197 dlg
= catd
.CategoryDialog (self
)
198 dlg
.category_name
.setText (cat
[1])
199 dlg
.category_desc
.setText (cat
[2])
200 dlg
.category_stub
.setText (cat
[3])
203 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
204 category_name
= str (dlg
.category_name
.text ()).strip ()
205 category_desc
= str (dlg
.category_desc
.text ()).strip ()
206 category_stub
= str (dlg
.category_stub
.text ()).strip ()
208 ret
= biaweb_db
.update_category (self
.current_db
,
209 catid
, category_name
, category_desc
, category_stub
)
211 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully updated")
212 self
.repopulate_categories ()
214 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating category")
217 # category delete action
218 def onCategoryDelete (self
):
219 # if there is no database
220 if self
.current_db
is None:
221 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
222 "Cannot delete category. You need to create or open a website first")
224 # get the selected category
225 catid
= self
.get_selected_item_id (self
.categories
)
227 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
228 # category is selected
230 # get confirmation first
231 flag
= PyQt4
.QtGui
.QMessageBox
.question (self
, "Confirm",
232 "This will delete the category and all associated articles. Are you sure you wish to continue?",
233 PyQt4
.QtGui
.QMessageBox
.Yes
, PyQt4
.QtGui
.QMessageBox
.No
)
235 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
236 ret
= biaweb_db
.remove_category (self
.current_db
, catid
)
238 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting category")
240 self
.repopulate_categories ()
241 self
.repopulate_articles ()
243 # category add action
244 def onCategoryAdd (self
):
245 # if there is no database
246 if self
.current_db
is None:
247 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
248 "Cannot add category. You need to create or open a website first")
250 # show the category add dialog
251 dlg
= catd
.CategoryDialog (self
)
252 # if OK button is pressed
253 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
254 cat_name
= str (dlg
.category_name
.text ()).strip ()
255 cat_desc
= str (dlg
.category_desc
.text ()).strip ()
256 cat_stub
= str (dlg
.category_stub
.text ()).strip ()
257 ret
= biaweb_db
.create_category (self
.current_db
, cat_name
, cat_desc
, cat_stub
)
259 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully created")
260 self
.repopulate_categories ()
261 self
.repopulate_articles ()
263 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating category")
265 # file open menu is clicked
266 def onFileOpen (self
):
267 filename
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "Open Site Database")
269 self
.current_db
= str (filename
)
270 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
271 self
.repopulate_categories ()
272 self
.repopulate_articles ()
274 # file new menu is clicked
275 def onFileNew (self
):
276 # show the site configuration dialog to get the new site details
277 dlg
= scd
.SiteConfigDialog (self
)
278 # if OK button is pressed
279 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
280 site_title
= str (dlg
.site_title
.text ()).strip ()
281 site_url
= str (dlg
.site_url
.text ()).strip ()
282 keywords
= str (dlg
.keywords
.text ()).strip ()
283 destination
= str (dlg
.destination
.text ()).strip ()
284 description
= str (dlg
.description
.toPlainText ()).strip ()
285 num_rss
= dlg
.num_rss_items
.value ()
286 copyright
= str (dlg
.copyright
.text ()).strip ()
288 savefilename
= PyQt4
.QtGui
.QFileDialog
.getSaveFileName (self
, "Save site database to")
291 self
.current_db
= str (savefilename
)
292 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
293 flag
= biaweb_db
.create_db (self
.current_db
, site_title
, site_url
, keywords
, description
,
294 copyright
, num_rss
, destination
)
296 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
297 "New site db successfully created")
298 self
.articles
.clear ()
299 self
.categories
.clear ()
301 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "System or SQLite 3 error in creating database")
303 # file quit is clicked
304 def onFileQuit (self
):