← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~meths/openlp/trivialfixes into lp:openlp

 

Jon Tibble has proposed merging lp:~meths/openlp/trivialfixes into lp:openlp.

Requested reviews:
  Raoul Snyman (raoul-snyman): approve
  Tim Bentley (trb143)


Database refactoring:
* Prefer python object filter clauses in queries
* Move another plugin to generic methods and remove its manager.py
-- 
https://code.launchpad.net/~meths/openlp/trivialfixes/+merge/28973
Your team OpenLP Core is subscribed to branch lp:openlp.
=== modified file 'openlp/core/lib/db.py'
--- openlp/core/lib/db.py	2010-06-28 13:38:29 +0000
+++ openlp/core/lib/db.py	2010-07-01 10:34:25 +0000
@@ -166,17 +166,17 @@
         else:
             return self.session.query(object_class).get(key)
 
-    def get_object_filtered(self, object_class, filter_string):
+    def get_object_filtered(self, object_class, filter_clause):
         """
         Returns an object matching specified criteria
 
         ``object_class``
             The type of object to return
 
-        ``filter_string``
+        ``filter_clause``
             The criteria to select the object by
         """
-        return self.session.query(object_class).filter(filter_string).first()
+        return self.session.query(object_class).filter(filter_clause).first()
 
     def get_all_objects(self, object_class, order_by_ref=None):
         """
@@ -188,21 +188,29 @@
         ``order_by_ref``
             Any parameters to order the returned objects by.  Defaults to None.
         """
+        query = self.session.query(object_class)
         if order_by_ref:
-            return self.session.query(object_class).order_by(order_by_ref).all()
-        return self.session.query(object_class).all()
+            return query.order_by(order_by_ref).all()
+        return query.all()
 
-    def get_all_objects_filtered(self, object_class, filter_string):
+    def get_all_objects_filtered(self, object_class, filter_clause,
+        order_by_ref=None):
         """
         Returns a selection of objects from the database
 
         ``object_class``
             The type of objects to return
 
-        ``filter_string``
+        ``filter_clause``
             The filter governing selection of objects to return
+
+        ``order_by_ref``
+            Any parameters to order the returned objects by.  Defaults to None.
         """
-        return self.session.query(object_class).filter(filter_string).all()
+        query = self.session.query(object_class).filter(filter_clause)
+        if order_by_ref:
+            return query.order_by(order_by_ref).all()
+        return query.all()
 
     def delete_object(self, object_class, key):
         """

=== modified file 'openlp/plugins/bibles/lib/db.py'
--- openlp/plugins/bibles/lib/db.py	2010-06-28 13:38:29 +0000
+++ openlp/plugins/bibles/lib/db.py	2010-07-01 10:34:25 +0000
@@ -299,11 +299,10 @@
             The name of the book to return
         """
         log.debug(u'BibleDb.get_book("%s")', book)
-        db_book = self.session.query(Book).filter(
-            Book.name.like(book + u'%')).first()
+        db_book = self.get_object_filtered(Book, Book.name.like(book + u'%'))
         if db_book is None:
-            db_book = self.session.query(Book).filter(
-                Book.abbreviation.like(book + u'%')).first()
+            db_book = self.get_object_filtered(Book,
+                Book.abbreviation.like(book + u'%'))
         return db_book
 
     def get_verses(self, reference_list):

=== modified file 'openlp/plugins/custom/customplugin.py'
--- openlp/plugins/custom/customplugin.py	2010-06-30 18:16:07 +0000
+++ openlp/plugins/custom/customplugin.py	2010-07-01 10:34:25 +0000
@@ -78,8 +78,7 @@
         return about_text
 
     def can_delete_theme(self, theme):
-        filter_string = u'theme_name=\'%s\'' % theme
         if not self.custommanager.get_all_objects_filtered(CustomSlide,
-            filter_string):
+            CustomSlide.theme_name == theme):
             return True
         return False

=== modified file 'openlp/plugins/songs/forms/songimportform.py'
--- openlp/plugins/songs/forms/songimportform.py	2010-06-21 16:43:59 +0000
+++ openlp/plugins/songs/forms/songimportform.py	2010-07-01 10:34:25 +0000
@@ -30,7 +30,7 @@
 from songimportwizard import Ui_SongImportWizard
 from openlp.core.lib import Receiver, SettingsManager, translate
 #from openlp.core.utils import AppLocation
-from openlp.plugins.songs.lib.manager import SongFormat
+from openlp.plugins.songs.lib import SongFormat
 
 log = logging.getLogger(__name__)
 

=== modified file 'openlp/plugins/songs/lib/__init__.py'
--- openlp/plugins/songs/lib/__init__.py	2010-06-24 20:48:38 +0000
+++ openlp/plugins/songs/lib/__init__.py	2010-07-01 10:34:25 +0000
@@ -25,6 +25,52 @@
 
 from openlp.core.lib import translate
 
