Refactor the author data api for multiple authors

This commit is contained in:
Kovid Goyal 2013-07-12 14:25:39 +05:30
parent abf3bed75b
commit 9249fd8a3e
3 changed files with 13 additions and 12 deletions

View File

@ -161,7 +161,8 @@ class Cache(object):
def _get_metadata(self, book_id, get_user_categories=True): # {{{
mi = Metadata(None, template_cache=self.formatter_template_cache)
author_ids = self._field_ids_for('authors', book_id)
aut_list = [self._author_data(i) for i in author_ids]
adata = self._author_data(author_ids)
aut_list = [adata[i] for i in author_ids]
aum = []
aus = {}
aul = {}
@ -388,17 +389,17 @@ class Cache(object):
raise ValueError('%s is not a many-one or many-many field' % field)
@read_api
def author_data(self, author_id):
def author_data(self, author_ids):
'''
Return author data as a dictionary with keys: name, sort, link
If no author with the specified id is found an empty dictionary is
returned.
If no authors with the specified ids are found an empty dictionary is
returned. If author_ids is None, data for all authors is returned.
'''
try:
return self.fields['authors'].author_data(author_id)
except (KeyError, IndexError):
return {}
af = self.fields['authors']
if author_ids is None:
author_ids = tuple(af.table.id_map)
return {aid:af.author_data(aid) for aid in author_ids if aid in af.table.id_map}
@read_api
def format_metadata(self, book_id, fmt, allow_cache=True):

View File

@ -243,6 +243,8 @@ class LegacyTest(BaseTest):
'_set_title', '_set_custom', '_update_author_in_cache',
# Feeds are now stored in the config folder
'get_feeds', 'get_feed', 'update_feed', 'remove_feeds', 'add_feed', 'set_feeds',
# Obsolete/broken methods
'author_id', # replaced by get_author_id
}
SKIP_ARGSPEC = {
'__init__', 'get_next_series_num_for', 'has_book', 'author_sort_from_authors', 'all_tags',

View File

@ -189,10 +189,8 @@ class View(object):
id_ = idx if index_is_id else self.index_to_id(idx)
with self.cache.read_lock:
ids = self.cache._field_ids_for('authors', id_)
ans = []
for id_ in ids:
data = self.cache._author_data(id_)
ans.append(':::'.join((data['name'], data['sort'], data['link'])))
adata = self.cache._author_data(ids)
ans = [':::'.join((adata[aid]['name'], adata[aid]['sort'], adata[aid]['link'])) for aid in ids if aid in adata]
return ':#:'.join(ans) if ans else default_value
def multisort(self, fields=[], subsort=False, only_ids=None):