Article creating and deleting implemented
authorHarishankar <v.harishankar@gmail.com>
Mon, 29 Nov 2010 03:43:03 +0000 (09:13 +0530)
committerHarishankar <v.harishankar@gmail.com>
Mon, 29 Nov 2010 03:43:03 +0000 (09:13 +0530)
Implemented article creating and deleting functionality.
What remains to be done is the article editing.

article_dialog.py
biaweb_db.py
main_window.py
main_window.ui
ui_main_window.py

index 3923a26..f8108cf 100644 (file)
@@ -111,7 +111,7 @@ class ArticleDialog (PyQt4.QtGui.QDialog, ui_article_dialog.Ui_ArticleDialog):
 
        # get a list of items in numbered or bulleted list
        def insert_list_items (self, numbered = False):
-               if numbered == True:
+               if numbered:
                        title = "Numbered list"
                        otag = "<ol>\n"
                        ctag = "</ol>\n"
index 9caad08..1946ad7 100644 (file)
@@ -4,6 +4,36 @@
 import sqlite3
 import os
 import os.path
+import time
+
+# function to create an article
+def create_article (dbname, title, summary, keywords, content, catid, stub, rating):
+       # time of creation
+       creattime = time.time ()
+       modtime = creattime
+       try:
+               conn = sqlite3.connect (dbname)
+               c = conn.cursor ()
+               c.execute ("INSERT INTO articles (title, summary, keywords, content, cdate, mdate, cid, stub, rating) \
+                                       VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", (title, summary, keywords, content, creattime,
+                                       modtime, catid, stub, rating))
+               conn.commit ()
+               conn.close ()
+               return True
+       except sqlite3.Error:
+               return False
+
+# function to delete an article
+def delete_article (dbname, artid):
+       try:
+               conn = sqlite3.connect (dbname)
+               c = conn.cursor ()
+               c.execute ("DELETE FROM articles WHERE aid=?;", (artid,))
+               conn.commit ()
+               conn.close ()
+               return True
+       except sqlite3.Error:
+               return False
 
 # function to get a category from the database
 def get_category (dbname, catid):
@@ -44,6 +74,20 @@ def remove_category (dbname, catid):
        except sqlite3.Error:
                return False
 
+
+# function to create a category
+def create_category (dbname, category_name, category_desc, category_stub):
+       try:
+               conn = sqlite3.connect (dbname)
+               c = conn.cursor ()
+               c.execute ("INSERT INTO categories (name, desc, stub) VALUES (?, ?, ?);",
+                                                       (category_name, category_desc, category_stub))
+               conn.commit ()
+               conn.close ()
+               return True
+       except sqlite3.Error:
+               return False
+
 # function to set the configuration and update the database
 def set_configuration (dbname, site_title, site_url, keywords, description, copyright,
                                                num_rss, dest_path):
@@ -92,20 +136,6 @@ def get_configuration (dbname):
        except sqlite3.Error:
                return False
 
-
-# function to create a category
-def create_category (dbname, category_name, category_desc, category_stub):
-       try:
-               conn = sqlite3.connect (dbname)
-               c = conn.cursor ()
-               c.execute ("INSERT INTO categories (name, desc, stub) VALUES (?, ?, ?);",
-                                                       (category_name, category_desc, category_stub))
-               conn.commit ()
-               conn.close ()
-               return True
-       except sqlite3.Error:
-               return False
-
 # Function to get list of articles (either full list or just in a category
 def get_articles (dbname, category_id=None):
        try:
index ff4f3cf..3e70b10 100644 (file)
@@ -18,7 +18,7 @@ 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 recs == False:
+               if not recs:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories")
                        return
 
@@ -30,7 +30,7 @@ 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 recs == False:
+               if not recs:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles")
                        return
 
@@ -47,7 +47,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
 
        # when add article is triggered
        def onArticleAdd (self):
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                        "Cannot create article. You need to create or open a website first")
                else:
@@ -60,7 +60,28 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                        cats = biaweb_db.get_categories (self.current_db)
 
                        artdlg.populate_categories (cats, catid)
-                       artdlg.exec_ ()
+
+                       # if OK is pressed
+                       if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
+                               title = str (artdlg.article_title.text ()).strip ()
+                               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 ()
+                               # 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,
+                                                                                               stub, rating)
+                               if ret:
+                                       PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully created")
+                                       self.repopulate_categories ()
+                                       self.repopulate_articles (catid)
+                               else:
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating article")
 
        # when edit article is triggered
        def onArticleEdit (self):
@@ -68,12 +89,36 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
 
        # when delete article is triggered
        def onArticleDelete (self):
-               pass
+               if self.current_db is None:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                                       "Cannot delete 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)
+                       if artid is None:
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
+                               return
+                       # get confirmation on delete
+                       conf = 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:
+                               ret = biaweb_db.delete_article (self.current_db, artid)
+                               if not ret:
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting article")
+                               else:
+                                       self.repopulate_articles (catid)
 
        # when category item is activated
        def onCategoryItemActivated (self):
                self.onCategoryEdit ()
 