+#from openlp.plugins.songs.lib import OpenLyricsSong, OpenSongSong, CCLISong, \
+#    CSVSong
+
+class SongFormat(object):
+    """
+    This is a special enumeration class that holds the various types of songs,
+    plus a few helper functions to facilitate generic handling of song types
+    for importing.
+    """
+    Unknown = -1
+    OpenLyrics = 0
+    OpenSong = 1
+    CCLI = 2
+    CSV = 3
+
+    @staticmethod
+    def get_class(id):
+        """
+        Return the appropriate imeplementation class.
+
+        ``id``
+            The song format.
+        """
+#        if id == SongFormat.OpenLyrics:
+#            return OpenLyricsSong
+#        elif id == SongFormat.OpenSong:
+#            return OpenSongSong
+#        elif id == SongFormat.CCLI:
+#            return CCLISong
+#        elif id == SongFormat.CSV:
+#            return CSVSong
+#        else:
+        return None
+
+    @staticmethod
+    def list():
+        """
+        Return a list of the supported song formats.
+        """
+        return [
+            SongFormat.OpenLyrics,
+            SongFormat.OpenSong,
+            SongFormat.CCLI,
+            SongFormat.CSV
+        ]
+
 class VerseType(object):
     """
     VerseType provides an enumeration for the tags that may be associated
@@ -91,7 +137,6 @@
             unicode(VerseType.to_string(VerseType.Other)).lower():
             return VerseType.Other
 
-from manager import SongManager
 from songstab import SongsTab
 from mediaitem import SongMediaItem
 from songimport import SongImport

=== removed file 'openlp/plugins/songs/lib/manager.py'
--- openlp/plugins/songs/lib/manager.py	2010-06-15 18:08:02 +0000
+++ openlp/plugins/songs/lib/manager.py	1970-01-01 00:00:00 +0000
@@ -1,116 +0,0 @@
-# -*- coding: utf-8 -*-
-# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
-
-###############################################################################
-# OpenLP - Open Source Lyrics Projection                                      #
-# --------------------------------------------------------------------------- #
-# Copyright (c) 2008-2010 Raoul Snyman                                        #
-# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael      #
-# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin      #
-# Thompson, Jon Tibble, Carsten Tinggaard                                     #
-# --------------------------------------------------------------------------- #
-# This program is free software; you can redistribute it and/or modify it     #
-# under the terms of the GNU General Public License as published by the Free  #
-# Software Foundation; version 2 of the License.                              #
-#                                                                             #
-# This program is distributed in the hope that it will be useful, but WITHOUT #
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
-# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    #
-# more details.                                                               #
-#                                                                             #
-# You should have received a copy of the GNU General Public License along     #
-# with this program; if not, write to the Free Software Foundation, Inc., 59  #
-# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
-###############################################################################
-
-import logging
-
-from openlp.core.lib.db import Manager
-from openlp.plugins.songs.lib.db import init_schema, Song, Author
-#from openlp.plugins.songs.lib import OpenLyricsSong, OpenSongSong, CCLISong, \
-#    CSVSong
-
-log = logging.getLogger(__name__)
-
-class SongFormat(object):
-    """
-    This is a special enumeration class that holds the various types of songs,
-    plus a few helper functions to facilitate generic handling of song types
-    for importing.
-    """
-    Unknown = -1
-    OpenLyrics = 0
-    OpenSong = 1
-    CCLI = 2
-    CSV = 3
-
-    @staticmethod
-    def get_class(id):
-        """
-        Return the appropriate imeplementation class.
-
-        ``id``
-            The song format.
-        """
-#        if id == SongFormat.OpenLyrics:
-#            return OpenLyricsSong
-#        elif id == SongFormat.OpenSong:
-#            return OpenSongSong
-#        elif id == SongFormat.CCLI:
-#            return CCLISong
-#        elif id == SongFormat.CSV:
-#            return CSVSong
-#        else:
-        return None
-
-    @staticmethod
-    def list():
-        """
-        Return a list of the supported song formats.
-        """
-        return [
-            SongFormat.OpenLyrics,
-            SongFormat.OpenSong,
-            SongFormat.CCLI,
-            SongFormat.CSV
-        ]
-
-
-class SongManager(Manager):
-    """
-    The Song Manager provides a central location for all database code. This
-    class takes care of connecting to the database and running all the queries.
-    """
-    log.info(u'Song manager loaded')
-
-    def __init__(self):
-        """
-        Creates the connection to the database, and creates the tables if they
-        don't exist.
-        """
-        log.debug(u'Song Initialising')
-        Manager.__init__(self, u'songs', init_schema)
-        log.debug(u'Song Initialised')
-
-    def search_song_title(self, keywords):
-        """
-        Searches the song title for keywords.
-        """
-        return self.session.query(Song).filter(
-            Song.search_title.like(u'%' + keywords + u'%')).order_by(
-                Song.search_title.asc()).all()
-
-    def search_song_lyrics(self, keywords):
-        """
-        Searches the song lyrics for keywords.
-        """
-        return self.session.query(Song).filter(
-            Song.search_lyrics.like(u'%' + keywords + u'%')).order_by(
-                Song.search_lyrics.asc()).all()
-
-    def get_song_from_author(self, keywords):
-        """
-        Searches the song authors for keywords.
-        """
-        return self.session.query(Author).filter(Author.display_name.like(
-            u'%' + keywords + u'%')).order_by(Author.display_name.asc()).all()

=== modified file 'openlp/plugins/songs/lib/mediaitem.py'
--- openlp/plugins/songs/lib/mediaitem.py	2010-06-25 00:30:26 +0000
+++ openlp/plugins/songs/lib/mediaitem.py	2010-07-01 10:34:25 +0000
@@ -165,18 +165,21 @@
         search_type = self.SearchTypeComboBox.currentIndex()
         if search_type == 0:
             log.debug(u'Titles Search')
-            search_results = self.parent.manager.search_song_title(
-                search_keywords)
+            search_results = self.parent.manager.get_all_objects_filtered(Song,
+                Song.search_title.like(u'%' + keywords + u'%'),
+                Song.search_title.asc())
             self.displayResultsSong(search_results)
         elif search_type == 1:
             log.debug(u'Lyrics Search')
-            search_results = self.parent.manager.search_song_lyrics(
-                search_keywords)
+            search_results = self.parent.manager.get_all_objects_filtered(Song,
+                Song.search_lyrics.like(u'%' + keywords + u'%'),
+                Song.search_lyrics.asc())
             self.displayResultsSong(search_results)
         elif search_type == 2:
             log.debug(u'Authors Search')
-            search_results = self.parent.manager.get_song_from_author(
-                search_keywords)
+            search_results = self.parent.manager.get_all_objects_filtered(
+                Author, Author.display_name.like(u'%' + search_keywords + u'%'),
+                Author.display_name.asc())
             self.displayResultsAuthor(search_results)
         #Called to redisplay the song list screen edith from a search
         #or from the exit of the Song edit dialog.  If remote editing is active

=== modified file 'openlp/plugins/songs/lib/songimport.py'
--- openlp/plugins/songs/lib/songimport.py	2010-06-28 13:38:29 +0000
+++ openlp/plugins/songs/lib/songimport.py	2010-07-01 10:34:25 +0000
@@ -302,8 +302,8 @@
         song.theme_name = self.theme_name
         song.ccli_number = self.ccli_number
         for authortext in self.authors:
-            filter_string = u'display_name=%s' % authortext
-            author = self.manager.get_object_filtered(Author, filter_string)
+            author = self.manager.get_object_filtered(Author,
+                Author.display_name == authortext)
             if author is None:
                 author = Author()
                 author.display_name = authortext
@@ -312,8 +312,8 @@
                 self.manager.save_object(author)
             song.authors.append(author)
         if self.song_book_name:
-            filter_string = u'name=%s' % self.song_book_name
-            song_book = self.manager.get_object_filtered(Book, filter_string)
+            song_book = self.manager.get_object_filtered(Book,
+                Book.name == self.song_book_name)
             if song_book is None:
                 song_book = Book()
                 song_book.name = self.song_book_name
@@ -321,8 +321,8 @@
                 self.manager.save_object(song_book)
             song.song_book_id = song_book.id
         for topictext in self.topics:
-            filter_string = u'name=%s' % topictext
-            topic = self.manager.get_object_filtered(Topic, filter_string)
+            topic = self.manager.get_object_filtered(Topic,
+                Topic.name == topictext)
             if topic is None:
                 topic = Topic()
                 topic.name = topictext

=== modified file 'openlp/plugins/songs/songsplugin.py'
--- openlp/plugins/songs/songsplugin.py	2010-06-30 18:16:07 +0000
+++ openlp/plugins/songs/songsplugin.py	2010-07-01 10:34:25 +0000
@@ -29,8 +29,9 @@
 
 from openlp.core.lib import Plugin, build_icon, PluginStatus, Receiver, \
     translate
-from openlp.plugins.songs.lib import SongManager, SongMediaItem, SongsTab
-from openlp.plugins.songs.lib.db import Song
+from openlp.core.lib.db import Manager
+from openlp.plugins.songs.lib import SongMediaItem, SongsTab
+from openlp.plugins.songs.lib.db import init_schema, Song
 
 try:
     from openlp.plugins.songs.lib import SofImport, OooImport
@@ -56,7 +57,7 @@
         """
         Plugin.__init__(self, u'Songs', u'1.9.2', plugin_helpers)
         self.weight = -10
-        self.manager = SongManager()
+        self.manager = Manager(u'songs', init_schema)
         self.icon = build_icon(u':/plugins/plugin_songs.png')
         self.status = PluginStatus.Active
 
@@ -65,8 +66,6 @@
 
     def initialise(self):
         log.info(u'Songs Initialising')
-        #if self.songmanager is None:
-        #    self.songmanager = SongManager()
         Plugin.initialise(self)
         self.insert_toolbox_item()
         self.media_item.displayResultsSong(
@@ -199,7 +198,7 @@
         return about_text
 
     def can_delete_theme(self, theme):
-        filter_string = u'theme_name=\'%s\'' % theme
-        if not self.manager.get_all_objects_filtered(Song, filter_string):
+        if not self.manager.get_all_objects_filtered(Song,
+            Song.theme_name == theme):
             return True
         return False


Follow ups