Completed the template editing functionality
[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 for tpl_name, tpl_content in tpls:
20 self.templates.addItem (str (tpl_name))
21
22 def onEdit (self):
23 # get the currently selected list item
24 tpl_name = str (self.templates.currentItem ().data (0).toString ())
25
26 # get the template content from the database
27 tpl_str = biaweb_db.get_template_text (self.current_db, tpl_name)
28
29 # if template string cannot be obtained for some reason
30 if tpl_str == False:
31 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in retrieving template")
32 else:
33 # open edit dialog with title "template name" and text as the template string
34 edlg = editor_dialog.EditorDialog (self, tpl_name, tpl_str)
35 # if editing is confirmed
36 if edlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
37 upd_tpl_str = str (edlg.text.toPlainText ()).strip ()
38
39 # update the template with the new template string
40 ret = biaweb_db.update_template (self.current_db, tpl_name, upd_tpl_str)
41 # if update failed
42 if ret == False:
43 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating template")
44