Category editing implemented
authorHarishankar <v.harishankar@gmail.com>
Sat, 27 Nov 2010 14:04:13 +0000 (19:34 +0530)
committerHarishankar <v.harishankar@gmail.com>
Sat, 27 Nov 2010 14:04:13 +0000 (19:34 +0530)
Implemented the category editing functionality

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

index 2c8e6cd..4487339 100644 (file)
@@ -5,6 +5,32 @@ import sqlite3
 import os
 import os.path
 
+# function to get a category from the database
+def get_category (dbname, catid):
+       try:
+               conn = sqlite3.connect (dbname)
+               c = conn.cursor ()
+               c.execute ("SELECT * FROM categories WHERE cid=?", (catid,))
+               conn.commit ()
+               cat = c.fetchone ()
+               conn.close ()
+               return cat
+       except sqlite3.Error:
+               return False
+
+# function to update a category
+def update_category (dbname, catid, cat_name, cat_desc, cat_stub):
+       try:
+               conn = sqlite3.connect (dbname)
+               c = conn.cursor ()
+               c.execute ("UPDATE categories SET name=?, desc=?, stub=? WHERE cid=?;",
+                                       (cat_name, cat_desc, cat_stub, catid))
+               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):
index f917e76..4d4afa3 100644 (file)
@@ -47,6 +47,11 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                        dlg = scd.SiteConfigDialog (self)
                        configs = biaweb_db.get_configuration (self.current_db)
 
+                       if configs == False:
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration")
+                               return
+
+                       # set the data
                        dlg.site_url.setText (configs[0])
                        dlg.site_title.setText (configs[1])
                        dlg.keywords.setText (configs[2])
@@ -56,6 +61,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                        dlg.copyright.setText (configs[6])
 
                        if (dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted):
+                               # if accepted, get the data and store in database
                                site_title = str (dlg.site_title.text ()).strip ()
                                site_url = str (dlg.site_url.text ()).strip ()
                                keywords = str (dlg.keywords.text ()).strip ()
@@ -63,12 +69,62 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                description = str (dlg.description.toPlainText ()).strip ()
                                num_rss = dlg.num_rss_items.value ()
                                copyright = str (dlg.copyright.text ()).strip ()
+                               # database update
                                flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
                                                                                        copyright, num_rss, destination)
                                if flag == False:
                                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                                                        "SQLite 3 error in updating configuration")
 
+       # function to get the category or article ID from current selected item in a tree widget
+       def get_selected_item_id (self, twidget):
+               selitems = twidget.selectedItems ()
+               if selitems:
+                       # get the first column data as integer
+                       selindex = selitems[0].data(0, 0).toInt ()[0]
+                       return selindex
+               else:
+                       return None
+
+       # category edit action
+       def onCategoryEdit (self):
+               # if no database is open
+               if self.current_db == None:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                                       "Cannot edit category. You need to create or open a website first")
+               # database is open
+               else:
+                       catid = self.get_selected_item_id (self.categories)
+                       # if no category is selected, display an error
+                       if catid is None:
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
+                       # category is selected
+                       else:
+                               cat = biaweb_db.get_category (self.current_db, catid)
+                               # if the category cannot be retrieved from database
+                               if cat == False:
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error reading category")
+                                       return
+
+                               # set the data in the dialog
+                               dlg = catd.CategoryDialog (self)
+                               dlg.category_name.setText (cat[1])
+                               dlg.category_desc.setText (cat[2])
+                               dlg.category_stub.setText (cat[3])
+
+                               # if accepted
+                               if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
+                                       category_name = str (dlg.category_name.text ()).strip ()
+                                       category_desc = str (dlg.category_desc.text ()).strip ()
+                                       category_stub = str (dlg.category_stub.text ()).strip ()
+
+                                       ret = biaweb_db.update_category (self.current_db,
+                                                                                               catid, category_name, category_desc, category_stub)
+                                       if ret == True:
+                                               PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated")
+                                       else:
+                                               PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category")
+
 
        # category add action
        def onCategoryAdd (self):
index 4e20f6d..74c1602 100644 (file)
@@ -63,7 +63,7 @@
         <bool>true</bool>
        </property>
        <property name="allColumnsShowFocus">
-        <bool>true</bool>
+        <bool>false</bool>
        </property>
        <column>
         <property name="text">
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>action_EditCategory</sender>
+   <signal>triggered()</signal>
+   <receiver>MainWindow</receiver>
+   <slot>onCategoryEdit()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>-1</x>
+     <y>-1</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>316</x>
+     <y>228</y>
+    </hint>
+   </hints>
+  </connection>
  </connections>
  <slots>
   <slot>onFileNew()</slot>
   <slot>onCategoryAdd()</slot>
   <slot>onFileOpen()</slot>
   <slot>onConfiguration()</slot>
+  <slot>onCategoryEdit()</slot>
  </slots>
 </ui>
index 46ecb10..8080f31 100644 (file)
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'main_window.ui'
 #
-# Created: Sat Nov 27 16:21:08 2010
+# Created: Sat Nov 27 19:17:18 2010
 #      by: PyQt4 UI code generator 4.7.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -38,7 +38,7 @@ class Ui_MainWindow(object):
         self.gridLayout_2.setObjectName("gridLayout_2")
         self.categories = QtGui.QTreeWidget(self.dockWidgetContents_2)
         self.categories.setAlternatingRowColors(True)
-        self.categories.setAllColumnsShowFocus(True)
+        self.categories.setAllColumnsShowFocus(False)
         self.categories.setObjectName("categories")
         self.gridLayout_2.addWidget(self.categories, 0, 0, 1, 1)
         self.dockWidget_2.setWidget(self.dockWidgetContents_2)
@@ -121,6 +121,7 @@ class Ui_MainWindow(object):
         QtCore.QObject.connect(self.action_AddCategory, QtCore.SIGNAL("triggered()"), MainWindow.onCategoryAdd)
         QtCore.QObject.connect(self.action_Open_site, QtCore.SIGNAL("triggered()"), MainWindow.onFileOpen)
         QtCore.QObject.connect(self.action_Configuration, QtCore.SIGNAL("triggered()"), MainWindow.onConfiguration)
+        QtCore.QObject.connect(self.action_EditCategory, QtCore.SIGNAL("triggered()"), MainWindow.onCategoryEdit)
         QtCore.QMetaObject.connectSlotsByName(MainWindow)
 
     def retranslateUi(self, MainWindow):