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
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:
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")
# 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):
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")
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