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
)
63 # when edit article is triggered
64 def onArticleEdit (self
):
67 # when delete article is triggered
68 def onArticleDelete (self
):
71 # when item selection is changed in categories tree widget
72 def onCategorySelectionChanged (self
):
73 if self
.current_db
is not None:
74 catid
= self
.get_selected_item_id (self
.categories
)
75 self
.repopulate_articles (catid
)
77 # when configuration menu is activated
78 def onConfiguration (self
):
79 if self
.current_db
== None:
80 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
81 "Cannot edit configuration. You need to create or open a website first")
83 dlg
= scd
.SiteConfigDialog (self
)
84 configs
= biaweb_db
.get_configuration (self
.current_db
)
87 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading configuration")
91 dlg
.site_url
.setText (configs
[0])
92 dlg
.site_title
.setText (configs
[1])
93 dlg
.keywords
.setText (configs
[2])
94 dlg
.description
.setPlainText (configs
[3])
95 dlg
.num_rss_items
.setValue (configs
[4])
96 dlg
.destination
.setText (configs
[5])
97 dlg
.copyright
.setText (configs
[6])
99 if (dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
):
100 # if accepted, get the data and store in database
101 site_title
= str (dlg
.site_title
.text ()).strip ()
102 site_url
= str (dlg
.site_url
.text ()).strip ()
103 keywords
= str (dlg
.keywords
.text ()).strip ()
104 destination
= str (dlg
.destination
.text ()).strip ()
105 description
= str (dlg
.description
.toPlainText ()).strip ()
106 num_rss
= dlg
.num_rss_items
.value ()
107 copyright
= str (dlg
.copyright
.text ()).strip ()
109 flag
= biaweb_db
.set_configuration (self
.current_db
, site_title
, site_url
, keywords
, description
,
110 copyright
, num_rss
, destination
)
112 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
113 "SQLite 3 error in updating configuration")
115 # function to get the category or article ID from current selected item in a tree widget
116 def get_selected_item_id (self
, twidget
):
117 selitems
= twidget
.selectedItems ()
119 # get the first column data as integer
120 selindex
= selitems
[0].data(0, 0).toInt ()[0]
125 # category edit action
126 def onCategoryEdit (self
):
127 # if no database is open
128 if self
.current_db
== None:
129 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
130 "Cannot edit category. You need to create or open a website first")
133 catid
= self
.get_selected_item_id (self
.categories
)
134 # if no category is selected, display an error
136 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
137 # category is selected
139 cat
= biaweb_db
.get_category (self
.current_db
, catid
)
140 # if the category cannot be retrieved from database
142 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error reading category")
145 # set the data in the dialog
146 dlg
= catd
.CategoryDialog (self
)
147 dlg
.category_name
.setText (cat
[1])
148 dlg
.category_desc
.setText (cat
[2])
149 dlg
.category_stub
.setText (cat
[3])
152 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
153 category_name
= str (dlg
.category_name
.text ()).strip ()
154 category_desc
= str (dlg
.category_desc
.text ()).strip ()
155 category_stub
= str (dlg
.category_stub
.text ()).strip ()
157 ret
= biaweb_db
.update_category (self
.current_db
,
158 catid
, category_name
, category_desc
, category_stub
)
160 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully updated")
162 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating category")
165 # category delete action
166 def onCategoryDelete (self
):
167 # if there is no database
168 if self
.current_db
== None:
169 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
170 "Cannot edit category. You need to create or open a website first")
172 # get the selected category
173 catid
= self
.get_selected_item_id (self
.categories
)
175 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
176 # category is selected
178 # get confirmation first
179 flag
= PyQt4
.QtGui
.QMessageBox
.question (self
, "Confirm",
180 "This will delete the category and all associated articles. Are you sure you wish to continue?",
181 PyQt4
.QtGui
.QMessageBox
.Yes
, PyQt4
.QtGui
.QMessageBox
.No
)
183 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
184 ret
= biaweb_db
.remove_category (self
.current_db
, catid
)
186 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting category")
188 self
.repopulate_categories ()
189 self
.repopulate_articles ()
191 # category add action
192 def onCategoryAdd (self
):
193 # if there is no database
194 if self
.current_db
== None:
195 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
196 "Cannot add category. You need to create or open a website first")
198 # show the category add dialog
199 dlg
= catd
.CategoryDialog (self
)
200 # if OK button is pressed
201 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
202 cat_name
= str (dlg
.category_name
.text ()).strip ()
203 cat_desc
= str (dlg
.category_desc
.text ()).strip ()
204 cat_stub
= str (dlg
.category_stub
.text ()).strip ()
205 ret
= biaweb_db
.create_category (self
.current_db
, cat_name
, cat_desc
, cat_stub
)
207 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully created")
208 self
.repopulate_categories ()
209 self
.repopulate_articles ()
211 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating category")
213 # file open menu is clicked
214 def onFileOpen (self
):
215 filename
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "Open Site Database")
217 self
.current_db
= str (filename
)
218 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
219 self
.repopulate_categories ()
220 self
.repopulate_articles ()
222 # file new menu is clicked
223 def onFileNew (self
):
224 # show the site configuration dialog to get the new site details
225 dlg
= scd
.SiteConfigDialog (self
)
226 # if OK button is pressed
227 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
228 site_title
= str (dlg
.site_title
.text ()).strip ()
229 site_url
= str (dlg
.site_url
.text ()).strip ()
230 keywords
= str (dlg
.keywords
.text ()).strip ()
231 destination
= str (dlg
.destination
.text ()).strip ()
232 description
= str (dlg
.description
.toPlainText ()).strip ()
233 num_rss
= dlg
.num_rss_items
.value ()
234 copyright
= str (dlg
.copyright
.text ()).strip ()
236 savefilename
= PyQt4
.QtGui
.QFileDialog
.getSaveFileName (self
, "Save site database to")
239 self
.current_db
= str (savefilename
)
240 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
241 flag
= biaweb_db
.create_db (self
.current_db
, site_title
, site_url
, keywords
, description
,
242 copyright
, num_rss
, destination
)
244 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
245 "New site db successfully created")
246 self
.articles
.clear ()
247 self
.categories
.clear ()
249 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "System or SQLite 3 error in creating database")
251 # file quit is clicked
252 def onFileQuit (self
):