Modified the template editing setup
authorHarishankar <v.harishankar@gmail.com>
Mon, 29 Nov 2010 10:01:07 +0000 (15:31 +0530)
committerHarishankar <v.harishankar@gmail.com>
Mon, 29 Nov 2010 10:01:07 +0000 (15:31 +0530)
Changed the main template editing setup since the
originally planned concept seems tricky to implement.
Now a separate editor window will be opened for template
editing.

biaweb_db.py
main_window.py
templates_dialog.py
templates_dialog.ui
ui_templates_dialog.py

index dd3e333..80e3915 100644 (file)
@@ -6,6 +6,19 @@ import os
 import os.path
 import time
 
+# function to retrieve the templates from the database
+def get_templates (dbname):
+       try:
+               conn = sqlite3.connect (dbname)
+               c = conn.cursor ()
+               c.execute ("SELECT * FROM templates;")
+               conn.commit ()
+               rows = c.fetchall ()
+               conn.close ()
+               return rows
+       except sqlite3.Error:
+               return False
+
 # function to get an article from the database
 def get_article (dbname, artid):
        try:
@@ -222,192 +235,192 @@ def create_db (dbname, site_title, site_url, keywords, description, copyright,
                                        templates (template_name TEXT, template_content);")
 
                template_main = """<?xml version="1.0" encoding="UTF-8"?>
-       <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-       <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-       <head>
-       <title>${site_title}</title>
-       <base href="${site_url}" />
-       <meta name="generator" content="BiaWeb"/>
-       <meta name="keywords" content="${meta_keywords}"/>
-       <meta name="description" content="${meta_description}"/>
-       <link rel="StyleSheet" type="text/css" href="style.css" />
-       <link rel="alternate" type="application/rss+xml" title="Entries RSS 2.0" href="subscribe.xml" />
-       </head>
-       <body>
-               <div id="head">
-                       <h1><a href="${site_url}">${page_title}</a></h1>
-                       <div class="headerdesc">${page_desc}</div>
-               </div>
-               <div id="main">
-                       ${contents_bit}
-               </div>
-               <div id="sidebar">
-                       <h2>Categories</h2>
-                       ${list_of_categories}
-                       <h2>Best rated</h2>
-                       ${list_best_rated}
-                       <h2>Subscribe</h2>
-                       <a href="subscribe.xml">Latest articles (RSS)</a>
-                       <h2>Search</h2>
-                       <form action="cgi-bin/search.py" method="post" enctype="multipart/form-data">
-                                               <p><input type="text" name="query" maxlength="255" style="width:142px;border: 1px inset #5A5A5A; color:#5A5A5A; background-color:#FFFFFF;" value="" /><br />
-                                               <input type="submit" value="Search" /><br />
-                                               <input type="hidden" name="fromsearch" value="fromsearch" />
-                                               <input type="radio" name="criteria" value="1" checked="checked" />All words<br />
-                                               <input type="radio" name="criteria" value="2" />Any word</p>
-                       </form>
-               </div>
-               <div id="footer">${copyright}<br />Site generated by
-               <a href=\"http://harishankar.org/software/biaweb.php\">BiaWeb</a> created by V. Harishankar</div>
-       </body>
-       </html>"""
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+<title>${site_title}</title>
+<base href="${site_url}" />
+<meta name="generator" content="BiaWeb"/>
+<meta name="keywords" content="${meta_keywords}"/>
+<meta name="description" content="${meta_description}"/>
+<link rel="StyleSheet" type="text/css" href="style.css" />
+<link rel="alternate" type="application/rss+xml" title="Entries RSS 2.0" href="subscribe.xml" />
+</head>
+<body>
+       <div id="head">
+               <h1><a href="${site_url}">${page_title}</a></h1>
+               <div class="headerdesc">${page_desc}</div>
+       </div>
+       <div id="main">
+               ${contents_bit}
+       </div>
+       <div id="sidebar">
+               <h2>Categories</h2>
+               ${list_of_categories}
+               <h2>Best rated</h2>
+               ${list_best_rated}
+               <h2>Subscribe</h2>
+               <a href="subscribe.xml">Latest articles (RSS)</a>
+               <h2>Search</h2>
+               <form action="cgi-bin/search.py" method="post" enctype="multipart/form-data">
+                                       <p><input type="text" name="query" maxlength="255" style="width:142px;border: 1px inset #5A5A5A; color:#5A5A5A; background-color:#FFFFFF;" value="" /><br />
+                                       <input type="submit" value="Search" /><br />
+                                       <input type="hidden" name="fromsearch" value="fromsearch" />
+                                       <input type="radio" name="criteria" value="1" checked="checked" />All words<br />
+                                       <input type="radio" name="criteria" value="2" />Any word</p>
+               </form>
+       </div>
+       <div id="footer">${copyright}<br />Site generated by
+       <a href=\"http://harishankar.org/software/biaweb.php\">BiaWeb</a> created by V. Harishankar</div>
+</body>
+</html>"""
 
                template_article_bit = """<h2>${article_title}</h2>
-       <div class="modified">Created: ${article_cdate} | Last modified: ${article_mdate}</div>
-       <div class="rating">Rating: ${rating}</div>
-       <div class="content">${article_contents}</div>
-       """
+<div class="modified">Created: ${article_cdate} | Last modified: ${article_mdate}</div>
+<div class="rating">Rating: ${rating}</div>
+<div class="content">${article_contents}</div>
+"""
 
                template_news_item_bit = """<h3><a href="${news_link}">${news_title}</a></h3>
-       <div class="modified">${news_datetime}</div>
-       <div class="content">${news_description}</div>
-       """
+<div class="modified">${news_datetime}</div>
+<div class="content">${news_description}</div>
+"""
 
                template_index_bit = """<h2>Welcome to ${site_name}</h2>
-       <div class="content">
-       Welcome to my site, ${site_name}.
-       </div>
-       <h2>Latest Articles</h2>
-       ${news_updates}
-       """
+<div class="content">
+Welcome to my site, ${site_name}.
+</div>
+<h2>Latest Articles</h2>
+${news_updates}
+"""
 
                template_table_bit = """<h2>${category_title}</h2>
-                       <p>${category_desc}</p>
-               <table class="categorytable">
-               <thead>
-                       <tr>
-                               <td style="width:50%">Title</td>
-                               <td>Created on</td>
-                               <td>Rated</td>
-                       </tr>
-               </thead>
-               <tbody>
-                       ${table_rows}
-               </tbody>
-       </table>
-       """
+       <p>${category_desc}</p>
+<table class="categorytable">
+<thead>
+       <tr>
+               <td style="width:50%">Title</td>
+               <td>Created on</td>
+               <td>Rated</td>
+       </tr>
+</thead>
+<tbody>
+       ${table_rows}
+</tbody>
+</table>
+"""
 
                template_tablerow_bit = """<tr>
-               <td style="width:50%"><a href="${article_url}">${title}</a></td>
-               <td>${created}</td>
-               <td>${rating}</td>
-       </tr>
-       """
+       <td style="width:50%"><a href="${article_url}">${title}</a></td>
+       <td>${created}</td>
+       <td>${rating}</td>
+</tr>
+"""
 
                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;
-       }"""
+       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;
+}"""
 
                c.executemany ("INSERT INTO templates (template_name, template_content) VALUES (?, ?);",
                                                                        [["main_template", template_main],
index 32e39f9..6b7f31a 100644 (file)
@@ -20,7 +20,9 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        # refresh the category list
        def repopulate_categories (self):
                recs = biaweb_db.get_categories (self.current_db)
-               if not recs:
+
+               # check with False because None is returned when no records exist
+               if recs == False:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories")
                        return
 
@@ -32,7 +34,9 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        # refresh the articles list
        def repopulate_articles (self, catid=None):
                recs = biaweb_db.get_articles (self.current_db, catid)
-               if not recs:
+
+               # check with False because None is returned when no records exist
+               if recs == False:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles")
                        return
 
@@ -218,7 +222,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                                "Cannot edit templates. You need to create or open a website first")
                else:
-                       tdlg = tpld.TemplatesDialog (self)
+                       tdlg = tpld.TemplatesDialog (self, self.current_db)
                        tdlg.exec_ ()
 
        # function to get the category or article ID from current selected item in a tree widget
index 7d8fbf0..8a57482 100644 (file)
@@ -3,10 +3,17 @@
 
 import PyQt4
 import ui_templates_dialog
-import highlighter
+import biaweb_db
 
 class TemplatesDialog (PyQt4.QtGui.QDialog, ui_templates_dialog.Ui_TemplatesDialog):
-       def __init__ (self, master):
+       def __init__ (self, master, currentdb):
                PyQt4.QtGui.QDialog.__init__ (self, master)
                self.setupUi (self)
-               self.setWindowFlags (PyQt4.QtCore.Qt.Window)
+
+               # set the current database
+               self.current_db = currentdb
+
+               # populate the templates
+               tpls = biaweb_db.get_templates (self.current_db)
+               for tpl_name, tpl_content in tpls:
+                       self.templates.addItem (str (tpl_name))
index 2b9be0a..e3f77aa 100644 (file)
@@ -6,32 +6,25 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>751</width>
-    <height>535</height>
+    <width>350</width>
+    <height>256</height>
    </rect>
   </property>
   <property name="windowTitle">
-   <string>Template Editor</string>
+   <string>Templates</string>
   </property>
   <layout class="QGridLayout" name="gridLayout">
-   <item row="0" column="0">
+   <item row="0" column="0" colspan="2">
     <widget class="QLabel" name="label">
      <property name="text">
-      <string>Template</string>
+      <string>Choose template</string>
      </property>
     </widget>
    </item>
-   <item row="0" column="1">
-    <widget class="QComboBox" name="templates">
-     <property name="minimumSize">
-      <size>
-       <width>250</width>
-       <height>0</height>
-      </size>
-     </property>
-    </widget>
+   <item row="1" column="0" colspan="3">
+    <widget class="QListWidget" name="templates"/>
    </item>
-   <item row="0" column="2">
+   <item row="2" column="1">
     <widget class="QPushButton" name="edit">
      <property name="toolTip">
       <string>Click to edit the
@@ -40,32 +33,15 @@ selected template</string>
      <property name="text">
       <string>&amp;Edit</string>
      </property>
-    </widget>
-   </item>
-   <item row="0" column="3">
-    <widget class="QPushButton" name="pushButton">
-     <property name="toolTip">
-      <string>Save the template
-modified below</string>
-     </property>
-     <property name="text">
-      <string>&amp;Save</string>
-     </property>
-    </widget>
-   </item>
-   <item row="1" column="0" colspan="4">
-    <widget class="QPlainTextEdit" name="template_text">
-     <property name="font">
-      <font>
-       <family>Monospace</family>
-      </font>
+     <property name="checkable">
+      <bool>false</bool>
      </property>
-     <property name="tabChangesFocus">
-      <bool>true</bool>
+     <property name="checked">
+      <bool>false</bool>
      </property>
     </widget>
    </item>
-   <item row="2" column="3">
+   <item row="2" column="2">
     <widget class="QPushButton" name="close">
      <property name="text">
       <string>&amp;Close</string>
@@ -75,10 +51,7 @@ modified below</string>
   </layout>
  </widget>
  <tabstops>
-  <tabstop>templates</tabstop>
   <tabstop>edit</tabstop>
-  <tabstop>pushButton</tabstop>
-  <tabstop>template_text</tabstop>
   <tabstop>close</tabstop>
  </tabstops>
  <resources/>
@@ -90,8 +63,8 @@ modified below</string>
    <slot>accept()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>515</x>
-     <y>383</y>
+     <x>741</x>
+     <y>525</y>
     </hint>
     <hint type="destinationlabel">
      <x>288</x>
@@ -100,4 +73,8 @@ modified below</string>
    </hints>
   </connection>
  </connections>
+ <slots>
+  <slot>onEdit()</slot>
+  <slot>onTemplateTextChanged()</slot>
+ </slots>
 </ui>
index 6845dc2..895e2be 100644 (file)
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'templates_dialog.ui'
 #
-# Created: Mon Nov 29 13:37:20 2010
+# Created: Mon Nov 29 15:30:13 2010
 #      by: PyQt4 UI code generator 4.7.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -12,49 +12,34 @@ from PyQt4 import QtCore, QtGui
 class Ui_TemplatesDialog(object):
     def setupUi(self, TemplatesDialog):
         TemplatesDialog.setObjectName("TemplatesDialog")
-        TemplatesDialog.resize(751, 535)
+        TemplatesDialog.resize(350, 256)
         self.gridLayout = QtGui.QGridLayout(TemplatesDialog)
         self.gridLayout.setObjectName("gridLayout")
         self.label = QtGui.QLabel(TemplatesDialog)
         self.label.setObjectName("label")
-        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
-        self.templates = QtGui.QComboBox(TemplatesDialog)
-        self.templates.setMinimumSize(QtCore.QSize(250, 0))
+        self.gridLayout.addWidget(self.label, 0, 0, 1, 2)
+        self.templates = QtGui.QListWidget(TemplatesDialog)
         self.templates.setObjectName("templates")
-        self.gridLayout.addWidget(self.templates, 0, 1, 1, 1)
+        self.gridLayout.addWidget(self.templates, 1, 0, 1, 3)
         self.edit = QtGui.QPushButton(TemplatesDialog)
+        self.edit.setCheckable(False)
+        self.edit.setChecked(False)
         self.edit.setObjectName("edit")
-        self.gridLayout.addWidget(self.edit, 0, 2, 1, 1)
-        self.pushButton = QtGui.QPushButton(TemplatesDialog)
-        self.pushButton.setObjectName("pushButton")
-        self.gridLayout.addWidget(self.pushButton, 0, 3, 1, 1)
-        self.template_text = QtGui.QPlainTextEdit(TemplatesDialog)
-        font = QtGui.QFont()
-        font.setFamily("Monospace")
-        self.template_text.setFont(font)
-        self.template_text.setTabChangesFocus(True)
-        self.template_text.setObjectName("template_text")
-        self.gridLayout.addWidget(self.template_text, 1, 0, 1, 4)
+        self.gridLayout.addWidget(self.edit, 2, 1, 1, 1)
         self.close = QtGui.QPushButton(TemplatesDialog)
         self.close.setObjectName("close")
-        self.gridLayout.addWidget(self.close, 2, 3, 1, 1)
+        self.gridLayout.addWidget(self.close, 2, 2, 1, 1)
 
         self.retranslateUi(TemplatesDialog)
         QtCore.QObject.connect(self.close, QtCore.SIGNAL("clicked()"), TemplatesDialog.accept)
         QtCore.QMetaObject.connectSlotsByName(TemplatesDialog)
-        TemplatesDialog.setTabOrder(self.templates, self.edit)
-        TemplatesDialog.setTabOrder(self.edit, self.pushButton)
-        TemplatesDialog.setTabOrder(self.pushButton, self.template_text)
-        TemplatesDialog.setTabOrder(self.template_text, self.close)
+        TemplatesDialog.setTabOrder(self.edit, self.close)
 
     def retranslateUi(self, TemplatesDialog):
-        TemplatesDialog.setWindowTitle(QtGui.QApplication.translate("TemplatesDialog", "Template Editor", None, QtGui.QApplication.UnicodeUTF8))
-        self.label.setText(QtGui.QApplication.translate("TemplatesDialog", "Template", None, QtGui.QApplication.UnicodeUTF8))
+        TemplatesDialog.setWindowTitle(QtGui.QApplication.translate("TemplatesDialog", "Templates", None, QtGui.QApplication.UnicodeUTF8))
+        self.label.setText(QtGui.QApplication.translate("TemplatesDialog", "Choose template", None, QtGui.QApplication.UnicodeUTF8))
         self.edit.setToolTip(QtGui.QApplication.translate("TemplatesDialog", "Click to edit the\n"
 "selected template", None, QtGui.QApplication.UnicodeUTF8))
         self.edit.setText(QtGui.QApplication.translate("TemplatesDialog", "&Edit", None, QtGui.QApplication.UnicodeUTF8))
-        self.pushButton.setToolTip(QtGui.QApplication.translate("TemplatesDialog", "Save the template\n"
-"modified below", None, QtGui.QApplication.UnicodeUTF8))
-        self.pushButton.setText(QtGui.QApplication.translate("TemplatesDialog", "&Save", None, QtGui.QApplication.UnicodeUTF8))
         self.close.setText(QtGui.QApplication.translate("TemplatesDialog", "&Close", None, QtGui.QApplication.UnicodeUTF8))