From: Harishankar Date: Mon, 29 Nov 2010 13:01:44 +0000 (+0530) Subject: Completed the template editing functionality X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=54389d5adbbb1830a7e85545b2bdde816713a272;p=biaweb_qt.git Completed the template editing functionality Completed the template editing functionality. Also removed the default templates from main code and put it in a separate module. This is for future use (e.g. for reverting templates to default feature) --- diff --git a/biaweb_db.py b/biaweb_db.py index c2761f5..b310972 100644 --- a/biaweb_db.py +++ b/biaweb_db.py @@ -6,6 +6,8 @@ import os import os.path import time +import biaweb_strings as bws + # function to get the template text for a particular template def get_template_text (dbname, template_name): try: @@ -20,6 +22,19 @@ def get_template_text (dbname, template_name): except sqlite3.Error: return False +# function to update the template +def update_template (dbname, template_name, template_str): + try: + conn = sqlite3.connect (dbname) + c = conn.cursor () + c.execute ("UPDATE templates SET template_content=? WHERE template_name=?", + (template_str, template_name)) + conn.commit () + conn.close () + return True + except sqlite3.Error: + return False + # function to retrieve the templates from the database def get_templates (dbname): try: @@ -233,6 +248,8 @@ def create_db (dbname, site_title, site_url, keywords, description, copyright, try: conn = sqlite3.connect (dbname) c = conn.cursor () + + # create the tables c.execute ("CREATE TABLE IF NOT EXISTS \ categories (cid INTEGER PRIMARY KEY, \ name TEXT, desc TEXT, \ @@ -248,203 +265,17 @@ def create_db (dbname, site_title, site_url, keywords, description, copyright, c.execute ("CREATE TABLE IF NOT EXISTS \ templates (template_name TEXT, template_content);") - template_main = """ - - - -${site_title} - - - - - - - - - -
- ${contents_bit} -
- - - -""" - - template_article_bit = """

${article_title}

-
Created: ${article_cdate} | Last modified: ${article_mdate}
-
Rating: ${rating}
-
${article_contents}
-""" - - template_news_item_bit = """

${news_title}

-
${news_datetime}
-
${news_description}
-""" - - template_index_bit = """

Welcome to ${site_name}

-
-Welcome to my site, ${site_name}. -
-

Latest Articles

-${news_updates} -""" - - template_table_bit = """

${category_title}

-

${category_desc}

