← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~raoul-snyman/openlp/reindex into lp:openlp

 

Raoul Snyman has proposed merging lp:~raoul-snyman/openlp/reindex into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)


Added a "re-index" tool for repopulating the "search_title" field of songs.
-- 
https://code.launchpad.net/~raoul-snyman/openlp/reindex/+merge/40932
Your team OpenLP Core is requested to review the proposed merge of lp:~raoul-snyman/openlp/reindex into lp:openlp.
=== modified file 'openlp/core/lib/db.py'
--- openlp/core/lib/db.py	2010-10-28 17:02:28 +0000
+++ openlp/core/lib/db.py	2010-11-16 06:36:11 +0000
@@ -158,6 +158,27 @@
             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 @@
             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

=== modified file 'openlp/plugins/songs/songsplugin.py'
--- openlp/plugins/songs/songsplugin.py	2010-10-27 15:30:30 +0000
+++ openlp/plugins/songs/songsplugin.py	2010-11-16 06:36:11 +0000
@@ -25,6 +25,7 @@
 ###############################################################################
 
 import logging
+import re
 
 from PyQt4 import QtCore, QtGui
 
@@ -55,6 +56,7 @@
         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 @@
     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 @@
         # 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', '&Re-index Songs'))
+        self.toolsReindexItem.setStatusTip(
+            translate('SongsPlugin', 'Re-index 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 @@
         """
         log.info(u'Songs Finalising')
         self.manager.finalise()
+        self.toolsReindexItem.setVisible(False)
         Plugin.finalise(self)
+


Follow ups