Site configuration updating implemented
[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 # 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")
46 else:
47 dlg = scd.SiteConfigDialog (self)
48 configs = biaweb_db.get_configuration (self.current_db)
49
50 dlg.site_url.setText (configs[0])
51 dlg.site_title.setText (configs[1])
52 dlg.keywords.setText (configs[2])
53 dlg.description.setPlainText (configs[3])
54 dlg.num_rss_items.setValue (configs[4])
55 dlg.destination.setText (configs[5])
56 dlg.copyright.setText (configs[6])
57
58 if (dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted):
59 site_title = str (dlg.site_title.text ()).strip ()
60 site_url = str (dlg.site_url.text ()).strip ()
61 keywords = str (dlg.keywords.text ()).strip ()
62 destination = str (dlg.destination.text ()).strip ()
63 description = str (dlg.description.toPlainText ()).strip ()
64 num_rss = dlg.num_rss_items.value ()
65 copyright = str (dlg.copyright.text ()).strip ()
66 flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
67 copyright, num_rss, destination)
68 if flag == False:
69 PyQt4.QtGui.QMessageBox.critical (self, "Error",
70 "SQLite 3 error in updating configuration")
71
72
73 # category add action
74 def onCategoryAdd (self):
75 # if there is no database
76 if self.current_db == None:
77 PyQt4.QtGui.QMessageBox.critical (self, "Error",
78 "Cannot add category. You need to create or open a website first")
79 else:
80 # show the category add dialog
81 dlg = catd.CategoryDialog (self)
82 # if OK button is pressed
83 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
84 cat_name = str (dlg.category_name.text ()).strip ()
85 cat_desc = str (dlg.category_desc.text ()).strip ()
86 cat_stub = str (dlg.category_stub.text ()).strip ()
87 ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub)
88 if ret == True:
89 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
90 self.repopulate_categories ()
91 self.repopulate_articles ()
92 else:
93 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
94
95 # file open menu is clicked
96 def onFileOpen (self):
97 filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
98 if filename:
99 self.current_db = str (filename)
100 self.setWindowTitle ("BiaWeb - " + self.current_db)
101 self.repopulate_categories ()
102 self.repopulate_articles ()
103
104 # file new menu is clicked
105 def onFileNew (self):
106 # show the site configuration dialog to get the new site details
107 dlg = scd.SiteConfigDialog (self)
108 # if OK button is pressed
109 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
110 site_title = str (dlg.site_title.text ()).strip ()
111 site_url = str (dlg.site_url.text ()).strip ()
112 keywords = str (dlg.keywords.text ()).strip ()
113 destination = str (dlg.destination.text ()).strip ()
114 description = str (dlg.description.toPlainText ()).strip ()
115 num_rss = dlg.num_rss_items.value ()
116 copyright = str (dlg.copyright.text ()).strip ()
117
118 savefilename = PyQt4.QtGui.QFileDialog.getSaveFileName (self, "Save site database to")
119
120 if savefilename:
121 self.current_db = str (savefilename)
122 self.setWindowTitle ("BiaWeb - " + self.current_db)
123 flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
124 copyright, num_rss, destination)
125 if flag == True:
126 PyQt4.QtGui.QMessageBox.information (self, "Success",
127 "New site db successfully created")
128 self.articles.clear ()
129 self.categories.clear ()
130 else:
131 PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
132
133 # file quit is clicked
134 def onFileQuit (self):
135 sys.exit (0)