Category creation completed
authorHarishankar <v.harishankar@gmail.com>
Fri, 26 Nov 2010 14:45:37 +0000 (20:15 +0530)
committerHarishankar <v.harishankar@gmail.com>
Fri, 26 Nov 2010 14:45:37 +0000 (20:15 +0530)
Category creation dialog and functionality
successfully implemented

biaweb_db.py
category_dialog.py [new file with mode: 0644]
category_dialog.ui
main_window.py
main_window.ui
site_configuration_dialog.py
ui_category_dialog.py
ui_main_window.py

index 9be184d..5496453 100644 (file)
@@ -2,9 +2,32 @@
 # Database handling functions
 
 import sqlite3
+import os
+import os.path
 
+# 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 create a new site database
 def create_db (dbname, site_title, site_url, keywords, description, copyright,
                                        num_rss, dest_path):
+       try:
+               if os.path.exists (dbname):
+                       os.remove (dbname)
+       except OSError:
+               return False
+
        try:
                conn = sqlite3.connect (dbname)
                c = conn.cursor ()
diff --git a/category_dialog.py b/category_dialog.py
new file mode 100644 (file)
index 0000000..af7f773
--- /dev/null
@@ -0,0 +1,18 @@
+# BiaWeb Website content manager (c) 2010 V.Harishankar
+# Category dialog class
+
+import PyQt4
+import ui_category_dialog
+
+class CategoryDialog (PyQt4.QtGui.QDialog, ui_category_dialog.Ui_CategoryDialog):
+       def __init__ (self, parent):
+               PyQt4.QtGui.QDialog.__init__ (self, parent)
+               self.setupUi (self)
+
+       def accept (self):
+               category_name = str (self.category_name.text ()).strip ()
+               category_stub = str (self.category_stub.text ()).strip ()
+               if category_name <> "" and category_stub <> "":
+                       PyQt4.QtGui.QDialog.accept (self)
+               else:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Missing fields", "Some required fields are missing")
index 39d2e43..89bcbed 100644 (file)
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QLabel" name="label">
+     <property name="font">
+      <font>
+       <weight>75</weight>
+       <bold>true</bold>
+      </font>
+     </property>
      <property name="text">
       <string>Category name</string>
      </property>
    </item>
    <item row="1" column="0">
     <widget class="QLabel" name="label_2">
+     <property name="font">
+      <font>
+       <weight>50</weight>
+       <bold>false</bold>
+      </font>
+     </property>
      <property name="text">
       <string>Category description</string>
      </property>
    </item>
    <item row="2" column="0">
     <widget class="QLabel" name="label_3">
+     <property name="font">
+      <font>
+       <weight>75</weight>
+       <bold>true</bold>
+      </font>
+     </property>
      <property name="text">
       <string>Stub (directory)</string>
      </property>
index 3632075..5902fbc 100644 (file)
@@ -4,6 +4,7 @@
 import PyQt4
 import ui_main_window
 import site_configuration_dialog as scd
+import category_dialog as catd
 import biaweb_db
 import sys
 
@@ -13,8 +14,31 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                self.setupUi (self)
                self.current_db = None
 
+       # category add action
+       def onCategoryAdd (self):
+               # if there is no database
+               if self.current_db == None:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                                       "Cannot add category. You need to create or open a website first.")
+               else:
+                       # show the category add dialog
+                       dlg = catd.CategoryDialog (self)
+                       # if OK button is pressed
+                       if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
+                               cat_name = str (dlg.category_name.text ()).strip ()
+                               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:
+                                       PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
+                               else:
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
+
+       # file new menu is clicked
        def onFileNew (self):
+               # show the site configuration dialog to get the new site details
                dlg = scd.SiteConfigDialog (self)
+               # if OK button is pressed
                if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
                        site_title = str (dlg.site_title.text ()).strip ()
                        site_url = str (dlg.site_url.text ()).strip ()
@@ -27,15 +51,16 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                        savefilename = PyQt4.QtGui.QFileDialog.getSaveFileName (self, "Save site database to")
 
                        if savefilename:
