Tentative {virtual_libraries} template support

This should allow the use of {virtual_libraries} in custom column
templates, with good performance, thanks to search caching.
This commit is contained in:
Kovid Goyal 2013-07-24 11:35:48 +05:30
parent 70f1dbb832
commit cbf2bb0c4e
2 changed files with 22 additions and 0 deletions

View File

@ -1620,6 +1620,18 @@ class Cache(object):
if cover and os.path.exists(cover):
self._set_field('cover', {book_id:1})
self.backend.restore_book(book_id, path, formats)
@read_api
def virtual_libraries_for_books(self, book_ids):
libraries = tuple(self._pref('virtual_libraries', {}).iterkeys())
ans = {book_id:[] for book_id in book_ids}
for lib in libraries:
books = self._search(lib) # We deliberately dont use book_ids as we want to use the search cache
for book in book_ids:
if book in books:
ans[book].append(lib)
return {k:tuple(sorted(v, key=sort_key)) for k, v in book_ids}
# }}}
class SortKey(object): # {{{

View File

@ -231,6 +231,15 @@ def composite_getter(mi, field, metadata, book_id, cache, formatter, template_ca
template_cache=template_cache).strip()
return ret
def virtual_libraries_getter(dbref, book_id, cache):
try:
return cache[field]
except KeyError:
db = dbref()
vls = db.virtual_libraries_for_books((book_id,))[book_id]
ret = cache[field] = ', '.join(vls)
return ret
getters = {
'title':simple_getter('title', _('Unknown')),
'title_sort':simple_getter('sort', _('Unknown')),
@ -247,6 +256,7 @@ getters = {
'series_index':series_index_getter(),
'application_id':lambda x, book_id, y: book_id,
'id':lambda x, book_id, y: book_id,
'virtual_libraries':virtual_libraries_getter,
}
for field in ('comments', 'publisher', 'identifiers', 'series', 'rating'):