diff --git a/src/calibre/db/legacy.py b/src/calibre/db/legacy.py index f8faed4290..0ab29d2e3d 100644 --- a/src/calibre/db/legacy.py +++ b/src/calibre/db/legacy.py @@ -312,6 +312,25 @@ class LibraryDatabase(object): mi = self.new_api.get_metadata(book_id, get_cover=key == 'cover') return mi.get(key, default) + def authors_sort_strings(self, index, index_is_id=False): + book_id = index if index_is_id else self.data.index_to_id(index) + with self.new_api.read_lock: + af = self.new_api.fields['authors'].table + authors = af.book_col_map.get(book_id, ()) + adata = self.new_api._author_data(authors) + return [adata[aid]['sort'] for aid in authors] + + def author_sort_from_book(self, index, index_is_id=False): + return ' & '.join(self.authors_sort_strings(index, index_is_id=index_is_id)) + + def authors_with_sort_strings(self, index, index_is_id=False): + book_id = index if index_is_id else self.data.index_to_id(index) + with self.new_api.read_lock: + af = self.new_api.fields['authors'].table + authors = af.book_col_map.get(book_id, ()) + adata = self.new_api._author_data(authors) + return [(aid, adata[aid]['name'], adata[aid]['sort'], adata[aid]['link']) for aid in authors] + # Private interface {{{ def __iter__(self): diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index 5df09ec065..fa76439387 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -172,6 +172,9 @@ class LegacyTest(BaseTest): 'tag_name':[(3,)], 'author_name':[(3,)], 'series_name':[(3,)], + 'authors_sort_strings':[(0,), (1,), (2,)], + 'author_sort_from_book':[(0,), (1,), (2,)], + 'authors_with_sort_strings':[(0,), (1,), (2,)], }.iteritems(): for a in args: fmt = lambda x: x