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
== 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 # when edit article is triggered
66 def onArticleEdit (self
):
69 # when delete article is triggered
70 def onArticleDelete (self
):
73 # when category item is activated
74 def onCategoryItemActivated (self
):
75 self
.onCategoryEdit ()
77 # when item selection is changed in categories tree widget
78 def onCategorySelectionChanged (self
):
79 if self
.current_db
is not None:
80 catid
= self
.get_selected_item_id (self
.categories
)
81 self
.repopulate_articles (catid
)
83 # when configuration menu is activated
84 def onConfiguration (self
):
85 if self
.current_db
== None:
86 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
87 "Cannot edit configuration. You need to create or open a website first")
89 dlg
= scd
.SiteConfigDialog (self
)
90 configs
= biaweb_db
.get_configuration (self
.current_db
)
93 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading configuration")
97 dlg
.site_url
.setText (configs
[0])
98 dlg
.site_title
.setText (configs
[1])
99 dlg
.keywords
.setText (configs
[2])
100 dlg
.description
.setPlainText (configs
[3])
101 dlg
.num_rss_items
.setValue (configs
[4])
102 dlg
.destination
.setText (configs
[5])
103 dlg
.copyright
.setText (configs
[6])
105 if (dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
):
106 # if accepted, get the data and store in database
107 site_title
= str (dlg
.site_title
.text ()).strip ()
108 site_url
= str (dlg
.site_url
.text ()).strip ()
109 keywords
= str (dlg
.keywords
.text ()).strip ()
110 destination
= str (dlg
.destination
.text ()).strip ()
111 description
= str (dlg
.description
.toPlainText ()).strip ()
112 num_rss
= dlg
.num_rss_items
.value ()
113 copyright
= str (dlg
.copyright
.text ()).strip ()
115 flag
= biaweb_db
.set_configuration (self
.current_db
, site_title
, site_url
, keywords
, description
,
116 copyright
, num_rss
, destination
)
118 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
119 "SQLite 3 error in updating configuration")
121 # function to get the category or article ID from current selected item in a tree widget
122 def get_selected_item_id (self
, twidget
):
123 selitems
= twidget
.selectedItems ()
125 # get the first column data as integer
126 selindex
= selitems
[0].data(0, 0).toInt ()[0]
131 # category edit action
132 def onCategoryEdit (self
):
133 # if no database is open
134 if self
.current_db
== None:
135 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
136 "Cannot edit category. You need to create or open a website first")
139 catid
= self
.get_selected_item_id (self
.categories
)
140 # if no category is selected, display an error
142 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
143 # category is selected
145 cat
= biaweb_db
.get_category (self
.current_db
, catid
)
146 # if the category cannot be retrieved from database
148 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error reading category")
151 # set the data in the dialog
152 dlg
= catd
.CategoryDialog (self
)
153 dlg
.category_name
.setText (cat
[1])
154 dlg
.category_desc
.setText (cat
[2])
155 dlg
.category_stub
.setText (cat
[3])
158 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
159 category_name
= str (dlg
.category_name
.text ()).strip ()
160 category_desc
= str (dlg
.category_desc
.text ()).strip ()
161 category_stub
= str (dlg
.category_stub
.text ()).strip ()
163 ret
= biaweb_db
.update_category (self
.current_db
,
164 catid
, category_name
, category_desc
, category_stub
)
166 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully updated")
168 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating category")
171 # category delete action
172 def onCategoryDelete (self
):
173 # if there is no database
174 if self
.current_db
== None:
175 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
176 "Cannot edit category. You need to create or open a website first")
178 # get the selected category
179 catid
= self
.get_selected_item_id (self
.categories
)
181 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
182 # category is selected
184 # get confirmation first
185 flag
= PyQt4
.QtGui
.QMessageBox
.question (self
, "Confirm",
186 "This will delete the category and all associated articles. Are you sure you wish to continue?",
187 PyQt4
.QtGui
.QMessageBox
.Yes
, PyQt4
.QtGui
.QMessageBox
.No
)
189 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
190 ret
= biaweb_db
.remove_category (self
.current_db
, catid
)
192 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting category")
194 self
.repopulate_categories ()
195 self
.repopulate_articles ()
197 # category add action
198 def onCategoryAdd (self
):
199 # if there is no database
200 if self
.current_db
== None:
201 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
202 "Cannot add category. You need to create or open a website first")
204 # show the category add dialog
205 dlg
= catd
.CategoryDialog (self
)
206 # if OK button is pressed
207 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
208 cat_name
= str (dlg
.category_name
.text ()).strip ()
209 cat_desc
= str (dlg
.category_desc
.text ()).strip ()
210 cat_stub
= str (dlg
.category_stub
.text ()).strip ()
211 ret
= biaweb_db
.create_category (self
.current_db
, cat_name
, cat_desc
, cat_stub
)
213 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully created")
214 self
.repopulate_categories ()
215 self
.repopulate_articles ()
217 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating category")
219 # file open menu is clicked
220 def onFileOpen (self
):
221 filename
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "Open Site Database")
223 self
.current_db
= str (filename
)
224 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
225 self
.repopulate_categories ()
226 self
.repopulate_articles ()
228 # file new menu is clicked
229 def onFileNew (self
):
230 # show the site configuration dialog to get the new site details
231 dlg
= scd
.SiteConfigDialog (self
)
232 # if OK button is pressed
233 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
234 site_title
= str (dlg
.site_title
.text ()).strip ()
235 site_url
= str (dlg
.site_url
.text ()).strip ()
236 keywords
= str (dlg
.keywords
.text ()).strip ()
237 destination
= str (dlg
.destination
.text ()).strip ()
238 description
= str (dlg
.description
.toPlainText ()).strip ()
239 num_rss
= dlg
.num_rss_items
.value ()
240 copyright
= str (dlg
.copyright
.text ()).strip ()
242 savefilename
= PyQt4
.QtGui
.QFileDialog
.getSaveFileName (self
, "Save site database to")
245 self
.current_db
= str (savefilename
)
246 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
247 flag
= biaweb_db
.create_db (self
.current_db
, site_title
, site_url
, keywords
, description
,
248 copyright
, num_rss
, destination
)
250 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
251 "New site db successfully created")
252 self
.articles
.clear ()
253 self
.categories
.clear ()
255 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "System or SQLite 3 error in creating database")
257 # file quit is clicked
258 def onFileQuit (self
):