From abc25744a2b02b5a47087c3a03b7f9546acbda22 Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Sun, 3 Apr 2016 22:20:40 +0100 Subject: [PATCH 01/10] Remove second sort from author, topic, theme search, change all sorting from sorted() to sort() --- openlp/plugins/songs/lib/mediaitem.py | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index b84e557c2..b709bd343 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -194,13 +194,13 @@ class SongMediaItem(MediaManagerItem): log.debug('Authors Search') search_string = '%' + search_keywords + '%' search_results = self.plugin.manager.get_all_objects( - Author, Author.display_name.like(search_string), Author.display_name.asc()) + Author, Author.display_name.like(search_string)) self.display_results_author(search_results) elif search_type == SongSearch.Topics: log.debug('Topics Search') search_string = '%' + search_keywords + '%' search_results = self.plugin.manager.get_all_objects( - Topic, Topic.name.like(search_string), Topic.name.asc()) + Topic, Topic.name.like(search_string)) self.display_results_topic(search_results) elif search_type == SongSearch.Books: log.debug('Songbook Search') @@ -215,7 +215,7 @@ class SongMediaItem(MediaManagerItem): log.debug('Theme Search') search_string = '%' + search_keywords + '%' search_results = self.plugin.manager.get_all_objects( - Song, Song.theme_name.like(search_string), Song.theme_name.asc()) + Song, Song.theme_name.like(search_string)) self.display_results_themes(search_results) elif search_type == SongSearch.Copyright: log.debug('Copyright Search') @@ -285,10 +285,10 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Author') self.list_view.clear() - search_results = sorted(search_results, key=lambda author: get_natural_key(author.display_name)) + search_results.sort(key=lambda author: get_natural_key(author.display_name)) for author in search_results: - songs = sorted(author.songs, key=lambda song: song.sort_key) - for song in songs: + author.songs.sort(key=lambda song: song.sort_key) + for song in author.songs: # Do not display temporary songs if song.temporary: continue @@ -306,8 +306,8 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Book') self.list_view.clear() - search_results = sorted(search_results, key=lambda songbook_entry: - (get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry))) + search_results.sort(key=lambda songbook_entry: + (get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry))) for songbook_entry in search_results: if songbook_entry.song.temporary: continue @@ -325,10 +325,10 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Topic') self.list_view.clear() - search_results = sorted(search_results, key=lambda topic: get_natural_key(topic.name)) + search_results.sort(key=lambda topic: get_natural_key(topic.name)) for topic in search_results: - songs = sorted(topic.songs, key=lambda song: song.sort_key) - for song in songs: + topic.songs.sort(key=lambda song: song.sort_key) + for song in topic.songs: # Do not display temporary songs if song.temporary: continue @@ -346,8 +346,8 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Themes') self.list_view.clear() - search_results = sorted(search_results, key=lambda song: (get_natural_key(song.theme_name), - song.sort_key)) + search_results.sort(key=lambda song: (get_natural_key(song.theme_name), + song.sort_key)) for song in search_results: # Do not display temporary songs if song.temporary: @@ -366,9 +366,9 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results CCLI number') self.list_view.clear() - songs = sorted(search_results, key=lambda song: (get_natural_key(song.ccli_number), - song.sort_key)) - for song in songs: + search_results.sort(key=lambda song: (get_natural_key(song.ccli_number), + song.sort_key)) + for song in search_results: # Do not display temporary songs if song.temporary: continue From 74497b92299939a953f38da34101794253d2844d Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Tue, 5 Apr 2016 18:55:22 +0100 Subject: [PATCH 02/10] Implement natural sort for authors, topics, songbooks, themes in Edit Song comboboxen, Song Maintenance form --- openlp/plugins/songs/forms/editsongform.py | 8 ++++++-- openlp/plugins/songs/forms/songmaintenanceform.py | 10 +++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index 678169e64..8da3fdde2 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -34,6 +34,7 @@ from PyQt5 import QtCore, QtWidgets from openlp.core.common import Registry, RegistryProperties, AppLocation, UiStrings, check_directory_exists, translate from openlp.core.lib import FileDialog, PluginStatus, MediaType, create_separated_list from openlp.core.lib.ui import set_case_insensitive_completer, critical_error_message_box, find_and_set_in_combo_box +from openlp.core.utils import get_natural_key from openlp.plugins.songs.lib import VerseType, clean_song from openlp.plugins.songs.lib.db import Book, Song, Author, AuthorType, Topic, MediaFile, SongBookEntry from openlp.plugins.songs.lib.ui import SongStrings @@ -110,7 +111,8 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): """ Generically load a set of objects into a cache and a combobox. """ - objects = self.manager.get_all_objects(cls, order_by_ref=cls.name) + objects = self.manager.get_all_objects(cls) + objects.sort(key=lambda object: get_natural_key(object.name)) combo.clear() combo.addItem('') for obj in objects: @@ -343,7 +345,8 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): """ Load the authors from the database into the combobox. """ - authors = self.manager.get_all_objects(Author, order_by_ref=Author.display_name) + authors = self.manager.get_all_objects(Author) + authors.sort(key=lambda author: get_natural_key(author.display_name)) self.authors_combo_box.clear() self.authors_combo_box.addItem('') self.authors = [] @@ -381,6 +384,7 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): self.theme_combo_box.clear() self.theme_combo_box.addItem('') self.themes = theme_list + self.themes.sort(key=lambda theme: get_natural_key(theme)) self.theme_combo_box.addItems(theme_list) set_case_insensitive_completer(self.themes, self.theme_combo_box) diff --git a/openlp/plugins/songs/forms/songmaintenanceform.py b/openlp/plugins/songs/forms/songmaintenanceform.py index 1fdfb74d4..470beda82 100644 --- a/openlp/plugins/songs/forms/songmaintenanceform.py +++ b/openlp/plugins/songs/forms/songmaintenanceform.py @@ -27,6 +27,7 @@ from sqlalchemy.sql import and_ from openlp.core.common import Registry, RegistryProperties, UiStrings, translate from openlp.core.lib.ui import critical_error_message_box +from openlp.core.utils import get_natural_key from openlp.plugins.songs.forms.authorsform import AuthorsForm from openlp.plugins.songs.forms.topicsform import TopicsForm from openlp.plugins.songs.forms.songbookform import SongBookForm @@ -121,7 +122,8 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP Reloads the Authors list. """ self.authors_list_widget.clear() - authors = self.manager.get_all_objects(Author, order_by_ref=Author.display_name) + authors = self.manager.get_all_objects(Author) + authors.sort(key=lambda author: get_natural_key(author.display_name)) for author in authors: if author.display_name: author_name = QtWidgets.QListWidgetItem(author.display_name) @@ -135,7 +137,8 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP Reloads the Topics list. """ self.topics_list_widget.clear() - topics = self.manager.get_all_objects(Topic, order_by_ref=Topic.name) + topics = self.manager.get_all_objects(Topic) + topics.sort(key=lambda topic: get_natural_key(topic.name)) for topic in topics: topic_name = QtWidgets.QListWidgetItem(topic.name) topic_name.setData(QtCore.Qt.UserRole, topic.id) @@ -146,7 +149,8 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP Reloads the Books list. """ self.song_books_list_widget.clear() - books = self.manager.get_all_objects(Book, order_by_ref=Book.name) + books = self.manager.get_all_objects(Book) + books.sort(key=lambda book: get_natural_key(book.name)) for book in books: book_name = QtWidgets.QListWidgetItem('%s (%s)' % (book.name, book.publisher)) book_name.setData(QtCore.Qt.UserRole, book.id) From dfb34a5156da99a1689e9e1e3420f3c94ce69c94 Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Tue, 12 Apr 2016 21:17:12 +0100 Subject: [PATCH 03/10] Enable auto-select for all searches when item previously selected --- openlp/plugins/songs/lib/mediaitem.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 7eb53e511..0bbea1fa0 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -284,6 +284,7 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results Author') + self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda author: get_natural_key(author.display_name)) for author in search_results: @@ -296,6 +297,10 @@ class SongMediaItem(MediaManagerItem): song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, song.id) self.list_view.addItem(song_name) + # Auto-select the item if name has been set + if song.id == self.auto_select_id: + self.list_view.setCurrentItem(song_name) + self.auto_select_id = -1 def display_results_book(self, search_results): """ @@ -305,6 +310,7 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results Book') + self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda songbook_entry: (get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry))) @@ -315,6 +321,10 @@ class SongMediaItem(MediaManagerItem): song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, songbook_entry.song.id) self.list_view.addItem(song_name) + # Auto-select the item if name has been set + if songbook_entry.song.id == self.auto_select_id: + self.list_view.setCurrentItem(song_name) + self.auto_select_id = -1 def display_results_topic(self, search_results): """ @@ -324,6 +334,7 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results Topic') + self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda topic: get_natural_key(topic.name)) for topic in search_results: @@ -336,6 +347,10 @@ class SongMediaItem(MediaManagerItem): song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, song.id) self.list_view.addItem(song_name) + # Auto-select the item if name has been set + if song.id == self.auto_select_id: + self.list_view.setCurrentItem(song_name) + self.auto_select_id = -1 def display_results_themes(self, search_results): """ @@ -345,6 +360,7 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results Themes') + self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda song: (get_natural_key(song.theme_name), song.sort_key)) @@ -356,6 +372,10 @@ class SongMediaItem(MediaManagerItem): song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, song.id) self.list_view.addItem(song_name) + # Auto-select the item if name has been set + if song.id == self.auto_select_id: + self.list_view.setCurrentItem(song_name) + self.auto_select_id = -1 def display_results_cclinumber(self, search_results): """ @@ -365,6 +385,7 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results CCLI number') + self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda song: (get_natural_key(song.ccli_number), song.sort_key)) @@ -376,6 +397,10 @@ class SongMediaItem(MediaManagerItem): song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, song.id) self.list_view.addItem(song_name) + # Auto-select the item if name has been set + if song.id == self.auto_select_id: + self.list_view.setCurrentItem(song_name) + self.auto_select_id = -1 def on_clear_text_button_click(self): """ From 0c8f9e3b712b50b07901c29972e28f2ca43c1f58 Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Sun, 17 Apr 2016 22:22:30 +0100 Subject: [PATCH 04/10] Removed auto-select for searches where multiple IDs selected, added tests to hide temporary songs --- openlp/plugins/songs/lib/mediaitem.py | 26 +----- .../openlp_plugins/songs/test_mediaitem.py | 83 +++++++++++++++---- 2 files changed, 66 insertions(+), 43 deletions(-) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 0bbea1fa0..2c483a48a 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -284,7 +284,6 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results Author') - self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda author: get_natural_key(author.display_name)) for author in search_results: @@ -297,10 +296,6 @@ class SongMediaItem(MediaManagerItem): song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, song.id) self.list_view.addItem(song_name) - # Auto-select the item if name has been set - if song.id == self.auto_select_id: - self.list_view.setCurrentItem(song_name) - self.auto_select_id = -1 def display_results_book(self, search_results): """ @@ -310,21 +305,17 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results Book') - self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda songbook_entry: (get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry))) for songbook_entry in search_results: + # Do not display temporary songs if songbook_entry.song.temporary: continue song_detail = '%s #%s: %s' % (songbook_entry.songbook.name, songbook_entry.entry, songbook_entry.song.title) song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, songbook_entry.song.id) self.list_view.addItem(song_name) - # Auto-select the item if name has been set - if songbook_entry.song.id == self.auto_select_id: - self.list_view.setCurrentItem(song_name) - self.auto_select_id = -1 def display_results_topic(self, search_results): """ @@ -334,7 +325,6 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results Topic') - self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda topic: get_natural_key(topic.name)) for topic in search_results: @@ -347,10 +337,6 @@ class SongMediaItem(MediaManagerItem): song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, song.id) self.list_view.addItem(song_name) - # Auto-select the item if name has been set - if song.id == self.auto_select_id: - self.list_view.setCurrentItem(song_name) - self.auto_select_id = -1 def display_results_themes(self, search_results): """ @@ -360,7 +346,6 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results Themes') - self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda song: (get_natural_key(song.theme_name), song.sort_key)) @@ -372,10 +357,6 @@ class SongMediaItem(MediaManagerItem): song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, song.id) self.list_view.addItem(song_name) - # Auto-select the item if name has been set - if song.id == self.auto_select_id: - self.list_view.setCurrentItem(song_name) - self.auto_select_id = -1 def display_results_cclinumber(self, search_results): """ @@ -385,7 +366,6 @@ class SongMediaItem(MediaManagerItem): :return: None """ log.debug('display results CCLI number') - self.save_auto_select_id() self.list_view.clear() search_results.sort(key=lambda song: (get_natural_key(song.ccli_number), song.sort_key)) @@ -397,10 +377,6 @@ class SongMediaItem(MediaManagerItem): song_name = QtWidgets.QListWidgetItem(song_detail) song_name.setData(QtCore.Qt.UserRole, song.id) self.list_view.addItem(song_name) - # Auto-select the item if name has been set - if song.id == self.auto_select_id: - self.list_view.setCurrentItem(song_name) - self.auto_select_id = -1 def on_clear_text_button_click(self): """ diff --git a/tests/functional/openlp_plugins/songs/test_mediaitem.py b/tests/functional/openlp_plugins/songs/test_mediaitem.py index 3cd5f97ba..4b9fd50ee 100644 --- a/tests/functional/openlp_plugins/songs/test_mediaitem.py +++ b/tests/functional/openlp_plugins/songs/test_mediaitem.py @@ -53,6 +53,7 @@ class TestMediaItem(TestCase, TestMixin): self.media_item.list_view.save_auto_select_id = MagicMock() self.media_item.list_view.clear = MagicMock() self.media_item.list_view.addItem = MagicMock() + self.media_item.list_view.setCurrentItem = MagicMock() self.media_item.auto_select_id = -1 self.media_item.display_songbook = False self.media_item.display_copyright_symbol = False @@ -79,13 +80,22 @@ class TestMediaItem(TestCase, TestMixin): mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.authors = [] + mock_song_temp = MagicMock() + mock_song_temp.id = 2 + mock_song_temp.title = 'My Temporary' + mock_song_temp.sort_key = 'My Temporary' + mock_song_temp.authors = [] mock_author = MagicMock() mock_author.display_name = 'My Author' mock_song.authors.append(mock_author) + mock_song_temp.authors.append(mock_author) mock_song.temporary = False + mock_song_temp.temporary = True mock_search_results.append(mock_song) + mock_search_results.append(mock_song_temp) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget + self.media_item.auto_select_id = 1 # WHEN: I display song search results self.media_item.display_results_song(mock_search_results) @@ -93,9 +103,10 @@ class TestMediaItem(TestCase, TestMixin): # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() self.media_item.save_auto_select_id.assert_called_with() - MockedQListWidgetItem.assert_called_with('My Song (My Author)') - mock_qlist_widget.setData.assert_called_with(MockedUserRole, mock_song.id) - self.media_item.list_view.addItem.assert_called_with(mock_qlist_widget) + MockedQListWidgetItem.assert_called_once_with('My Song (My Author)') + mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id) + self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget) + self.media_item.list_view.setCurrentItem.assert_called_with(mock_qlist_widget) def display_results_author_test(self): """ @@ -107,13 +118,19 @@ class TestMediaItem(TestCase, TestMixin): mock_search_results = [] mock_author = MagicMock() mock_song = MagicMock() + mock_song_temp = MagicMock() mock_author.display_name = 'My Author' mock_author.songs = [] mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.temporary = False + mock_song_temp.id = 2 + mock_song_temp.title = 'My Temporary' + mock_song_temp.sort_key = 'My Temporary' + mock_song_temp.temporary = True mock_author.songs.append(mock_song) + mock_author.songs.append(mock_song_temp) mock_search_results.append(mock_author) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget @@ -123,9 +140,9 @@ class TestMediaItem(TestCase, TestMixin): # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() - MockedQListWidgetItem.assert_called_with('My Author (My Song)') - mock_qlist_widget.setData.assert_called_with(MockedUserRole, mock_song.id) - self.media_item.list_view.addItem.assert_called_with(mock_qlist_widget) + MockedQListWidgetItem.assert_called_once_with('My Author (My Song)') + mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id) + self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget) def display_results_book_test(self): """ @@ -136,17 +153,27 @@ class TestMediaItem(TestCase, TestMixin): patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole: mock_search_results = [] mock_songbook_entry = MagicMock() + mock_songbook_entry_temp = MagicMock() mock_songbook = MagicMock() mock_song = MagicMock() + mock_song_temp = MagicMock() mock_songbook_entry.entry = '1' + mock_songbook_entry_temp.entry = '2' mock_songbook.name = 'My Book' mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.temporary = False + mock_song_temp.id = 2 + mock_song_temp.title = 'My Temporary' + mock_song_temp.sort_key = 'My Temporary' + mock_song_temp.temporary = True mock_songbook_entry.song = mock_song mock_songbook_entry.songbook = mock_songbook + mock_songbook_entry_temp.song = mock_song_temp + mock_songbook_entry_temp.songbook = mock_songbook mock_search_results.append(mock_songbook_entry) + mock_search_results.append(mock_songbook_entry_temp) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget @@ -155,9 +182,9 @@ class TestMediaItem(TestCase, TestMixin): # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() - MockedQListWidgetItem.assert_called_with('My Book #1: My Song') - mock_qlist_widget.setData.assert_called_with(MockedUserRole, mock_songbook_entry.song.id) - self.media_item.list_view.addItem.assert_called_with(mock_qlist_widget) + MockedQListWidgetItem.assert_called_once_with('My Book #1: My Song') + mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_songbook_entry.song.id) + self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget) def display_results_topic_test(self): """ @@ -169,13 +196,19 @@ class TestMediaItem(TestCase, TestMixin): mock_search_results = [] mock_topic = MagicMock() mock_song = MagicMock() + mock_song_temp = MagicMock() mock_topic.name = 'My Topic' mock_topic.songs = [] mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.temporary = False + mock_song_temp.id = 2 + mock_song_temp.title = 'My Temporary' + mock_song_temp.sort_key = 'My Temporary' + mock_song_temp.temporary = True mock_topic.songs.append(mock_song) + mock_topic.songs.append(mock_song_temp) mock_search_results.append(mock_topic) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget @@ -185,9 +218,9 @@ class TestMediaItem(TestCase, TestMixin): # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() - MockedQListWidgetItem.assert_called_with('My Topic (My Song)') - mock_qlist_widget.setData.assert_called_with(MockedUserRole, mock_song.id) - self.media_item.list_view.addItem.assert_called_with(mock_qlist_widget) + MockedQListWidgetItem.assert_called_once_with('My Topic (My Song)') + mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id) + self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget) def display_results_themes_test(self): """ @@ -198,12 +231,19 @@ class TestMediaItem(TestCase, TestMixin): patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole: mock_search_results = [] mock_song = MagicMock() + mock_song_temp = MagicMock() mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.theme_name = 'My Theme' mock_song.temporary = False + mock_song_temp.id = 2 + mock_song_temp.title = 'My Temporary' + mock_song_temp.sort_key = 'My Temporary' + mock_song_temp.theme_name = 'My Theme' + mock_song_temp.temporary = True mock_search_results.append(mock_song) + mock_search_results.append(mock_song_temp) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget @@ -212,9 +252,9 @@ class TestMediaItem(TestCase, TestMixin): # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() - MockedQListWidgetItem.assert_called_with('My Theme (My Song)') - mock_qlist_widget.setData.assert_called_with(MockedUserRole, mock_song.id) - self.media_item.list_view.addItem.assert_called_with(mock_qlist_widget) + MockedQListWidgetItem.assert_called_once_with('My Theme (My Song)') + mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id) + self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget) def display_results_cclinumber_test(self): """ @@ -225,12 +265,19 @@ class TestMediaItem(TestCase, TestMixin): patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole: mock_search_results = [] mock_song = MagicMock() + mock_song_temp = MagicMock() mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.ccli_number = '12345' mock_song.temporary = False + mock_song_temp.id = 2 + mock_song_temp.title = 'My Temporary' + mock_song_temp.sort_key = 'My Temporary' + mock_song_temp.ccli_number = '12346' + mock_song_temp.temporary = True mock_search_results.append(mock_song) + mock_search_results.append(mock_song_temp) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget @@ -239,9 +286,9 @@ class TestMediaItem(TestCase, TestMixin): # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() - MockedQListWidgetItem.assert_called_with('12345 (My Song)') - mock_qlist_widget.setData.assert_called_with(MockedUserRole, mock_song.id) - self.media_item.list_view.addItem.assert_called_with(mock_qlist_widget) + MockedQListWidgetItem.assert_called_once_with('12345 (My Song)') + mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id) + self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget) def build_song_footer_one_author_test(self): """ From 2dd3cdc5ea1fdd25e1923d98e7d1854e57f6e38c Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Sun, 17 Apr 2016 22:32:42 +0100 Subject: [PATCH 05/10] Remove theme natural sort - already done by filename --- openlp/plugins/songs/forms/editsongform.py | 1 - 1 file changed, 1 deletion(-) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index b1a22f1cf..cc2332fd3 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -384,7 +384,6 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): self.theme_combo_box.clear() self.theme_combo_box.addItem('') self.themes = theme_list - self.themes.sort(key=lambda theme: get_natural_key(theme)) self.theme_combo_box.addItems(theme_list) set_case_insensitive_completer(self.themes, self.theme_combo_box) From a38daf9779c68c7e48ef8d1458094c606f48b3e0 Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Sun, 17 Apr 2016 22:41:29 +0100 Subject: [PATCH 06/10] natural sort for theme in edit song form --- openlp/plugins/songs/forms/editsongform.py | 1 + 1 file changed, 1 insertion(+) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index cc2332fd3..b1a22f1cf 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -384,6 +384,7 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): self.theme_combo_box.clear() self.theme_combo_box.addItem('') self.themes = theme_list + self.themes.sort(key=lambda theme: get_natural_key(theme)) self.theme_combo_box.addItems(theme_list) set_case_insensitive_completer(self.themes, self.theme_combo_box) From d76965f8fd1a62f23ee795283bb5a15c0d80d2ee Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Tue, 19 Apr 2016 20:00:45 +0100 Subject: [PATCH 07/10] Extracted lambdas from sorts in song plugin - improve performance --- openlp/plugins/songs/forms/editsongform.py | 9 ++++--- openlp/plugins/songs/forms/songexportform.py | 3 ++- .../songs/forms/songmaintenanceform.py | 9 ++++--- openlp/plugins/songs/lib/mediaitem.py | 27 +++++++++++-------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index b1a22f1cf..cfe183db6 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -112,7 +112,8 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): Generically load a set of objects into a cache and a combobox. """ objects = self.manager.get_all_objects(cls) - objects.sort(key=lambda object: get_natural_key(object.name)) + get_key = lambda object: get_natural_key(object.name) + objects.sort(key=get_key) combo.clear() combo.addItem('') for obj in objects: @@ -346,7 +347,8 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): Load the authors from the database into the combobox. """ authors = self.manager.get_all_objects(Author) - authors.sort(key=lambda author: get_natural_key(author.display_name)) + get_author_key = lambda author: get_natural_key(author.display_name) + authors.sort(key=get_author_key) self.authors_combo_box.clear() self.authors_combo_box.addItem('') self.authors = [] @@ -384,7 +386,8 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): self.theme_combo_box.clear() self.theme_combo_box.addItem('') self.themes = theme_list - self.themes.sort(key=lambda theme: get_natural_key(theme)) + get_theme_key = lambda theme: get_natural_key(theme) + self.themes.sort(key=get_theme_key) self.theme_combo_box.addItems(theme_list) set_case_insensitive_completer(self.themes, self.theme_combo_box) diff --git a/openlp/plugins/songs/forms/songexportform.py b/openlp/plugins/songs/forms/songexportform.py index ee35ea7e5..fbf909552 100644 --- a/openlp/plugins/songs/forms/songexportform.py +++ b/openlp/plugins/songs/forms/songexportform.py @@ -213,7 +213,8 @@ class SongExportForm(OpenLPWizard): # Load the list of songs. self.application.set_busy_cursor() songs = self.plugin.manager.get_all_objects(Song) - songs.sort(key=lambda song: song.sort_key) + get_song_key = lambda song: song.sort_key + songs.sort(key=get_song_key) for song in songs: # No need to export temporary songs. if song.temporary: diff --git a/openlp/plugins/songs/forms/songmaintenanceform.py b/openlp/plugins/songs/forms/songmaintenanceform.py index 6089c94e7..52c84acbd 100644 --- a/openlp/plugins/songs/forms/songmaintenanceform.py +++ b/openlp/plugins/songs/forms/songmaintenanceform.py @@ -123,7 +123,8 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP """ self.authors_list_widget.clear() authors = self.manager.get_all_objects(Author) - authors.sort(key=lambda author: get_natural_key(author.display_name)) + get_author_key = lambda author: get_natural_key(author.display_name) + authors.sort(key=get_author_key) for author in authors: if author.display_name: author_name = QtWidgets.QListWidgetItem(author.display_name) @@ -138,7 +139,8 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP """ self.topics_list_widget.clear() topics = self.manager.get_all_objects(Topic) - topics.sort(key=lambda topic: get_natural_key(topic.name)) + get_topic_key = lambda topic: get_natural_key(topic.name) + topics.sort(key=get_topic_key) for topic in topics: topic_name = QtWidgets.QListWidgetItem(topic.name) topic_name.setData(QtCore.Qt.UserRole, topic.id) @@ -150,7 +152,8 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP """ self.song_books_list_widget.clear() books = self.manager.get_all_objects(Book) - books.sort(key=lambda book: get_natural_key(book.name)) + get_book_key = lambda book: get_natural_key(book.name) + books.sort(key=get_book_key) for book in books: book_name = QtWidgets.QListWidgetItem('%s (%s)' % (book.name, book.publisher)) book_name.setData(QtCore.Qt.UserRole, book.id) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 2c483a48a..c43a23781 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -261,7 +261,8 @@ class SongMediaItem(MediaManagerItem): log.debug('display results Song') self.save_auto_select_id() self.list_view.clear() - search_results.sort(key=lambda song: song.sort_key) + get_song_key = lambda song: song.sort_key + search_results.sort(key=get_song_key) for song in search_results: # Do not display temporary songs if song.temporary: @@ -285,9 +286,11 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Author') self.list_view.clear() - search_results.sort(key=lambda author: get_natural_key(author.display_name)) + get_author_key = lambda author: get_natural_key(author.display_name) + search_results.sort(key=get_author_key) for author in search_results: - author.songs.sort(key=lambda song: song.sort_key) + get_song_key = lambda song: song.sort_key + author.songs.sort(key=get_song_key) for song in author.songs: # Do not display temporary songs if song.temporary: @@ -306,8 +309,8 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Book') self.list_view.clear() - search_results.sort(key=lambda songbook_entry: - (get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry))) + get_songbook_key = lambda songbook_entry:(get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry)) + search_results.sort(key=get_songbook_key) for songbook_entry in search_results: # Do not display temporary songs if songbook_entry.song.temporary: @@ -326,9 +329,11 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Topic') self.list_view.clear() - search_results.sort(key=lambda topic: get_natural_key(topic.name)) + get_topic_key = lambda topic: get_natural_key(topic.name) + search_results.sort(key=get_topic_key) for topic in search_results: - topic.songs.sort(key=lambda song: song.sort_key) + get_song_key = lambda song: song.sort_key + topic.songs.sort(key=get_song_key) for song in topic.songs: # Do not display temporary songs if song.temporary: @@ -347,8 +352,8 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Themes') self.list_view.clear() - search_results.sort(key=lambda song: (get_natural_key(song.theme_name), - song.sort_key)) + get_theme_key = lambda song: (get_natural_key(song.theme_name), song.sort_key) + search_results.sort(key=get_theme_key) for song in search_results: # Do not display temporary songs if song.temporary: @@ -367,8 +372,8 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results CCLI number') self.list_view.clear() - search_results.sort(key=lambda song: (get_natural_key(song.ccli_number), - song.sort_key)) + get_cclinumber_key = lambda song: (get_natural_key(song.ccli_number), song.sort_key) + search_results.sort(key=get_cclinumber_key) for song in search_results: # Do not display temporary songs if song.temporary: From 4b088acc93134dd68b787e21b59b739978f7e78c Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Tue, 19 Apr 2016 20:32:23 +0100 Subject: [PATCH 08/10] Coding standards fix - E731 do not assign a lambda expression, use a def for pep8 --- openlp/plugins/songs/forms/editsongform.py | 6 +++--- openlp/plugins/songs/forms/songexportform.py | 2 +- .../plugins/songs/forms/songmaintenanceform.py | 6 +++--- openlp/plugins/songs/lib/mediaitem.py | 17 +++++++++-------- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index cfe183db6..dbc7a97da 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -112,7 +112,7 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): Generically load a set of objects into a cache and a combobox. """ objects = self.manager.get_all_objects(cls) - get_key = lambda object: get_natural_key(object.name) + def get_key(object): return get_natural_key(object.name) objects.sort(key=get_key) combo.clear() combo.addItem('') @@ -347,7 +347,7 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): Load the authors from the database into the combobox. """ authors = self.manager.get_all_objects(Author) - get_author_key = lambda author: get_natural_key(author.display_name) + def get_author_key(author): return get_natural_key(author.display_name) authors.sort(key=get_author_key) self.authors_combo_box.clear() self.authors_combo_box.addItem('') @@ -386,7 +386,7 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): self.theme_combo_box.clear() self.theme_combo_box.addItem('') self.themes = theme_list - get_theme_key = lambda theme: get_natural_key(theme) + def get_theme_key(theme): return get_natural_key(theme) self.themes.sort(key=get_theme_key) self.theme_combo_box.addItems(theme_list) set_case_insensitive_completer(self.themes, self.theme_combo_box) diff --git a/openlp/plugins/songs/forms/songexportform.py b/openlp/plugins/songs/forms/songexportform.py index fbf909552..fc4ed16c5 100644 --- a/openlp/plugins/songs/forms/songexportform.py +++ b/openlp/plugins/songs/forms/songexportform.py @@ -213,7 +213,7 @@ class SongExportForm(OpenLPWizard): # Load the list of songs. self.application.set_busy_cursor() songs = self.plugin.manager.get_all_objects(Song) - get_song_key = lambda song: song.sort_key + def get_song_key(song): return song.sort_key songs.sort(key=get_song_key) for song in songs: # No need to export temporary songs. diff --git a/openlp/plugins/songs/forms/songmaintenanceform.py b/openlp/plugins/songs/forms/songmaintenanceform.py index 52c84acbd..e949b77c8 100644 --- a/openlp/plugins/songs/forms/songmaintenanceform.py +++ b/openlp/plugins/songs/forms/songmaintenanceform.py @@ -123,7 +123,7 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP """ self.authors_list_widget.clear() authors = self.manager.get_all_objects(Author) - get_author_key = lambda author: get_natural_key(author.display_name) + def get_author_key(author): return get_natural_key(author.display_name) authors.sort(key=get_author_key) for author in authors: if author.display_name: @@ -139,7 +139,7 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP """ self.topics_list_widget.clear() topics = self.manager.get_all_objects(Topic) - get_topic_key = lambda topic: get_natural_key(topic.name) + def get_topic_key(topic): return get_natural_key(topic.name) topics.sort(key=get_topic_key) for topic in topics: topic_name = QtWidgets.QListWidgetItem(topic.name) @@ -152,7 +152,7 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP """ self.song_books_list_widget.clear() books = self.manager.get_all_objects(Book) - get_book_key = lambda book: get_natural_key(book.name) + def get_book_key(book): return get_natural_key(book.name) books.sort(key=get_book_key) for book in books: book_name = QtWidgets.QListWidgetItem('%s (%s)' % (book.name, book.publisher)) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index c43a23781..30b26ea24 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -261,7 +261,7 @@ class SongMediaItem(MediaManagerItem): log.debug('display results Song') self.save_auto_select_id() self.list_view.clear() - get_song_key = lambda song: song.sort_key + def get_song_key(song): return song.sort_key search_results.sort(key=get_song_key) for song in search_results: # Do not display temporary songs @@ -286,10 +286,10 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Author') self.list_view.clear() - get_author_key = lambda author: get_natural_key(author.display_name) + def get_author_key(author): return get_natural_key(author.display_name) search_results.sort(key=get_author_key) for author in search_results: - get_song_key = lambda song: song.sort_key + def get_song_key(song): return song.sort_key author.songs.sort(key=get_song_key) for song in author.songs: # Do not display temporary songs @@ -309,7 +309,8 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Book') self.list_view.clear() - get_songbook_key = lambda songbook_entry:(get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry)) + def get_songbook_key(songbook_entry): return (get_natural_key(songbook_entry.songbook.name), + get_natural_key(songbook_entry.entry)) search_results.sort(key=get_songbook_key) for songbook_entry in search_results: # Do not display temporary songs @@ -329,10 +330,10 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Topic') self.list_view.clear() - get_topic_key = lambda topic: get_natural_key(topic.name) + def get_topic_key(topic): return get_natural_key(topic.name) search_results.sort(key=get_topic_key) for topic in search_results: - get_song_key = lambda song: song.sort_key + def get_song_key(song): return song.sort_key topic.songs.sort(key=get_song_key) for song in topic.songs: # Do not display temporary songs @@ -352,7 +353,7 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Themes') self.list_view.clear() - get_theme_key = lambda song: (get_natural_key(song.theme_name), song.sort_key) + def get_theme_key(song): return (get_natural_key(song.theme_name), song.sort_key) search_results.sort(key=get_theme_key) for song in search_results: # Do not display temporary songs @@ -372,7 +373,7 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results CCLI number') self.list_view.clear() - get_cclinumber_key = lambda song: (get_natural_key(song.ccli_number), song.sort_key) + def get_cclinumber_key(song): return (get_natural_key(song.ccli_number), song.sort_key) search_results.sort(key=get_cclinumber_key) for song in search_results: # Do not display temporary songs From 1e9b7148f57e9eb5da750af9a16de79d80b37f9a Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Mon, 25 Apr 2016 11:53:20 +0100 Subject: [PATCH 09/10] Resolve pep8 errors - separate def, two line split --- openlp/plugins/songs/forms/editsongform.py | 15 +++++-- openlp/plugins/songs/forms/songexportform.py | 5 ++- .../songs/forms/songmaintenanceform.py | 15 +++++-- openlp/plugins/songs/lib/mediaitem.py | 41 +++++++++++++++---- 4 files changed, 60 insertions(+), 16 deletions(-) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index dbc7a97da..386380ec0 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -111,8 +111,11 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): """ Generically load a set of objects into a cache and a combobox. """ + def get_key(obj): + """Get the key to sort by""" + return get_natural_key(obj.name) + objects = self.manager.get_all_objects(cls) - def get_key(object): return get_natural_key(object.name) objects.sort(key=get_key) combo.clear() combo.addItem('') @@ -346,8 +349,11 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): """ Load the authors from the database into the combobox. """ + def get_author_key(author): + """Get the key to sort by""" + return get_natural_key(author.display_name) + authors = self.manager.get_all_objects(Author) - def get_author_key(author): return get_natural_key(author.display_name) authors.sort(key=get_author_key) self.authors_combo_box.clear() self.authors_combo_box.addItem('') @@ -383,10 +389,13 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): """ Load the themes into a combobox. """ + def get_theme_key(theme): + """Get the key to sort by""" + return get_natural_key(theme) + self.theme_combo_box.clear() self.theme_combo_box.addItem('') self.themes = theme_list - def get_theme_key(theme): return get_natural_key(theme) self.themes.sort(key=get_theme_key) self.theme_combo_box.addItems(theme_list) set_case_insensitive_completer(self.themes, self.theme_combo_box) diff --git a/openlp/plugins/songs/forms/songexportform.py b/openlp/plugins/songs/forms/songexportform.py index fc4ed16c5..80a6004a4 100644 --- a/openlp/plugins/songs/forms/songexportform.py +++ b/openlp/plugins/songs/forms/songexportform.py @@ -203,6 +203,10 @@ class SongExportForm(OpenLPWizard): """ Set default form values for the song export wizard. """ + def get_song_key(song): + """Get the key to sort by""" + return song.sort_key + self.restart() self.finish_button.setVisible(False) self.cancel_button.setVisible(True) @@ -213,7 +217,6 @@ class SongExportForm(OpenLPWizard): # Load the list of songs. self.application.set_busy_cursor() songs = self.plugin.manager.get_all_objects(Song) - def get_song_key(song): return song.sort_key songs.sort(key=get_song_key) for song in songs: # No need to export temporary songs. diff --git a/openlp/plugins/songs/forms/songmaintenanceform.py b/openlp/plugins/songs/forms/songmaintenanceform.py index e949b77c8..55e5031fe 100644 --- a/openlp/plugins/songs/forms/songmaintenanceform.py +++ b/openlp/plugins/songs/forms/songmaintenanceform.py @@ -121,9 +121,12 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP """ Reloads the Authors list. """ + def get_author_key(author): + """Get the key to sort by""" + return get_natural_key(author.display_name) + self.authors_list_widget.clear() authors = self.manager.get_all_objects(Author) - def get_author_key(author): return get_natural_key(author.display_name) authors.sort(key=get_author_key) for author in authors: if author.display_name: @@ -137,9 +140,12 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP """ Reloads the Topics list. """ + def get_topic_key(topic): + """Get the key to sort by""" + return get_natural_key(topic.name) + self.topics_list_widget.clear() topics = self.manager.get_all_objects(Topic) - def get_topic_key(topic): return get_natural_key(topic.name) topics.sort(key=get_topic_key) for topic in topics: topic_name = QtWidgets.QListWidgetItem(topic.name) @@ -150,9 +156,12 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP """ Reloads the Books list. """ + def get_book_key(book): + """Get the key to sort by""" + return get_natural_key(book.name) + self.song_books_list_widget.clear() books = self.manager.get_all_objects(Book) - def get_book_key(book): return get_natural_key(book.name) books.sort(key=get_book_key) for book in books: book_name = QtWidgets.QListWidgetItem('%s (%s)' % (book.name, book.publisher)) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 30b26ea24..0824a80b0 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -258,10 +258,13 @@ class SongMediaItem(MediaManagerItem): :param search_results: A list of db Song objects :return: None """ + def get_song_key(song): + """Get the key to sort by""" + return song.sort_key + log.debug('display results Song') self.save_auto_select_id() self.list_view.clear() - def get_song_key(song): return song.sort_key search_results.sort(key=get_song_key) for song in search_results: # Do not display temporary songs @@ -284,12 +287,18 @@ class SongMediaItem(MediaManagerItem): :param search_results: A list of db Author objects :return: None """ + def get_author_key(author): + """Get the key to sort by""" + return get_natural_key(author.display_name) + + def get_song_key(song): + """Get the key to sort by""" + return song.sort_key + log.debug('display results Author') self.list_view.clear() - def get_author_key(author): return get_natural_key(author.display_name) search_results.sort(key=get_author_key) for author in search_results: - def get_song_key(song): return song.sort_key author.songs.sort(key=get_song_key) for song in author.songs: # Do not display temporary songs @@ -307,10 +316,12 @@ class SongMediaItem(MediaManagerItem): :param search_results: A list of db SongBookEntry objects :return: None """ + def get_songbook_key(songbook_entry): + """Get the key to sort by""" + return (get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry)) + log.debug('display results Book') self.list_view.clear() - def get_songbook_key(songbook_entry): return (get_natural_key(songbook_entry.songbook.name), - get_natural_key(songbook_entry.entry)) search_results.sort(key=get_songbook_key) for songbook_entry in search_results: # Do not display temporary songs @@ -328,12 +339,18 @@ class SongMediaItem(MediaManagerItem): :param search_results: A list of db Topic objects :return: None """ + def get_topic_key(topic): + """Get the key to sort by""" + return get_natural_key(topic.name) + + def get_song_key(song): + """Get the key to sort by""" + return song.sort_key + log.debug('display results Topic') self.list_view.clear() - def get_topic_key(topic): return get_natural_key(topic.name) search_results.sort(key=get_topic_key) for topic in search_results: - def get_song_key(song): return song.sort_key topic.songs.sort(key=get_song_key) for song in topic.songs: # Do not display temporary songs @@ -351,9 +368,12 @@ class SongMediaItem(MediaManagerItem): :param search_results: A list of db Song objects :return: None """ + def get_theme_key(song): + """Get the key to sort by""" + return (get_natural_key(song.theme_name), song.sort_key) + log.debug('display results Themes') self.list_view.clear() - def get_theme_key(song): return (get_natural_key(song.theme_name), song.sort_key) search_results.sort(key=get_theme_key) for song in search_results: # Do not display temporary songs @@ -371,9 +391,12 @@ class SongMediaItem(MediaManagerItem): :param search_results: A list of db Song objects :return: None """ + def get_cclinumber_key(song): + """Get the key to sort by""" + return (get_natural_key(song.ccli_number), song.sort_key) + log.debug('display results CCLI number') self.list_view.clear() - def get_cclinumber_key(song): return (get_natural_key(song.ccli_number), song.sort_key) search_results.sort(key=get_cclinumber_key) for song in search_results: # Do not display temporary songs From a5cd1e15bcc857098385194e106177dc962c786a Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Mon, 25 Apr 2016 12:01:32 +0100 Subject: [PATCH 10/10] resolve pep8 W293 warning --- openlp/plugins/songs/forms/editsongform.py | 6 +++--- openlp/plugins/songs/forms/songexportform.py | 2 +- .../plugins/songs/forms/songmaintenanceform.py | 6 +++--- openlp/plugins/songs/lib/mediaitem.py | 16 ++++++++-------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index 386380ec0..b33788a4c 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -114,7 +114,7 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): def get_key(obj): """Get the key to sort by""" return get_natural_key(obj.name) - + objects = self.manager.get_all_objects(cls) objects.sort(key=get_key) combo.clear() @@ -352,7 +352,7 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): def get_author_key(author): """Get the key to sort by""" return get_natural_key(author.display_name) - + authors = self.manager.get_all_objects(Author) authors.sort(key=get_author_key) self.authors_combo_box.clear() @@ -392,7 +392,7 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties): def get_theme_key(theme): """Get the key to sort by""" return get_natural_key(theme) - + self.theme_combo_box.clear() self.theme_combo_box.addItem('') self.themes = theme_list diff --git a/openlp/plugins/songs/forms/songexportform.py b/openlp/plugins/songs/forms/songexportform.py index da49f2728..e8a559c44 100644 --- a/openlp/plugins/songs/forms/songexportform.py +++ b/openlp/plugins/songs/forms/songexportform.py @@ -206,7 +206,7 @@ class SongExportForm(OpenLPWizard): def get_song_key(song): """Get the key to sort by""" return song.sort_key - + self.restart() self.finish_button.setVisible(False) self.cancel_button.setVisible(True) diff --git a/openlp/plugins/songs/forms/songmaintenanceform.py b/openlp/plugins/songs/forms/songmaintenanceform.py index 55e5031fe..74462e6d0 100644 --- a/openlp/plugins/songs/forms/songmaintenanceform.py +++ b/openlp/plugins/songs/forms/songmaintenanceform.py @@ -124,7 +124,7 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP def get_author_key(author): """Get the key to sort by""" return get_natural_key(author.display_name) - + self.authors_list_widget.clear() authors = self.manager.get_all_objects(Author) authors.sort(key=get_author_key) @@ -143,7 +143,7 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP def get_topic_key(topic): """Get the key to sort by""" return get_natural_key(topic.name) - + self.topics_list_widget.clear() topics = self.manager.get_all_objects(Topic) topics.sort(key=get_topic_key) @@ -159,7 +159,7 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP def get_book_key(book): """Get the key to sort by""" return get_natural_key(book.name) - + self.song_books_list_widget.clear() books = self.manager.get_all_objects(Book) books.sort(key=get_book_key) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 0824a80b0..8edac2877 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -261,7 +261,7 @@ class SongMediaItem(MediaManagerItem): def get_song_key(song): """Get the key to sort by""" return song.sort_key - + log.debug('display results Song') self.save_auto_select_id() self.list_view.clear() @@ -290,11 +290,11 @@ class SongMediaItem(MediaManagerItem): def get_author_key(author): """Get the key to sort by""" return get_natural_key(author.display_name) - + def get_song_key(song): """Get the key to sort by""" return song.sort_key - + log.debug('display results Author') self.list_view.clear() search_results.sort(key=get_author_key) @@ -319,7 +319,7 @@ class SongMediaItem(MediaManagerItem): def get_songbook_key(songbook_entry): """Get the key to sort by""" return (get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry)) - + log.debug('display results Book') self.list_view.clear() search_results.sort(key=get_songbook_key) @@ -342,11 +342,11 @@ class SongMediaItem(MediaManagerItem): def get_topic_key(topic): """Get the key to sort by""" return get_natural_key(topic.name) - + def get_song_key(song): """Get the key to sort by""" return song.sort_key - + log.debug('display results Topic') self.list_view.clear() search_results.sort(key=get_topic_key) @@ -371,7 +371,7 @@ class SongMediaItem(MediaManagerItem): def get_theme_key(song): """Get the key to sort by""" return (get_natural_key(song.theme_name), song.sort_key) - + log.debug('display results Themes') self.list_view.clear() search_results.sort(key=get_theme_key) @@ -394,7 +394,7 @@ class SongMediaItem(MediaManagerItem): def get_cclinumber_key(song): """Get the key to sort by""" return (get_natural_key(song.ccli_number), song.sort_key) - + log.debug('display results CCLI number') self.list_view.clear() search_results.sort(key=get_cclinumber_key)