import ui_main_window
import site_configuration_dialog as scd
import category_dialog as catd
+import article_dialog as artd
import biaweb_db
import sys
self.setupUi (self)
self.current_db = None
+ # refresh the category list
+ def repopulate_categories (self):
+ recs = biaweb_db.get_categories (self.current_db)
+ if not recs:
+ PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories")
+ return
+
+ self.categories.clear ()
+ for (id, name, desc, stub) in recs:
+ qrow = PyQt4.QtGui.QTreeWidgetItem ([str(id), str(name)])
+ self.categories.addTopLevelItem (qrow)
+
+ # refresh the articles list
+ def repopulate_articles (self, catid=None):
+ recs = biaweb_db.get_articles (self.current_db, catid)
+ if not recs:
+ PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles")
+ return
+
+ self.articles.clear ()
+ for item in recs:
+ qrow = PyQt4.QtGui.QTreeWidgetItem ([str(item[0]), str(item[1])])
+ self.articles.addTopLevelItem (qrow)
+
+ # when the view is refreshed
+ def onViewRefresh (self):
+ if self.current_db is not None:
+ self.repopulate_categories ()
+ self.repopulate_articles ()
+
+ # when add article is triggered
+ def onArticleAdd (self):
+ 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:
+ catid = self.get_selected_item_id (self.categories)
+ # if no category is selected
+ if catid is None:
+ PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected for article")
+ return
+ artdlg = artd.ArticleDialog (self)
+ cats = biaweb_db.get_categories (self.current_db)
+
+ artdlg.populate_categories (cats, catid)
+
+ # 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):
+ pass
+
+ # when delete article is triggered
+ def onArticleDelete (self):
+ 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:
+ catid = self.get_selected_item_id (self.categories)
+ self.repopulate_articles (catid)
+
+ # when configuration menu is activated
+ def onConfiguration (self):
+ 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 not configs:
+ 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])
+ dlg.description.setPlainText (configs[3])
+ dlg.num_rss_items.setValue (configs[4])
+ dlg.destination.setText (configs[5])
+ 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 ()
+ destination = str (dlg.destination.text ()).strip ()
+ 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 not flag:
+ 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 is 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 not cat:
+ 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:
+ 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")
+
+
+ # category delete action
+ def onCategoryDelete (self):
+ # if there is no database
+ if self.current_db is None:
+ PyQt4.QtGui.QMessageBox.critical (self, "Error",
+ "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)
+ if catid is None:
+ PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
+ # category is selected
+ else:
+ # get confirmation first
+ flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
+ "This will delete the category and all associated articles. Are you sure you wish to continue?",
+ PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
+ # if confirmed
+ if flag == PyQt4.QtGui.QMessageBox.Yes:
+ ret = biaweb_db.remove_category (self.current_db, catid)
+ if not ret:
+ PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting category")
+ else:
+ self.repopulate_categories ()
+ self.repopulate_articles ()
+
# 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.")
+ "Cannot add category. You need to create or open a website first")
else:
# show the category add dialog
dlg = catd.CategoryDialog (self)
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 ()
else:
PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
+ # file open menu is clicked
+ def onFileOpen (self):
+ filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
+ if filename:
+ self.current_db = str (filename)
+ self.setWindowTitle ("BiaWeb - " + self.current_db)
+ self.repopulate_categories ()
+ self.repopulate_articles ()
+
# file new menu is clicked
def onFileNew (self):
# show the site configuration dialog to get the new site details
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 ()
+ self.categories.clear ()
else:
PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")