Article editing fully implemented
authorHarishankar <v.harishankar@gmail.com>
Mon, 29 Nov 2010 06:08:03 +0000 (11:38 +0530)
committerHarishankar <v.harishankar@gmail.com>
Mon, 29 Nov 2010 06:08:03 +0000 (11:38 +0530)
Editing an article is now fully implemented.

article_dialog.py
article_dialog.ui
biaweb_db.py
main_window.py
ui_article_dialog.py

index f8108cf..740692c 100644 (file)
@@ -113,11 +113,11 @@ class ArticleDialog (PyQt4.QtGui.QDialog, ui_article_dialog.Ui_ArticleDialog):
        def insert_list_items (self, numbered = False):
                if numbered:
                        title = "Numbered list"
-                       otag = "<ol>\n"
+                       otag = "\n<ol>\n"
                        ctag = "</ol>\n"
                else:
                        title = "Bulleted list"
-                       otag = "<ul>\n"
+                       otag = "\n<ul>\n"
                        ctag = "</ul>\n"
 
                itemlist = [ otag ]
@@ -228,6 +228,6 @@ class ArticleDialog (PyQt4.QtGui.QDialog, ui_article_dialog.Ui_ArticleDialog):
        # when break is clicked
        def onBreak (self):
                textcur = self.content.textCursor ()
-               breaktxt = "\n<br />\n"
+               breaktxt = "<br />\n"
                textcur.clearSelection ()
                textcur.insertText (breaktxt)
index 62ee75f..1e8557c 100644 (file)
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>654</width>
-    <height>544</height>
+    <height>548</height>
    </rect>
   </property>
   <property name="windowTitle">
index 1946ad7..dd3e333 100644 (file)
@@ -6,6 +6,19 @@ import os
 import os.path
 import time
 
+# function to get an article from the database
+def get_article (dbname, artid):
+       try:
+               conn = sqlite3.connect (dbname)
+               c = conn.cursor ()
+               c.execute ("SELECT * FROM articles WHERE aid=?;", (artid,))
+               conn.commit ()
+               row = c.fetchone ()
+               conn.close ()
+               return row
+       except sqlite3.Error:
+               return False
+
 # function to create an article
 def create_article (dbname, title, summary, keywords, content, catid, stub, rating):
        # time of creation
@@ -23,6 +36,22 @@ def create_article (dbname, title, summary, keywords, content, catid, stub, rati
        except sqlite3.Error:
                return False
 
+# function to update an article
+def update_article (dbname, aid, title, summary, keywords, content, catid, stub, rating):
+       # modification time only to be updated
+       modtime = time.time ()
+       try:
+               conn = sqlite3.connect (dbname)
+               c = conn.cursor ()
+               c.execute ("UPDATE articles SET title=?, summary=?, keywords=?, content=?, mdate=?, cid=?, \
+                                       stub=?, rating=? WHERE aid=?;",
+                                       (title, summary, keywords, content, modtime, catid, stub, rating, aid))
+               conn.commit ()
+               conn.close ()
+               return True
+       except sqlite3.Error:
+               return False
+
 # function to delete an article
 def delete_article (dbname, artid):
        try:
index 3e70b10..6f033d6 100644 (file)
@@ -67,14 +67,14 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                keywords = str (artdlg.keywords.text ()).strip ()
                                summary = str (artdlg.summary.toPlainText ()).strip ()
                                content = str (artdlg.content.toPlainText ()).strip ()
-                               catid, ok = artdlg.category.itemData (artdlg.category.currentIndex ()).toInt ()
+                               cid, ok = artdlg.category.itemData (artdlg.category.currentIndex ()).toInt ()
                                # if catid is not an integer then abort
                                if not ok:
                                        return
                                rating = artdlg.rating.value ()
                                stub = str (artdlg.stub.text ()).strip ()
 
-                               ret = biaweb_db.create_article (self.current_db, title, summary, keywords, content, catid,
+                               ret = biaweb_db.create_article (self.current_db, title, summary, keywords, content, cid,
                                                                                                stub, rating)
                                if ret:
                                        PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully created")
@@ -85,7 +85,54 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
 
        # when edit article is triggered
        def onArticleEdit (self):
-               pass
+               if self.current_db is None:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                                       "Cannot edit article. You need to create or open a website first")
+               else:
+                       # get the selected article
+                       artid = self.get_selected_item_id (self.articles)
+                       catid = self.get_selected_item_id (self.categories)
+
+                       # no article is selected
+                       if artid is None:
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
+                               return
+                       article = biaweb_db.get_article (self.current_db, artid)
+                       # if article cannot be read
+                       if not article:
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading article")
+                               return
+                       artdlg = artd.ArticleDialog (self)
+
+                       artdlg.article_title.setText (article[1])
+                       artdlg.summary.setPlainText (article[2])
+                       artdlg.keywords.setText (article[3])
+                       artdlg.content.setPlainText (article[4])
+                       # populate category combo box and set active category to the article's category
+                       cats = biaweb_db.get_categories (self.current_db)
+                       artdlg.populate_categories (cats, article[7])
+                       artdlg.stub.setText (article[8])
+                       artdlg.rating.setValue (article[9])
+
+                       # ok is pressed
+                       if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
+                               title = str (artdlg.article_title.text ()).strip ()
+                               summary = str (artdlg.summary.toPlainText ()).strip ()
+                               keywords = str (artdlg.keywords.text ()).strip ()
+                               content = str (artdlg.content.toPlainText ()).strip ()
+                               cid, ok = artdlg.category.itemData (artdlg.category.currentIndex()).toInt ()
+                               # if cat id is not an integer
+                               if not ok:
+                                       return
+                               rating = artdlg.rating.value ()
+                               stub = str (artdlg.stub.text ()).strip ()
+                               ret = biaweb_db.update_article (self.current_db, artid, title,
+                                                                               summary, keywords, content, cid, stub, rating)
+                               if ret:
+                                       PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully updated")
+                                       self.repopulate_articles (catid)
+                               else:
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating article")
 
        # when delete article is triggered
        def onArticleDelete (self):
@@ -100,11 +147,11 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
                                return
                        # get confirmation on delete
-                       conf = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
+                       flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
                                                        "Are you sure you wish to delete the selected article?",
                                                        PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
                        # confirmed
-                       if conf == PyQt4.QtGui.QMessageBox.Yes:
+                       if flag == PyQt4.QtGui.QMessageBox.Yes:
                                ret = biaweb_db.delete_article (self.current_db, artid)
                                if not ret:
                                        PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting article")
@@ -190,7 +237,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                cat = biaweb_db.get_category (self.current_db, catid)
                                # if the category cannot be retrieved from database
                                if not cat:
-                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error reading category")
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading category")
                                        return
 
                                # set the data in the dialog
index 5f0876a..6267265 100644 (file)
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'article_dialog.ui'
 #
-# Created: Sun Nov 28 20:16:53 2010
+# Created: Mon Nov 29 11:37:34 2010
 #      by: PyQt4 UI code generator 4.7.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -12,7 +12,7 @@ from PyQt4 import QtCore, QtGui
 class Ui_ArticleDialog(object):
     def setupUi(self, ArticleDialog):
         ArticleDialog.setObjectName("ArticleDialog")
-        ArticleDialog.resize(654, 544)
+        ArticleDialog.resize(654, 548)
         ArticleDialog.setAutoFillBackground(False)
         ArticleDialog.setSizeGripEnabled(True)
         ArticleDialog.setModal(True)