1 # BiaWeb Website content manager (c) 2010 V.Harishankar
6 import site_configuration_dialog
as scd
7 import category_dialog
as catd
11 class MainWindow (PyQt4
.QtGui
.QMainWindow
, ui_main_window
.Ui_MainWindow
):
13 PyQt4
.QtGui
.QMainWindow
.__init
__ (self
)
15 self
.current_db
= None
17 # refresh the category list
18 def repopulate_categories (self
):
19 recs
= biaweb_db
.get_categories (self
.current_db
)
21 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the categories")
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
)
29 # refresh the articles list
30 def repopulate_articles (self
, catid
=None):
31 recs
= biaweb_db
.get_articles (self
.current_db
, catid
)
33 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in getting the articles")
36 self
.articles
.clear ()
38 qrow
= PyQt4
.QtGui
.QTreeWidgetItem ([str(item
[0]), str(item
[1])])
39 self
.articles
.addTopLevelItem (qrow
)
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 ()
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
)
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")
59 dlg
= scd
.SiteConfigDialog (self
)
60 configs
= biaweb_db
.get_configuration (self
.current_db
)
63 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading configuration")
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])
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 ()
85 flag
= biaweb_db
.set_configuration (self
.current_db
, site_title
, site_url
, keywords
, description
,
86 copyright
, num_rss
, destination
)
88 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
89 "SQLite 3 error in updating configuration")
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 ()
95 # get the first column data as integer
96 selindex
= selitems
[0].data(0, 0).toInt ()[0]
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")
109 catid
= self
.get_selected_item_id (self
.categories
)
110 # if no category is selected, display an error
112 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
113 # category is selected
115 cat
= biaweb_db
.get_category (self
.current_db
, catid
)
116 # if the category cannot be retrieved from database
118 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error reading category")
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])
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 ()
133 ret
= biaweb_db
.update_category (self
.current_db
,
134 catid
, category_name
, category_desc
, category_stub
)
136 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully updated")
138 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating category")
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")
148 # get the selected category
149 catid
= self
.get_selected_item_id (self
.categories
)
151 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
152 # category is selected
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
)
159 if flag
== PyQt4
.QtGui
.QMessageBox
.Yes
:
160 ret
= biaweb_db
.remove_category (self
.current_db
, catid
)
162 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in deleting category")
164 self
.repopulate_categories ()
165 self
.repopulate_articles ()
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")
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
)
183 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully created")
184 self
.repopulate_categories ()
185 self
.repopulate_articles ()
187 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating category")
189 # file open menu is clicked
190 def onFileOpen (self
):
191 filename
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "Open Site Database")
193 self
.current_db
= str (filename
)
194 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
195 self
.repopulate_categories ()
196 self
.repopulate_articles ()
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 ()
212 savefilename
= PyQt4
.QtGui
.QFileDialog
.getSaveFileName (self
, "Save site database to")
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
)
220 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
221 "New site db successfully created")
222 self
.articles
.clear ()
223 self
.categories
.clear ()
225 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "System or SQLite 3 error in creating database")
227 # file quit is clicked
228 def onFileQuit (self
):