+       # when article item is activated
+       def onArticleItemActivated (self):
+               self.onArticleEdit ()
+
        # when item selection is changed in categories tree widget
        def onCategorySelectionChanged (self):
                if self.current_db is not None:
@@ -82,14 +127,14 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
 
        # when configuration menu is activated
        def onConfiguration (self):
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                        "Cannot edit configuration. You need to create or open a website first")
                else:
                        dlg = scd.SiteConfigDialog (self)
                        configs = biaweb_db.get_configuration (self.current_db)
 
-                       if configs == False:
+                       if not configs:
                                PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration")
                                return
 
@@ -114,7 +159,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                # database update
                                flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
                                                                                        copyright, num_rss, destination)
-                               if flag == False:
+                               if not flag:
                                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                                                        "SQLite 3 error in updating configuration")
 
@@ -131,7 +176,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        # category edit action
        def onCategoryEdit (self):
                # if no database is open
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                        "Cannot edit category. You need to create or open a website first")
                # database is open
@@ -144,7 +189,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                        else:
                                cat = biaweb_db.get_category (self.current_db, catid)
                                # if the category cannot be retrieved from database
-                               if cat == False:
+                               if not cat:
                                        PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error reading category")
                                        return
 
@@ -162,8 +207,9 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
 
                                        ret = biaweb_db.update_category (self.current_db,
                                                                                                catid, category_name, category_desc, category_stub)
-                                       if ret == True:
+                                       if ret:
                                                PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated")
+                                               self.repopulate_categories ()
                                        else:
                                                PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category")
 
@@ -171,9 +217,9 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        # category delete action
        def onCategoryDelete (self):
                # if there is no database
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
-                                                               "Cannot edit category. You need to create or open a website first")
+                                                               "Cannot delete category. You need to create or open a website first")
                else:
                        # get the selected category
                        catid = self.get_selected_item_id (self.categories)
@@ -188,7 +234,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                # if confirmed
                                if flag == PyQt4.QtGui.QMessageBox.Yes:
                                        ret = biaweb_db.remove_category (self.current_db, catid)
-                                       if ret == False:
+                                       if not ret:
                                                PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting category")
                                        else:
                                                self.repopulate_categories ()
@@ -197,7 +243,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        # category add action
        def onCategoryAdd (self):
                # if there is no database
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                        "Cannot add category. You need to create or open a website first")
                else:
@@ -209,7 +255,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                cat_desc = str (dlg.category_desc.text ()).strip ()
                                cat_stub = str (dlg.category_stub.text ()).strip ()
                                ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub)
-                               if ret == True:
+                               if ret:
                                        PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
                                        self.repopulate_categories ()
                                        self.repopulate_articles ()
@@ -246,7 +292,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                self.setWindowTitle ("BiaWeb - " + self.current_db)
                                flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
                                                                copyright, num_rss, destination)
-                               if flag == True:
+                               if flag:
                                        PyQt4.QtGui.QMessageBox.information (self, "Success",
                                                                                                                        "New site db successfully created")
                                        self.articles.clear ()
index 5849977..1709029 100644 (file)
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>articles</sender>
+   <signal>itemActivated(QTreeWidgetItem*,int)</signal>
+   <receiver>MainWindow</receiver>
+   <slot>onArticleItemActivated()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>338</x>
+     <y>342</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>280</x>
+     <y>347</y>
+    </hint>
+   </hints>
+  </connection>
  </connections>
  <slots>
   <slot>onFileNew()</slot>
   <slot>onArticleEdit()</slot>
   <slot>onArticleDelete()</slot>
   <slot>onCategoryItemActivated()</slot>
+  <slot>onArticleItemActivated()</slot>
  </slots>
 </ui>
index 9298f1f..9bdfe98 100644 (file)
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'main_window.ui'
 #
-# Created: Sun Nov 28 20:24:17 2010
+# Created: Mon Nov 29 09:09:27 2010
 #      by: PyQt4 UI code generator 4.7.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -136,6 +136,7 @@ class Ui_MainWindow(object):
         QtCore.QObject.connect(self.action_EditArticle, QtCore.SIGNAL("triggered()"), MainWindow.onArticleEdit)
         QtCore.QObject.connect(self.action_DeleteArticle, QtCore.SIGNAL("triggered()"), MainWindow.onArticleDelete)
         QtCore.QObject.connect(self.categories, QtCore.SIGNAL("itemActivated(QTreeWidgetItem*,int)"), MainWindow.onCategoryItemActivated)
+        QtCore.QObject.connect(self.articles, QtCore.SIGNAL("itemActivated(QTreeWidgetItem*,int)"), MainWindow.onArticleItemActivated)
         QtCore.QMetaObject.connectSlotsByName(MainWindow)
         MainWindow.setTabOrder(self.categories, self.articles)