Implemented category delete and more
[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 biaweb_db
9 import sys
10
11 class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
12 def __init__ (self):
13 PyQt4.QtGui.QMainWindow.__init__ (self)
14 self.setupUi (self)
15 self.current_db = None
16
17 # refresh the category list
18 def repopulate_categories (self):
19 recs = biaweb_db.get_categories (self.current_db)
20 if recs == False:
21 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories")
22 return
23
24 self.categories.clear ()
25 for (id, name, desc, stub) in recs:
26 qrow = PyQt4.QtGui.QTreeWidgetItem ([str(id), str(name)])
27 self.categories.addTopLevelItem (qrow)
28
29 # refresh the articles list
30 def repopulate_articles (self, catid=None):
31 recs = biaweb_db.get_articles (self.current_db, catid)
32 if recs == False:
33 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles")
34 return
35
36 self.articles.clear ()
37 for item in recs:
38 qrow = PyQt4.QtGui.QTreeWidgetItem ([str(item[0]), str(item[1])])
39 self.articles.addTopLevelItem (qrow)
40
41 # when the view is refreshed
42 def onViewRefresh (self):
43 if self.current_db is not None:
44 self.repopulate_categories ()
45 self.repopulate_articles ()
46
47 # when item selection is changed in categories tree widget
48 def onCategorySelectionChanged (self):
49 if self.current_db is not None:
50 catid = self.get_selected_item_id (self.categories)
51 self.repopulate_articles (catid)
52
53 # when configuration menu is activated
54 def onConfiguration (self):
55 if self.current_db == None:
56 PyQt4.QtGui.QMessageBox.critical (self, "Error",
57 "Cannot edit configuration. You need to create or open a website first")
58 else:
59 dlg = scd.SiteConfigDialog (self)
60 configs = biaweb_db.get_configuration (self.current_db)
61
62 if configs == False:
63 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration")
64 return
65
66 # set the data
67 dlg.site_url.setText (configs[0])
68 dlg.site_title.setText (configs[1])
69 dlg.keywords.setText (configs[2])
70 dlg.description.setPlainText (configs[3])
71 dlg.num_rss_items.setValue (configs[4])
72 dlg.destination.setText (configs[5])
73 dlg.copyright.setText (configs[6])
74
75 if (dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted):
76 # if accepted, get the data and store in database
77 site_title = str (dlg.site_title.text ()).strip ()
78 site_url = str (dlg.site_url.text ()).strip ()
79 keywords = str (dlg.keywords.text ()).strip ()
80 destination = str (dlg.destination.text ()).strip ()
81 description = str (dlg.description.toPlainText ()).strip ()
82 num_rss = dlg.num_rss_items.value ()
83 copyright = str (dlg.copyright.text ()).strip ()
84 # database update
85 flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
86 copyright, num_rss, destination)
87 if flag == False:
88 PyQt4.QtGui.QMessageBox.critical (self, "Error",
89 "SQLite 3 error in updating configuration")
90
91 # function to get the category or article ID from current selected item in a tree widget
92 def get_selected_item_id (self, twidget):
93 selitems = twidget.selectedItems ()
94 if selitems:
95 # get the first column data as integer
96 selindex = selitems[0].data(0, 0).toInt ()[0]
97 return selindex
98 else:
99 return None
100
101 # category edit action
102 def onCategoryEdit (self):
103 # if no database is open
104 if self.current_db == None:
105 PyQt4.QtGui.QMessageBox.critical (self, "Error",
106 "Cannot edit category. You need to create or open a website first")
107 # database is open
108 else:
109 catid = self.get_selected_item_id (self.categories)
110 # if no category is selected, display an error
111 if catid is None:
112 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
113 # category is selected
114 else:
115 cat = biaweb_db.get_category (self.current_db, catid)
116 # if the category cannot be retrieved from database
117 if cat == False:
118 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error reading category")
119 return
120
121 # set the data in the dialog
122 dlg = catd.CategoryDialog (self)
123 dlg.category_name.setText (cat[1])
124 dlg.category_desc.setText (cat[2])
125 dlg.category_stub.setText (cat[3])
126
127 # if accepted
128 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
129 category_name = str (dlg.category_name.text ()).strip ()
130 category_desc = str (dlg.category_desc.text ()).strip ()
131 category_stub = str (dlg.category_stub.text ()).strip ()
132
133 ret = biaweb_db.update_category (self.current_db,
134 catid, category_name, category_desc, category_stub)
135 if ret == True:
136 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated")
137 else:
138 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category")
139
140
141 # category delete action
142 def onCategoryDelete (self):
143 # if there is no database
144 if self.current_db == None:
145 PyQt4.QtGui.QMessageBox.critical (self, "Error",
146 "Cannot edit category. You need to create or open a website first")
147 else:
148 # get the selected category
149 catid = self.get_selected_item_id (self.categories)
150 if catid is None:
151 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
152 # category is selected
153 else:
154 # get confirmation first
155 flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
156 "This will delete the category and all associated articles. Are you sure you wish to continue?",
157 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
158 # if confirmed
159 if flag == PyQt4.QtGui.QMessageBox.Yes:
160 ret = biaweb_db.remove_category (self.current_db, catid)
161 if ret == False:
162 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting category")
163 else:
164 self.repopulate_categories ()
165 self.repopulate_articles ()
166
167 # category add action
168 def onCategoryAdd (self):
169 # if there is no database
170 if self.current_db == None:
171 PyQt4.QtGui.QMessageBox.critical (self, "Error",
172 "Cannot add category. You need to create or open a website first")
173 else:
174 # show the category add dialog
175 dlg = catd.CategoryDialog (self)
176 # if OK button is pressed
177 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
178 cat_name = str (dlg.category_name.text ()).strip ()
179 cat_desc = str (dlg.category_desc.text ()).strip ()
180 cat_stub = str (dlg.category_stub.text ()).strip ()
181 ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub)
182 if ret == True:
183 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
184 self.repopulate_categories ()
185 self.repopulate_articles ()
186 else:
187 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
188
189 # file open menu is clicked
190 def onFileOpen (self):
191 filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
192 if filename:
193 self.current_db = str (filename)
194 self.setWindowTitle ("BiaWeb - " + self.current_db)
195 self.repopulate_categories ()
196 self.repopulate_articles ()
197
198 # file new menu is clicked
199 def onFileNew (self):
200 # show the site configuration dialog to get the new site details
201 dlg = scd.SiteConfigDialog (self)
202 # if OK button is pressed
203 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
204 site_title = str (dlg.site_title.text ()).strip ()
205 site_url = str (dlg.site_url.text ()).strip ()
206 keywords = str (dlg.keywords.text ()).strip ()
207 destination = str (dlg.destination.text ()).strip ()
208 description = str (dlg.description.toPlainText ()).strip ()
209 num_rss = dlg.num_rss_items.value ()
210 copyright = str (dlg.copyright.text ()).strip ()
211
212 savefilename = PyQt4.QtGui.QFileDialog.getSaveFileName (self, "Save site database to")
213
214 if savefilename:
215 self.current_db = str (savefilename)
216 self.setWindowTitle ("BiaWeb - " + self.current_db)
217 flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
218 copyright, num_rss, destination)
219 if flag == True:
220 PyQt4.QtGui.QMessageBox.information (self, "Success",
221 "New site db successfully created")
222 self.articles.clear ()
223 self.categories.clear ()
224 else:
225 PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
226
227 # file quit is clicked
228 def onFileQuit (self):
229 sys.exit (0)