Article creating and deleting implemented
[biaweb_qt.git] / main_window.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Main Window class
3
4 import PyQt4
5 import ui_main_window
6 import site_configuration_dialog as scd
7 import category_dialog as catd
8 import article_dialog as artd
9 import biaweb_db
10 import sys
11
12 class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
13 def __init__ (self):
14 PyQt4.QtGui.QMainWindow.__init__ (self)
15 self.setupUi (self)
16 self.current_db = None
17
18 # refresh the category list
19 def repopulate_categories (self):
20 recs = biaweb_db.get_categories (self.current_db)
21 if not recs:
22 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories")
23 return
24
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)
29
30 # refresh the articles list
31 def repopulate_articles (self, catid=None):
32 recs = biaweb_db.get_articles (self.current_db, catid)
33 if not recs:
34 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles")
35 return
36
37 self.articles.clear ()
38 for item in recs:
39 qrow = PyQt4.QtGui.QTreeWidgetItem ([str(item[0]), str(item[1])])
40 self.articles.addTopLevelItem (qrow)
41
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 ()
47
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")
53 else:
54 catid = self.get_selected_item_id (self.categories)
55 # if no category is selected
56 if catid is None:
57 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected for article")
58 return
59 artdlg = artd.ArticleDialog (self)
60 cats = biaweb_db.get_categories (self.current_db)
61
62 artdlg.populate_categories (cats, catid)
63
64 # if OK is pressed
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
72 if not ok:
73 return
74 rating = artdlg.rating.value ()
75 stub = str (artdlg.stub.text ()).strip ()
76
77 ret = biaweb_db.create_article (self.current_db, title, summary, keywords, content, catid,
78 stub, rating)
79 if ret:
80 PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully created")
81 self.repopulate_categories ()
82 self.repopulate_articles (catid)
83 else:
84 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating article")
85
86 # when edit article is triggered
87 def onArticleEdit (self):
88 pass
89
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")
95 else:
96 # get the selected article
97 artid = self.get_selected_item_id (self.articles)
98 catid = self.get_selected_item_id (self.categories)
99 if artid is None:
100 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
101 return
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)
106 # confirmed
107 if conf == PyQt4.QtGui.QMessageBox.Yes:
108 ret = biaweb_db.delete_article (self.current_db, artid)
109 if not ret:
110 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting article")
111 else:
112 self.repopulate_articles (catid)
113
114 # when category item is activated
115 def onCategoryItemActivated (self):
116 self.onCategoryEdit ()
117
118 # when article item is activated
119 def onArticleItemActivated (self):
120 self.onArticleEdit ()
121
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)
127
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")
133 else:
134 dlg = scd.SiteConfigDialog (self)
135 configs = biaweb_db.get_configuration (self.current_db)
136
137 if not configs:
138 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration")
139 return
140
141 # set the data
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])
149
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 ()
159 # database update
160 flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
161 copyright, num_rss, destination)
162 if not flag:
163 PyQt4.QtGui.QMessageBox.critical (self, "Error",
164 "SQLite 3 error in updating configuration")
165
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 ()
169 if selitems:
170 # get the first column data as integer
171 selindex = selitems[0].data(0, 0).toInt ()[0]
172 return selindex
173 else:
174 return None
175
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")
182 # database is open
183 else:
184 catid = self.get_selected_item_id (self.categories)
185 # if no category is selected, display an error
186 if catid is None:
187 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
188 # category is selected
189 else:
190 cat = biaweb_db.get_category (self.current_db, catid)
191 # if the category cannot be retrieved from database
192 if not cat:
193 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error reading category")
194 return
195
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])
201
202 # if accepted
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 ()
207
208 ret = biaweb_db.update_category (self.current_db,
209 catid, category_name, category_desc, category_stub)
210 if ret:
211 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated")
212 self.repopulate_categories ()
213 else:
214 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category")
215
216
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")
223 else:
224 # get the selected category
225 catid = self.get_selected_item_id (self.categories)
226 if catid is None:
227 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
228 # category is selected
229 else:
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)
234 # if confirmed
235 if flag == PyQt4.QtGui.QMessageBox.Yes:
236 ret = biaweb_db.remove_category (self.current_db, catid)
237 if not ret:
238 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting category")
239 else:
240 self.repopulate_categories ()
241 self.repopulate_articles ()
242
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")
249 else:
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)
258 if ret:
259 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
260 self.repopulate_categories ()
261 self.repopulate_articles ()
262 else:
263 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
264
265 # file open menu is clicked
266 def onFileOpen (self):
267 filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
268 if filename:
269 self.current_db = str (filename)
270 self.setWindowTitle ("BiaWeb - " + self.current_db)
271 self.repopulate_categories ()
272 self.repopulate_articles ()
273
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 ()
287
288 savefilename = PyQt4.QtGui.QFileDialog.getSaveFileName (self, "Save site database to")
289
290 if savefilename:
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)
295 if flag:
296 PyQt4.QtGui.QMessageBox.information (self, "Success",
297 "New site db successfully created")
298 self.articles.clear ()
299 self.categories.clear ()
300 else:
301 PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
302
303 # file quit is clicked
304 def onFileQuit (self):
305 sys.exit (0)