From e4ecc3e76381c56500360ff4812a523cead361cb Mon Sep 17 00:00:00 2001 From: Samuel Mehrbrodt Date: Mon, 4 Jan 2016 19:46:00 +0100 Subject: [PATCH] Fix searching with multiple songbooks --- openlp/plugins/songs/forms/songbookdialog.py | 4 +- openlp/plugins/songs/lib/db.py | 2 +- openlp/plugins/songs/lib/mediaitem.py | 49 ++++++++++---------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/openlp/plugins/songs/forms/songbookdialog.py b/openlp/plugins/songs/forms/songbookdialog.py index 5bfed8e87..f132f6eb1 100644 --- a/openlp/plugins/songs/forms/songbookdialog.py +++ b/openlp/plugins/songs/forms/songbookdialog.py @@ -28,7 +28,7 @@ from openlp.core.lib.ui import create_button_box class Ui_SongBookDialog(object): """ - The user interface for the song book dialog. + The user interface for the Songbook dialog. """ def setupUi(self, song_book_dialog): """ @@ -63,6 +63,6 @@ class Ui_SongBookDialog(object): """ Translate the UI on the fly. """ - song_book_dialog.setWindowTitle(translate('SongsPlugin.SongBookForm', 'Song Book Maintenance')) + song_book_dialog.setWindowTitle(translate('SongsPlugin.SongBookForm', 'Songbook Maintenance')) self.name_label.setText(translate('SongsPlugin.SongBookForm', '&Name:')) self.publisher_label.setText(translate('SongsPlugin.SongBookForm', '&Publisher:')) diff --git a/openlp/plugins/songs/lib/db.py b/openlp/plugins/songs/lib/db.py index 986739b08..5cb56c4db 100644 --- a/openlp/plugins/songs/lib/db.py +++ b/openlp/plugins/songs/lib/db.py @@ -383,7 +383,7 @@ def init_schema(url): # Use lazy='joined' to always load authors when the song is fetched from the database (bug 1366198) 'authors': relation(Author, secondary=authors_songs_table, viewonly=True, lazy='joined'), 'media_files': relation(MediaFile, backref='songs', order_by=media_files_table.c.weight), - 'songbookentries': relation(SongBookEntry, cascade="all, delete-orphan"), + 'songbookentries': relation(SongBookEntry, backref='song', cascade="all, delete-orphan"), 'topics': relation(Topic, backref='songs', secondary=songs_topics_table) }) mapper(Topic, topics_table) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 56f5f99fd..295acee4b 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -151,7 +151,7 @@ class SongMediaItem(MediaManagerItem): (SongSearch.Authors, ':/songs/song_search_author.png', SongStrings.Authors, translate('SongsPlugin.MediaItem', 'Search Authors...')), (SongSearch.Books, ':/songs/song_book_edit.png', SongStrings.SongBooks, - translate('SongsPlugin.MediaItem', 'Search Song Books...')), + translate('SongsPlugin.MediaItem', 'Search Songbooks...')), (SongSearch.Themes, ':/slides/slide_theme.png', UiStrings().Themes, UiStrings().SearchThemes) ]) self.search_text_edit.set_current_search_type(Settings().value('%s/last search type' % self.settings_section)) @@ -184,17 +184,8 @@ class SongMediaItem(MediaManagerItem): Author, Author.display_name.like(search_string), Author.display_name.asc()) self.display_results_author(search_results) elif search_type == SongSearch.Books: - log.debug('Books Search') - search_string = '%' + search_keywords + '%' - search_results = self.plugin.manager.get_all_objects(Book, Book.name.like(search_string), Book.name.asc()) - song_number = False - if not search_results: - search_keywords = search_keywords.rpartition(' ') - search_string = '%' + search_keywords[0] + '%' - search_results = self.plugin.manager.get_all_objects(Book, - Book.name.like(search_string), Book.name.asc()) - song_number = re.sub(r'[^0-9]', '', search_keywords[2]) - self.display_results_book(search_results, song_number) + log.debug('Songbook Search') + self.display_results_book(search_keywords) elif search_type == SongSearch.Themes: log.debug('Theme Search') search_string = '%' + search_keywords + '%' @@ -254,21 +245,29 @@ class SongMediaItem(MediaManagerItem): song_name.setData(QtCore.Qt.UserRole, song.id) self.list_view.addItem(song_name) - def display_results_book(self, search_results, song_number=False): + def display_results_book(self, search_keywords): log.debug('display results Book') self.list_view.clear() - for book in search_results: - songs = sorted(book.songs, key=lambda song: int(re.match(r'[0-9]+', '0' + song.song_number).group())) - for song in songs: - # Do not display temporary songs - if song.temporary: - continue - if song_number and song_number not in song.song_number: - continue - song_detail = '%s - %s (%s)' % (book.name, song.song_number, song.title) - song_name = QtWidgets.QListWidgetItem(song_detail) - song_name.setData(QtCore.Qt.UserRole, song.id) - self.list_view.addItem(song_name) + + search_keywords = search_keywords.rpartition(' ') + search_book = search_keywords[0] + search_entry = re.sub(r'[^0-9]', '', search_keywords[2]) + + songbookentries = (self.plugin.manager.session.query(SongBookEntry) + .join(Book) + .order_by(Book.name) + .order_by(SongBookEntry.entry)) + for songbookentry in songbookentries: + if songbookentry.song.temporary: + continue + if search_book.lower() not in songbookentry.songbook.name.lower(): + continue + if search_entry not in songbookentry.entry: + continue + song_detail = '%s #%s: %s' % (songbookentry.songbook.name, songbookentry.entry, songbookentry.song.title) + song_name = QtWidgets.QListWidgetItem(song_detail) + song_name.setData(QtCore.Qt.UserRole, songbookentry.song.id) + self.list_view.addItem(song_name) def on_clear_text_button_click(self): """