Add Author Caching to stop excess database hits

Finish 1st cut of Author Maintenance (Add / Update / Delete)

bzr-revno: 265
This commit is contained in:
Tim Bentley 2009-01-03 14:19:43 +00:00
parent 22f308268c
commit 0e05dc79f8
5 changed files with 135 additions and 67 deletions

View File

@ -2,7 +2,7 @@
# Form implementation generated from reading ui file 'authorsdialog.ui'
#
# Created: Thu Jan 1 09:41:26 2009
# Created: Sat Jan 3 11:48:36 2009
# by: PyQt4 UI code generator 4.4.3
#
# WARNING! All changes made in this file will be lost!
@ -28,19 +28,19 @@ class Ui_AuthorsDialog(object):
self.gridLayout.addWidget(self.DisplayLabel, 0, 0, 1, 1)
self.DisplayEdit = QtGui.QLineEdit(self.AuthorDetails)
self.DisplayEdit.setObjectName("DisplayEdit")
self.gridLayout.addWidget(self.DisplayEdit, 0, 1, 1, 3)
self.gridLayout.addWidget(self.DisplayEdit, 0, 1, 1, 4)
self.FirstNameLabel = QtGui.QLabel(self.AuthorDetails)
self.FirstNameLabel.setObjectName("FirstNameLabel")
self.gridLayout.addWidget(self.FirstNameLabel, 1, 0, 1, 1)
self.FirstNameEdit = QtGui.QLineEdit(self.AuthorDetails)
self.FirstNameEdit.setObjectName("FirstNameEdit")
self.gridLayout.addWidget(self.FirstNameEdit, 1, 1, 1, 3)
self.gridLayout.addWidget(self.FirstNameEdit, 1, 1, 1, 4)
self.LastNameLabel = QtGui.QLabel(self.AuthorDetails)
self.LastNameLabel.setObjectName("LastNameLabel")
self.gridLayout.addWidget(self.LastNameLabel, 2, 0, 1, 1)
self.LastNameEdit = QtGui.QLineEdit(self.AuthorDetails)
self.LastNameEdit.setObjectName("LastNameEdit")
self.gridLayout.addWidget(self.LastNameEdit, 2, 1, 1, 3)
self.gridLayout.addWidget(self.LastNameEdit, 2, 1, 1, 4)
spacerItem = QtGui.QSpacerItem(198, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 3, 0, 1, 2)
self.DeleteButton = QtGui.QPushButton(self.AuthorDetails)
@ -48,22 +48,29 @@ class Ui_AuthorsDialog(object):
icon.addPixmap(QtGui.QPixmap(":/services/service_delete.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.DeleteButton.setIcon(icon)
self.DeleteButton.setObjectName("DeleteButton")
self.gridLayout.addWidget(self.DeleteButton, 3, 2, 1, 1)
self.gridLayout.addWidget(self.DeleteButton, 3, 3, 1, 1)
self.AddUpdateButton = QtGui.QPushButton(self.AuthorDetails)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(":/system/system_settings.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.AddUpdateButton.setIcon(icon1)
self.AddUpdateButton.setObjectName("AddUpdateButton")
self.gridLayout.addWidget(self.AddUpdateButton, 3, 3, 1, 1)
self.gridLayout.addWidget(self.AddUpdateButton, 3, 4, 1, 1)
self.ClearButton = QtGui.QPushButton(self.AuthorDetails)
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap(":/songs/song_edit.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.ClearButton.setIcon(icon2)
self.ClearButton.setObjectName("ClearButton")
self.gridLayout.addWidget(self.ClearButton, 3, 2, 1, 1)
self.MessageLabel = QtGui.QLabel(AuthorsDialog)
self.MessageLabel.setGeometry(QtCore.QRect(20, 500, 261, 17))
self.MessageLabel.setObjectName("MessageLabel")
self.AuthorListView = QtGui.QTableWidget(AuthorsDialog)
self.AuthorListView.setGeometry(QtCore.QRect(20, 20, 341, 301))
self.AuthorListView.setDragEnabled(True)
self.AuthorListView.setAlternatingRowColors(True)
self.AuthorListView.setColumnCount(2)
self.AuthorListView.setColumnCount(0)
self.AuthorListView.setObjectName("AuthorListView")
self.AuthorListView.setColumnCount(2)
self.AuthorListView.setColumnCount(0)
self.AuthorListView.setRowCount(0)
self.retranslateUi(AuthorsDialog)
@ -80,4 +87,4 @@ class Ui_AuthorsDialog(object):
self.LastNameLabel.setText(QtGui.QApplication.translate("AuthorsDialog", "Last Name:", None, QtGui.QApplication.UnicodeUTF8))
self.DeleteButton.setToolTip(QtGui.QApplication.translate("AuthorsDialog", "Delete Author", None, QtGui.QApplication.UnicodeUTF8))
self.AddUpdateButton.setToolTip(QtGui.QApplication.translate("AuthorsDialog", "Add Update Author", None, QtGui.QApplication.UnicodeUTF8))
self.ClearButton.setToolTip(QtGui.QApplication.translate("AuthorsDialog", "Clear Selection", None, QtGui.QApplication.UnicodeUTF8))

View File

@ -39,7 +39,8 @@ class AuthorsForm(QDialog, Ui_AuthorsDialog):
self.AuthorListView.setColumnWidth(1, 300)
self.AuthorListView.setHorizontalHeaderLabels(QtCore.QStringList([" ","Author"]))
self.authorid = 0 # this is the selected authorid for updates and deletes
self.candelete = False
self.currentrow = 0
self.songmanager = songmanager
def load_form(self):
@ -53,8 +54,14 @@ class AuthorsForm(QDialog, Ui_AuthorsDialog):
twi = QtGui.QTableWidgetItem(str(id))
self.AuthorListView.setItem(c , 0, twi)
twi = QtGui.QTableWidgetItem(str(txt))
#twi.setFlags(Not QtCore.Qt.ItemIsSelectable & QtCore.Qt.ItemIsEnabled)
twi.setFlags(QtCore.Qt.ItemIsSelectable)
self.AuthorListView.setItem(c , 1, twi)
self.AuthorListView.setRowHeight(c, 20)
c = self.AuthorListView.rowCount()
if self.currentrow > c: # incase we have delete the last row of the table
self.currentrow = c
self.AuthorListView.selectRow(self.currentrow)
@pyqtSignature("")
def on_buttonBox_accepted(self):
@ -70,46 +77,54 @@ class AuthorsForm(QDialog, Ui_AuthorsDialog):
"""
print "bb rej"
@pyqtSignature("")
def on_FirstNameEdit_lostFocus(self):
"""
Slot documentation goes here.
"""
print "fm lf"
@pyqtSignature("")
def on_LastNameEdit_lostFocus(self):
"""
Slot documentation goes here.
"""
print "ln lf"
@pyqtSignature("")
def on_DeleteButton_clicked(self):
"""
Slot documentation goes here.
"""
print "db clicked"
if self.candelete == True:
self.songmanager.delete_author(self.authorid)
self.on_ClearButton_clicked()
self.load_form()
@pyqtSignature("")
def on_AddUpdateButton_clicked(self):
"""
Slot documentation goes here.
"""
print "au cli"
self.songmanager.save_author(str(self.DisplayEdit.displayText()),str(self.FirstNameEdit.displayText()) ,str(self.LastNameEdit.displayText()) , self.authorid)
self.on_ClearButton_clicked()
self.load_form()
@pyqtSignature("")
def on_ClearButton_clicked(self):
"""
Slot documentation goes here.
"""
self.authorid = 0
self.DisplayEdit.setText("")
self.FirstNameEdit.setText("")
self.LastNameEdit.setText("")
self.candelete = True
@pyqtSignature("QTableWidgetItem*")
def on_AuthorListView_itemClicked(self, item):
"""
Slot documentation goes here.
"""
cr = self.AuthorListView.currentRow()
id = int(self.AuthorListView.item(cr, 0).text())
self.currentrow = self.AuthorListView.currentRow()
id = int(self.AuthorListView.item(self.currentrow, 0).text())
author = self.songmanager.get_author(id)
print author
self.authorid = author[0]
self.DisplayEdit.setText(author[1])
self.FirstNameEdit.setText(author[2])
self.LastNameEdit.setText(author[3])
songs = self.songmanager.get_song_authors_for_author(id)
if len(songs) > 0:
self.MessageLabel.setText("Author is attached to Songs")
self.candelete = False
else:
self.MessageLabel.setText("Author is not used")
self.candelete = True

View File

@ -31,14 +31,32 @@ from openlp.core.utils import ConfigHelper
import logging
class BibleDBException(Exception):
class SongDBException(Exception):
pass
class BibleInvalidDatabaseError(Exception):
class SongInvalidDatabaseError(Exception):
pass
metadata = MetaData()
#authors = Table('authors', metadata, autoload=True)
author_table = Table('authors', metadata,
Column('authorid', Integer, primary_key=True),
Column('authorname', String(40)),
Column('first_name', String(40)),
Column('last_name', String(40)),
)
class Author(object):
def __init__(self, authorname, first_name, last_name):
#self.authorid = authorid
self.authorname =authorname
self.first_name =first_name
self.last_name =last_name
def __repr__(self):
return "<authormeta(%s,%s,%s)>" , self.authorname, self.first_name, self.last_name
mapper(Author,author_table)
class SongDBImpl(BibleCommon):
global log
log=logging.getLogger("SongDBImpl")
@ -58,12 +76,12 @@ class SongDBImpl(BibleCommon):
self.db.convert_unicode=False
metadata.bind = self.db
metadata.bind.echo = False
self.Session = sessionmaker()
self.Session = sessionmaker(autoflush=True, autocommit=False)
self.Session.configure(bind=self.db)
self.authors = Table('authors', metadata, autoload=True)
self.settings = Table('settings', metadata, autoload=True)
self.songauthors = Table('songauthors', metadata, autoload=True)
self.songs = Table('songs', metadata, autoload=True)
#self.authors_table = Table('authors', metadata, autoload=True)
#self.settings = Table('settings', metadata, autoload=True)
#self.songauthors = Table('songauthors', metadata, autoload=True)
#self.songs = Table('songs', metadata, autoload=True)
def create_tables(self):
log.debug( "createTables")
@ -76,12 +94,12 @@ class SongDBImpl(BibleCommon):
self.save_meta("dbversion", "2")
self._loadTestaments()
def add_verse(self, bookid, chap, verse, text):
log.debug( "add_verse %s,%s,%s,%s", bookid, chap, verse, text)
metadata.bind.echo = False
def add_author(self, author_name, first_name, last_name):
log.debug( "add_author %s,%s,%s", author_name, first_name, last_name)
metadata.bind.echo = True
session = self.Session()
versemeta = Verse(book_id=int(bookid), chapter=int(chap), verse=int(verse), text=(text))
session.add(versemeta)
authorsmeta = Author(authorname=author_name, first_name=first_name,last_name=last_name)
session.add(authorsmeta)
session.commit()
def save_meta(self, key, value):
@ -117,13 +135,30 @@ class SongDBImpl(BibleCommon):
metadata.bind.echo = True
s = text (""" select * FROM authors where authorid = :i """)
return self.db.execute(s, i=authorid).fetchone()
def delete_author(self, authorid):
log.debug( "delete_author %s" , authorid)
metadata.bind.echo = True
s = text (""" delete FROM authors where authorid = :i """)
return self.db.execute(s, i=authorid)
def update_author(self, authorid, author_name, first_name, last_name):
log.debug( "update_author %s,%s,%s,%s" , authorid, author_name, first_name, last_name)
metadata.bind.echo = True
s = text (""" update authors set authorname= :an ,first_name = :fn,last_name = :ln where authorid = :i """)
return self.db.execute(s, an=author_name, fn=first_name, ln=last_name, i=authorid)
def get_song_authors(self, song):
log.debug( "get_song_authors ")
def get_song_authors_for_author(self, authorid):
log.debug( "get_song_authors for author %s ", authorid)
metadata.bind.echo = False
s = text (""" select * FROM songauthors where songid = ;c """)
return self.db.execute(s, c=songid).fetchall()
s = text (""" select * FROM songauthors where authorid = :c """)
return self.db.execute(s, c=authorid).fetchall()
def get_song_authors_for_song(self, songid):
log.debug( "get_song_authors for song %s ", songid)
metadata.bind.echo = False
s = text (""" select * FROM songauthors where songid = :c """)
return self.db.execute(s, c=songid).fetchall()
def get_max_bible_book_verses(self, bookname, chapter):
log.debug( "get_max_bible_book_verses %s,%s ", bookname , chapter)
@ -170,11 +205,10 @@ class SongDBImpl(BibleCommon):
def get_song_from_title(self,searchtext):
log.debug( "get_song_from_title %s",searchtext)
#metadata.bind.echo = False
#s = text (""" SELECT s.songid AS songid, s.songtitle AS songtitle, a.authorname AS authorname FROM songs s OUTER JOIN songauthors sa ON s.songid = sa.songid OUTER JOIN authors a ON sa.authorid = a.authorid WHERE s.songtitle LIKE :t ORDER BY s.songtitle ASC """)
s = ' SELECT s.songid AS songid, s.songtitle AS songtitle, a.authorname AS authorname FROM songs s OUTER JOIN songauthors sa ON s.songid = sa.songid OUTER JOIN authors a ON sa.authorid = a.authorid WHERE s.songtitle LIKE '+"'" +'%'+ searchtext + '%'+ "'"+' ORDER BY s.songtitle ASC'
print s
return self.db.execute(s).fetchall()
metadata.bind.echo = False
searchtext = "%"+searchtext+"%"
s = text (""" SELECT s.songid AS songid, s.songtitle AS songtitle, a.authorname AS authorname FROM songs s OUTER JOIN songauthors sa ON s.songid = sa.songid OUTER JOIN authors a ON sa.authorid = a.authorid WHERE s.songtitle LIKE :t ORDER BY s.songtitle ASC """)
return self.db.execute(s, t=searchtext).fetchall()
def get_song_from_author(self,searchtext):
log.debug( "get_song_from_author %s",searchtext)

View File

@ -40,8 +40,7 @@ class SongManager():
"""
self.config = config
log.debug( "Song Initialising")
self.bibleDBCache = None
#self.bibleHTTPCache = {} # dict of bible http readers
self.authorcache = None
self.songPath = self.config.get_data_path()
self.songSuffix = self.config.get_config("suffix name", u'olp3')
log.debug("Song Path %s and suffix %s", self.songPath, self.songSuffix )
@ -63,11 +62,13 @@ class SongManager():
"""
return self.songDBCache.get_song(songid)
def get_authors(self):
def get_authors(self, reload=False):
"""
Returns a list of all the authors
"""
return self.songDBCache.get_authors()
if self.authorcache == None or reload == True:
self.authorcache = self.songDBCache.get_authors()
return self.authorcache
def get_author(self, authorid):
"""
@ -75,24 +76,36 @@ class SongManager():
"""
return self.songDBCache.get_author(authorid)
def save_author(self, author_name, first_name, last_name):
def save_author(self, author_name, first_name, last_name, id = 0):
"""
Details of the Author
Save the Author and refresh the cache
"""
return self.songDBCache.get_author(authorid)
if id == 0:
self.songDBCache.add_author(author_name, first_name, last_name)
else:
self.songDBCache.update_author(id, author_name, first_name, last_name)
self.get_authors(True) # Updates occured refresh the cache
return True
def delete_author(self, authorid):
"""
Details of the Author
Delete the author and refresh the author cache
"""
return self.songDBCache.get_author(authorid)
def get_song_authors(self, songid):
self.songDBCache.delete_author(authorid)
self.get_authors(True) # Updates occured refresh the cache
return True
def get_song_authors_for_author(self, authorid):
"""
Returns the details of a song
"""
return self.songDBCache.get_song_authors(songid)
return self.songDBCache.get_song_authors_for_author(authorid)
def get_song_authors_for_song(self, songid):
"""
Returns the details of a song
"""
return self.songDBCache.get_song_authors_for_song(songid)
def get_bible_books(self,bible):
"""

View File

@ -195,7 +195,7 @@ class SongsPlugin(Plugin, PluginUtils):
self.SearchTypeComboBox.addItem("Authors")
def onClearTextButton(self):
print self.SearchTextEdit.clear()
self.SearchTextEdit.clear()
def onSearchTextEdit(self):
sl = 3
@ -223,7 +223,6 @@ class SongsPlugin(Plugin, PluginUtils):
self.edit_song_form.show()
def onSongEditClick(self):
print "SongEdit"
cr = self.SongListView.currentRow()
id = int(self.SongListView.item(cr, 0).text())
self.edit_song_form.load_song(id)