Specific boolean comparisons in code made literal
[biaweb_qt.git] / templates_dialog.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Templates dialog class
3
4 import PyQt4
5 import ui_templates_dialog
6 import editor_dialog
7 import biaweb_db
8
9 class TemplatesDialog (PyQt4.QtGui.QDialog, ui_templates_dialog.Ui_TemplatesDialog):
10 def __init__ (self, master, currentdb):
11 PyQt4.QtGui.QDialog.__init__ (self, master)
12 self.setupUi (self)
13
14 # set the current database
15 self.current_db = currentdb
16
17 # populate the templates
18 tpls = biaweb_db.get_templates (self.current_db)
19 # if templates populated
20 if tpls:
21 for tpl_name, tpl_content in tpls:
22 self.templates.addItem (str (tpl_name))
23 else:
24 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in retrieving templates")
25
26 def onEdit (self):
27 # get the currently selected list item
28 sel_item = self.templates.currentItem ()
29 # if no item is selected return
30 if sel_item is None:
31 return
32 tpl_name = str (sel_item.data (0).toString ())
33
34 # get the template content from the database
35 tpl_str = biaweb_db.get_template_text (self.current_db, tpl_name)
36
37 # if template string cannot be obtained for some reason
38 if tpl_str is False:
39 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in retrieving template")
40 else:
41 # open edit dialog with title "template name" and text as the template string
42 edlg = editor_dialog.EditorDialog (self, tpl_name, tpl_str)
43 # if editing is confirmed
44 if edlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
45 upd_tpl_str = str (edlg.text.toPlainText ()).strip ()
46
47 # update the template with the new template string
48 ret = biaweb_db.update_template (self.current_db, tpl_name, upd_tpl_str)
49 # if update failed
50 if ret is False:
51 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating template")
52