API to easily get books in a virtual library

Uses the search cache for maximum performance
This commit is contained in:
Kovid Goyal 2017-04-23 12:00:13 +05:30
parent ebeec06b0b
commit 303a5cca6a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 20 additions and 7 deletions

View File

@ -981,6 +981,14 @@ class Cache(object):
''' '''
return self._search_api(self, query, restriction, virtual_fields=virtual_fields, book_ids=book_ids) return self._search_api(self, query, restriction, virtual_fields=virtual_fields, book_ids=book_ids)
@read_api
def books_in_virtual_library(self, vl):
' Return the set of books in the specified virtual library '
vl = self._pref('virtual_libraries', {}).get(vl) if vl else None
if vl is None:
return self.all_book_ids()
return frozenset(self._search('', vl))
@api @api
def get_categories(self, sort='name', book_ids=None, already_fixed=None, def get_categories(self, sort='name', book_ids=None, already_fixed=None,
first_letter_sort=False): first_letter_sort=False):

View File

@ -483,13 +483,18 @@ class ReadingTest(BaseTest):
def test_restrictions(self): # {{{ def test_restrictions(self): # {{{
' Test searching with and without restrictions ' ' Test searching with and without restrictions '
cache = self.init_cache() cache = self.init_cache()
self.assertSetEqual(cache.all_book_ids(), cache.search('')) se = self.assertSetEqual
self.assertSetEqual({1, 2}, cache.search('', 'not authors:=Unknown')) se(cache.all_book_ids(), cache.search(''))
self.assertSetEqual(set(), cache.search('authors:=Unknown', 'not authors:=Unknown')) se({1, 2}, cache.search('', 'not authors:=Unknown'))
self.assertSetEqual({2}, cache.search('not authors:"=Author Two"', 'not authors:=Unknown')) se(set(), cache.search('authors:=Unknown', 'not authors:=Unknown'))
self.assertSetEqual({2}, cache.search('not authors:"=Author Two"', book_ids={1, 2})) se({2}, cache.search('not authors:"=Author Two"', 'not authors:=Unknown'))
self.assertSetEqual({2}, cache.search('not authors:"=Author Two"', 'not authors:=Unknown', book_ids={1,2,3})) se({2}, cache.search('not authors:"=Author Two"', book_ids={1, 2}))
self.assertSetEqual(set(), cache.search('authors:=Unknown', 'not authors:=Unknown', book_ids={1,2,3})) se({2}, cache.search('not authors:"=Author Two"', 'not authors:=Unknown', book_ids={1,2,3}))
se(set(), cache.search('authors:=Unknown', 'not authors:=Unknown', book_ids={1,2,3}))
se(cache.all_book_ids(), cache.books_in_virtual_library(''))
se(cache.all_book_ids(), cache.books_in_virtual_library('does not exist'))
cache.set_pref('virtual_libraries', {'1':'title:"=Title One"'})
se({2}, cache.books_in_virtual_library('1'))
# }}} # }}}
def test_search_caching(self): # {{{ def test_search_caching(self): # {{{