Added a "re-index" tool for songs.

This commit is contained in:
Raoul Snyman 2010-11-16 08:19:23 +02:00
parent e9e895f969
commit 8ed5bb883e
2 changed files with 88 additions and 0 deletions

View File

@ -158,6 +158,27 @@ class Manager(object):
log.exception(u'Object save failed')
return False
def save_objects(self, object_list, commit=True):
"""
Save a list of objects to the database
``object_list``
The list of objects to save
``commit``
Commit the session with this object
"""
try:
self.session.add_all(object_list)
if commit:
self.session.commit()
self.is_dirty = True
return True
except InvalidRequestError:
self.session.rollback()
log.exception(u'Object list save failed')
return False
def get_object(self, object_class, key=None):
"""
Return the details of an object
@ -207,6 +228,22 @@ class Manager(object):
return query.order_by(order_by_ref).all()
return query.all()
def get_object_count(self, object_class, filter_clause=None):
"""
Returns a count of the number of objects in the database.
``object_class``
The type of objects to return.
``filter_clause``
The filter governing selection of objects to return. Defaults to
None.
"""
query = self.session.query(object_class)
if filter_clause is not None:
query = query.filter(filter_clause)
return query.count()
def delete_object(self, object_class, key):
"""
Delete an object from the database

View File

@ -25,6 +25,7 @@
###############################################################################
import logging
import re
from PyQt4 import QtCore, QtGui
@ -55,6 +56,7 @@ class SongsPlugin(Plugin):
self.manager = Manager(u'songs', init_schema)
self.icon_path = u':/plugins/plugin_songs.png'
self.icon = build_icon(self.icon_path)
self.whitespace = re.compile(r'\W+')
def getSettingsTab(self):
visible_name = self.getString(StringContent.VisibleName)
@ -63,6 +65,7 @@ class SongsPlugin(Plugin):
def initialise(self):
log.info(u'Songs Initialising')
Plugin.initialise(self)
self.toolsReindexItem.setVisible(True)
self.mediaItem.displayResultsSong(
self.manager.get_all_objects(Song, order_by_ref=Song.search_title))
@ -106,6 +109,52 @@ class SongsPlugin(Plugin):
# No menu items for now.
pass
def addToolsMenuItem(self, tools_menu):
"""
Give the alerts plugin the opportunity to add items to the
**Tools** menu.
``tools_menu``
The actual **Tools** menu item, so that your actions can
use it as their parent.
"""
log.info(u'add tools menu')
self.toolsReindexItem = QtGui.QAction(tools_menu)
self.toolsReindexItem.setIcon(build_icon(u':/plugins/plugin_songs.png'))
self.toolsReindexItem.setObjectName(u'toolsReindexItem')
self.toolsReindexItem.setText(translate('SongsPlugin', '&Reindex Songs'))
self.toolsReindexItem.setStatusTip(
translate('SongsPlugin', 'Rsindex the songs database to improve '
'searching and ordering.'))
#self.toolsReindexItem.setShortcut(u'F7')
tools_menu.addAction(self.toolsReindexItem)
QtCore.QObject.connect(self.toolsReindexItem,
QtCore.SIGNAL(u'triggered()'), self.onToolsReindexItemTriggered)
self.toolsReindexItem.setVisible(False)
def onToolsReindexItemTriggered(self):
"""
Rebuild the search title of each song.
"""
maxSongs = self.manager.get_object_count(Song)
progressDialog = QtGui.QProgressDialog(
translate('SongsPlugin', 'Reindexing songs...'),
translate('SongsPlugin', 'Cancel'),
0, maxSongs + 1, self.formparent)
progressDialog.setWindowModality(QtCore.Qt.WindowModal)
songs = self.manager.get_all_objects(Song)
counter = 0
for song in songs:
counter += 1
song.search_title = self.whitespace.sub(u' ', song.title.lower()) +\
u' ' + self.whitespace.sub(u' ', song.alternate_title.lower())
progressDialog.setValue(counter)
self.manager.save_objects(songs)
counter += 1
progressDialog.setValue(counter)
self.mediaItem.displayResultsSong(
self.manager.get_all_objects(Song, order_by_ref=Song.search_title))
def onSongImportItemClicked(self):
if self.mediaItem:
self.mediaItem.onImportClick()
@ -206,4 +255,6 @@ class SongsPlugin(Plugin):
"""
log.info(u'Songs Finalising')
self.manager.finalise()
self.toolsReindexItem.setVisible(False)
Plugin.finalise(self)