-                               self.current_db = savefilename
+                               self.current_db = str (savefilename)
                                self.setWindowTitle ("BiaWeb - " + self.current_db)
-                               flag = biaweb_db.create_db (str (savefilename), site_title, site_url, keywords, description,
+                               flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
                                                                copyright, num_rss, destination)
                                if flag == True:
                                        PyQt4.QtGui.QMessageBox.information (self, "Success",
                                                                                                                        "New site db successfully created")
                                else:
-                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating database.")
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
 
+       # file quit is clicked
        def onFileQuit (self):
                sys.exit (0)
index 6d83adb..61423a3 100644 (file)
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>action_AddCategory</sender>
+   <signal>triggered()</signal>
+   <receiver>MainWindow</receiver>
+   <slot>onCategoryAdd()</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>onFileQuit()</slot>
+  <slot>onCategoryAdd()</slot>
  </slots>
 </ui>
index 3b74ded..651889c 100644 (file)
@@ -17,6 +17,7 @@ class SiteConfigDialog (PyQt4.QtGui.QDialog, uscd.Ui_SiteConfigDialog):
                        self.destination.setText (destpath)
 
        def accept (self):
+               # check the required fields before accepting
                site_title = str (self.site_title.text ()).strip ()
                site_url = str (self.site_url.text ()).strip ()
                keywords = str (self.keywords.text ()).strip ()
index a27f428..6107c47 100644 (file)
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'category_dialog.ui'
 #
-# Created: Wed Nov 24 14:12:44 2010
+# Created: Fri Nov 26 17:27:34 2010
 #      by: PyQt4 UI code generator 4.7.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -16,18 +16,30 @@ class Ui_CategoryDialog(object):
         self.gridLayout = QtGui.QGridLayout(CategoryDialog)
         self.gridLayout.setObjectName("gridLayout")
         self.label = QtGui.QLabel(CategoryDialog)
+        font = QtGui.QFont()
+        font.setWeight(75)
+        font.setBold(True)
+        self.label.setFont(font)
         self.label.setObjectName("label")
         self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
         self.category_name = QtGui.QLineEdit(CategoryDialog)
         self.category_name.setObjectName("category_name")
         self.gridLayout.addWidget(self.category_name, 0, 1, 1, 1)
         self.label_2 = QtGui.QLabel(CategoryDialog)
+        font = QtGui.QFont()
+        font.setWeight(50)
+        font.setBold(False)
+        self.label_2.setFont(font)
         self.label_2.setObjectName("label_2")
         self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
         self.category_desc = QtGui.QLineEdit(CategoryDialog)
         self.category_desc.setObjectName("category_desc")
         self.gridLayout.addWidget(self.category_desc, 1, 1, 1, 1)
         self.label_3 = QtGui.QLabel(CategoryDialog)
+        font = QtGui.QFont()
+        font.setWeight(75)
+        font.setBold(True)
+        self.label_3.setFont(font)
         self.label_3.setObjectName("label_3")
         self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
         self.category_stub = QtGui.QLineEdit(CategoryDialog)
index 3b83164..5d613ca 100644 (file)
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'main_window.ui'
 #
-# Created: Fri Nov 26 13:58:47 2010
+# Created: Fri Nov 26 17:19:44 2010
 #      by: PyQt4 UI code generator 4.7.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -115,6 +115,7 @@ class Ui_MainWindow(object):
         self.retranslateUi(MainWindow)
         QtCore.QObject.connect(self.actionNew_site, QtCore.SIGNAL("triggered()"), MainWindow.onFileNew)
         QtCore.QObject.connect(self.action_Quit, QtCore.SIGNAL("triggered()"), MainWindow.onFileQuit)
+        QtCore.QObject.connect(self.action_AddCategory, QtCore.SIGNAL("triggered()"), MainWindow.onCategoryAdd)
         QtCore.QMetaObject.connectSlotsByName(MainWindow)
 
     def retranslateUi(self, MainWindow):