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 # configuration dialog
42 def onConfiguration (self
):
43 if self
.current_db
== None:
44 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
45 "Cannot edit configuration. You need to create or open a website first")
47 dlg
= scd
.SiteConfigDialog (self
)
48 configs
= biaweb_db
.get_configuration (self
.current_db
)
51 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in reading configuration")
55 dlg
.site_url
.setText (configs
[0])
56 dlg
.site_title
.setText (configs
[1])
57 dlg
.keywords
.setText (configs
[2])
58 dlg
.description
.setPlainText (configs
[3])
59 dlg
.num_rss_items
.setValue (configs
[4])
60 dlg
.destination
.setText (configs
[5])
61 dlg
.copyright
.setText (configs
[6])
63 if (dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
):
64 # if accepted, get the data and store in database
65 site_title
= str (dlg
.site_title
.text ()).strip ()
66 site_url
= str (dlg
.site_url
.text ()).strip ()
67 keywords
= str (dlg
.keywords
.text ()).strip ()
68 destination
= str (dlg
.destination
.text ()).strip ()
69 description
= str (dlg
.description
.toPlainText ()).strip ()
70 num_rss
= dlg
.num_rss_items
.value ()
71 copyright
= str (dlg
.copyright
.text ()).strip ()
73 flag
= biaweb_db
.set_configuration (self
.current_db
, site_title
, site_url
, keywords
, description
,
74 copyright
, num_rss
, destination
)
76 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
77 "SQLite 3 error in updating configuration")
79 # function to get the category or article ID from current selected item in a tree widget
80 def get_selected_item_id (self
, twidget
):
81 selitems
= twidget
.selectedItems ()
83 # get the first column data as integer
84 selindex
= selitems
[0].data(0, 0).toInt ()[0]
89 # category edit action
90 def onCategoryEdit (self
):
91 # if no database is open
92 if self
.current_db
== None:
93 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
94 "Cannot edit category. You need to create or open a website first")
97 catid
= self
.get_selected_item_id (self
.categories
)
98 # if no category is selected, display an error
100 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "No category selected")
101 # category is selected
103 cat
= biaweb_db
.get_category (self
.current_db
, catid
)
104 # if the category cannot be retrieved from database
106 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error reading category")
109 # set the data in the dialog
110 dlg
= catd
.CategoryDialog (self
)
111 dlg
.category_name
.setText (cat
[1])
112 dlg
.category_desc
.setText (cat
[2])
113 dlg
.category_stub
.setText (cat
[3])
116 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
117 category_name
= str (dlg
.category_name
.text ()).strip ()
118 category_desc
= str (dlg
.category_desc
.text ()).strip ()
119 category_stub
= str (dlg
.category_stub
.text ()).strip ()
121 ret
= biaweb_db
.update_category (self
.current_db
,
122 catid
, category_name
, category_desc
, category_stub
)
124 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully updated")
126 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in updating category")
129 # category add action
130 def onCategoryAdd (self
):
131 # if there is no database
132 if self
.current_db
== None:
133 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error",
134 "Cannot add category. You need to create or open a website first")
136 # show the category add dialog
137 dlg
= catd
.CategoryDialog (self
)
138 # if OK button is pressed
139 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
140 cat_name
= str (dlg
.category_name
.text ()).strip ()
141 cat_desc
= str (dlg
.category_desc
.text ()).strip ()
142 cat_stub
= str (dlg
.category_stub
.text ()).strip ()
143 ret
= biaweb_db
.create_category (self
.current_db
, cat_name
, cat_desc
, cat_stub
)
145 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success", "Category successfully created")
146 self
.repopulate_categories ()
147 self
.repopulate_articles ()
149 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "SQLite 3 error in creating category")
151 # file open menu is clicked
152 def onFileOpen (self
):
153 filename
= PyQt4
.QtGui
.QFileDialog
.getOpenFileName (self
, "Open Site Database")
155 self
.current_db
= str (filename
)
156 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
157 self
.repopulate_categories ()
158 self
.repopulate_articles ()
160 # file new menu is clicked
161 def onFileNew (self
):
162 # show the site configuration dialog to get the new site details
163 dlg
= scd
.SiteConfigDialog (self
)
164 # if OK button is pressed
165 if dlg
.exec_ () == PyQt4
.QtGui
.QDialog
.Accepted
:
166 site_title
= str (dlg
.site_title
.text ()).strip ()
167 site_url
= str (dlg
.site_url
.text ()).strip ()
168 keywords
= str (dlg
.keywords
.text ()).strip ()
169 destination
= str (dlg
.destination
.text ()).strip ()
170 description
= str (dlg
.description
.toPlainText ()).strip ()
171 num_rss
= dlg
.num_rss_items
.value ()
172 copyright
= str (dlg
.copyright
.text ()).strip ()
174 savefilename
= PyQt4
.QtGui
.QFileDialog
.getSaveFileName (self
, "Save site database to")
177 self
.current_db
= str (savefilename
)
178 self
.setWindowTitle ("BiaWeb - " + self
.current_db
)
179 flag
= biaweb_db
.create_db (self
.current_db
, site_title
, site_url
, keywords
, description
,
180 copyright
, num_rss
, destination
)
182 PyQt4
.QtGui
.QMessageBox
.information (self
, "Success",
183 "New site db successfully created")
184 self
.articles
.clear ()
185 self
.categories
.clear ()
187 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Error", "System or SQLite 3 error in creating database")
189 # file quit is clicked
190 def onFileQuit (self
):