- - - - - - - - - - ${table_rows} - -
TitleCreated onRated
-""" - - template_tablerow_bit = """ - ${title} - ${created} - ${rating} - -""" - - template_style = """body { - font-family: "Bitstream Vera Sans", Verdana, Arial, Sans Serif; - font-size: 0.9em; - background-color: #ffffff; - color: #000000; - margin: auto -} -#head { - width: 98%; - background-color: #efefef; - padding: 1%; - text-align: center; -} - -#main { - width: 73%; - padding: 1%; - float: left; -} - -#sidebar { - width: 23%; - padding: 1%; - float: right; -} - -#footer { - width: 100%; - padding-top: 5px; - padding-bottom: 5px; - font-size: 0.9em; - text-align: center; - float: left; - background-color: #efefef; -} - -.headerdesc { - font-variant: small-caps; - font-size: 1.1em; -} - -.content { - text-align: justify; - line-height: 1.1em; -} - -.categorytable { - width: 100%; -} - -.categorytable thead { - font-weight: bold; -} - -.modified { - font-size: 0.8em; - color: #666666; -} - -.rating { - font-size: 0.8em; - color: #666666; -} - -h1, h2, h3 { - font-family: "Bitstream Vera Serif", Serif; - padding: 0; - margin: 0; - margin-top: 5px; - margin-bottom: 5px; -} - -hr { - border: 0; - border-bottom: 1px solid; - border-color: #888888; -} - -h1 { - font-size: 2.4em; - color: #000099; - -} -h1 a, h1 a:hover, h1 a:visited, h2 a:active { - text-decoration: none; -} -h2 { - font-size: 1.4em; - background-color: #efefef; -} -h2 a, h2 a:hover, h2 a:visited, h2 a:active { - text-decoration: none; -} -h3 { - font-size: 1.2em; -a { - color: #0000dd; -} -a:visited { - color: #0000aa; -} -a:active, a:hover { - color: #0000ff; -}""" - + # insert the templates into the newly created database c.executemany ("INSERT INTO templates (template_name, template_content) VALUES (?, ?);", - [["main_template", template_main], - ["article_bit", template_article_bit], - ["news_bit", template_news_item_bit], - ["table_bit", template_table_bit], - ["tablerow_bit", template_tablerow_bit], - ["stylesheet", template_style], - ["index_bit", template_index_bit]]) - + [["main_template", bws.template_main], + ["article_bit", bws.template_article_bit], + ["news_bit", bws.template_news_item_bit], + ["table_bit", bws.template_table_bit], + ["tablerow_bit", bws.template_tablerow_bit], + ["stylesheet", bws.template_style], + ["index_bit", bws.template_index_bit]]) + + # inser the configuration c.executemany ("INSERT INTO config (config_name, config_param) VALUES (?, ?); ", [["Website URL", site_url], ["Website Title", site_title], diff --git a/biaweb_strings.py b/biaweb_strings.py new file mode 100644 index 0000000..62d1f5e --- /dev/null +++ b/biaweb_strings.py @@ -0,0 +1,190 @@ +# BiaWeb Website content manager (c) 2010 V.Harishankar +# Strings class + +template_main = """ + + + +${site_title} + + + + + + + + + +
+ ${contents_bit} +
+ + + +""" + +template_article_bit = """

${article_title}

+
Created: ${article_cdate} | Last modified: ${article_mdate}
+
Rating: ${rating}
+
${article_contents}
+""" + +template_news_item_bit = """

${news_title}

+
${news_datetime}
+
${news_description}
+""" + +template_index_bit = """

Welcome to ${site_name}

+
+Welcome to my site, ${site_name}. +
+

Latest Articles

+${news_updates} +""" + +template_table_bit = """

${category_title}

+

${category_desc}

+ + + + + + + + + + ${table_rows} + +
TitleCreated onRated
+""" + +template_tablerow_bit = """ + ${title} + ${created} + ${rating} + +""" + +template_style = """body { + font-family: "Bitstream Vera Sans", Verdana, Arial, Sans Serif; + font-size: 0.9em; + background-color: #ffffff; + color: #000000; + margin: auto +} +#head { + width: 98%; + background-color: #efefef; + padding: 1%; + text-align: center; +} + +#main { + width: 73%; + padding: 1%; + float: left; +} + +#sidebar { + width: 23%; + padding: 1%; + float: right; +} + +#footer { + width: 100%; + padding-top: 5px; + padding-bottom: 5px; + font-size: 0.9em; + text-align: center; + float: left; + background-color: #efefef; +} + +.headerdesc { + font-variant: small-caps; + font-size: 1.1em; +} + +.content { + text-align: justify; + line-height: 1.1em; +} + +.categorytable { + width: 100%; +} + +.categorytable thead { + font-weight: bold; +} + +.modified { + font-size: 0.8em; + color: #666666; +} + +.rating { + font-size: 0.8em; + color: #666666; +} + +h1, h2, h3 { + font-family: "Bitstream Vera Serif", Serif; + padding: 0; + margin: 0; + margin-top: 5px; + margin-bottom: 5px; +} + +hr { + border: 0; + border-bottom: 1px solid; + border-color: #888888; +} + +h1 { + font-size: 2.4em; + color: #000099; + +} +h1 a, h1 a:hover, h1 a:visited, h2 a:active { + text-decoration: none; +} +h2 { + font-size: 1.4em; + background-color: #efefef; +} +h2 a, h2 a:hover, h2 a:visited, h2 a:active { + text-decoration: none; +} +h3 { + font-size: 1.2em; +a { + color: #0000dd; +} +a:visited { + color: #0000aa; +} +a:active, a:hover { + color: #0000ff; +}""" \ No newline at end of file diff --git a/editor_dialog.py b/editor_dialog.py index 9c35a22..e19101e 100644 --- a/editor_dialog.py +++ b/editor_dialog.py @@ -17,9 +17,9 @@ class EditorDialog (PyQt4.QtGui.QDialog, ui_editor_dialog.Ui_EditorDialog): self.setWindowTitle ("Editor - " + title) # set text highlighting - highlighter.SimpleHtmlHighlighter (self.template_text.document ()) + highlighter.SimpleHtmlHighlighter (self.text.document ()) - self.template_text.setPlainText (init_text) + self.text.setPlainText (init_text) # set modified flag to false self.modified = False diff --git a/editor_dialog.ui b/editor_dialog.ui index 8a86b7c..e652b6a 100644 --- a/editor_dialog.ui +++ b/editor_dialog.ui @@ -15,7 +15,7 @@ - + Monospace @@ -79,7 +79,7 @@ - template_text + text textChanged() EditorDialog onTextChanged() diff --git a/templates_dialog.py b/templates_dialog.py index efd5bd7..64cc70c 100644 --- a/templates_dialog.py +++ b/templates_dialog.py @@ -21,10 +21,10 @@ class TemplatesDialog (PyQt4.QtGui.QDialog, ui_templates_dialog.Ui_TemplatesDial def onEdit (self): # get the currently selected list item - tpl_name = self.templates.currentItem ().data (0).toString () + tpl_name = str (self.templates.currentItem ().data (0).toString ()) # get the template content from the database - tpl_str = biaweb_db.get_template_text (self.current_db, str (tpl_name)) + tpl_str = biaweb_db.get_template_text (self.current_db, tpl_name) # if template string cannot be obtained for some reason if tpl_str == False: @@ -32,4 +32,13 @@ class TemplatesDialog (PyQt4.QtGui.QDialog, ui_templates_dialog.Ui_TemplatesDial else: # open edit dialog with title "template name" and text as the template string edlg = editor_dialog.EditorDialog (self, tpl_name, tpl_str) - edlg.exec_ () + # if editing is confirmed + if edlg.exec_ () == PyQt4.QtGui.QDialog.Accepted: + upd_tpl_str = str (edlg.text.toPlainText ()).strip () + + # update the template with the new template string + ret = biaweb_db.update_template (self.current_db, tpl_name, upd_tpl_str) + # if update failed + if ret == False: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating template") + diff --git a/templates_dialog.ui b/templates_dialog.ui index d8e5a04..93e391d 100644 --- a/templates_dialog.ui +++ b/templates_dialog.ui @@ -10,6 +10,18 @@ 256 + + + 350 + 256 + + + + + 350 + 256 + + Templates diff --git a/ui_editor_dialog.py b/ui_editor_dialog.py index 62b5e44..e9d7a25 100644 --- a/ui_editor_dialog.py +++ b/ui_editor_dialog.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'editor_dialog.ui' # -# Created: Mon Nov 29 17:43:54 2010 +# Created: Mon Nov 29 18:18:36 2010 # by: PyQt4 UI code generator 4.7.4 # # WARNING! All changes made in this file will be lost! @@ -15,15 +15,15 @@ class Ui_EditorDialog(object): EditorDialog.resize(663, 463) self.gridLayout = QtGui.QGridLayout(EditorDialog) self.gridLayout.setObjectName("gridLayout") - self.template_text = QtGui.QPlainTextEdit(EditorDialog) + self.text = QtGui.QPlainTextEdit(EditorDialog) font = QtGui.QFont() font.setFamily("Monospace") - self.template_text.setFont(font) - self.template_text.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) - self.template_text.setTabChangesFocus(True) - self.template_text.setLineWrapMode(QtGui.QPlainTextEdit.NoWrap) - self.template_text.setObjectName("template_text") - self.gridLayout.addWidget(self.template_text, 0, 0, 1, 1) + self.text.setFont(font) + self.text.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) + self.text.setTabChangesFocus(True) + self.text.setLineWrapMode(QtGui.QPlainTextEdit.NoWrap) + self.text.setObjectName("text") + self.gridLayout.addWidget(self.text, 0, 0, 1, 1) self.buttonBox = QtGui.QDialogButtonBox(EditorDialog) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) @@ -33,7 +33,7 @@ class Ui_EditorDialog(object): self.retranslateUi(EditorDialog) QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), EditorDialog.accept) QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), EditorDialog.reject) - QtCore.QObject.connect(self.template_text, QtCore.SIGNAL("textChanged()"), EditorDialog.onTextChanged) + QtCore.QObject.connect(self.text, QtCore.SIGNAL("textChanged()"), EditorDialog.onTextChanged) QtCore.QMetaObject.connectSlotsByName(EditorDialog) def retranslateUi(self, EditorDialog): diff --git a/ui_templates_dialog.py b/ui_templates_dialog.py index 7c2fce4..9912eff 100644 --- a/ui_templates_dialog.py +++ b/ui_templates_dialog.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'templates_dialog.ui' # -# Created: Mon Nov 29 16:38:12 2010 +# Created: Mon Nov 29 18:11:30 2010 # by: PyQt4 UI code generator 4.7.4 # # WARNING! All changes made in this file will be lost! @@ -13,6 +13,8 @@ class Ui_TemplatesDialog(object): def setupUi(self, TemplatesDialog): TemplatesDialog.setObjectName("TemplatesDialog") TemplatesDialog.resize(350, 256) + TemplatesDialog.setMinimumSize(QtCore.QSize(350, 256)) + TemplatesDialog.setMaximumSize(QtCore.QSize(350, 256)) self.gridLayout = QtGui.QGridLayout(TemplatesDialog) self.gridLayout.setObjectName("gridLayout") self.label = QtGui.QLabel(TemplatesDialog)