CC get API

This commit is contained in:
Kovid Goyal 2013-07-17 09:36:12 +05:30
parent 1dc169db4c
commit 6c1bcc6503
5 changed files with 55 additions and 3 deletions

View File

@ -792,6 +792,11 @@ class DB(object):
return self.field_metadata.custom_field_prefix + label
return self.field_metadata.custom_field_prefix + self.custom_column_num_to_label_map[num]
def custom_field_metadata(self, label=None, num=None):
if label is not None:
return self.custom_column_label_map[label]
return self.custom_column_num_map[num]
def close(self):
if self._conn is not None:
self._conn.close()

View File

@ -325,7 +325,11 @@ class ManyToManyField(Field):
def for_book(self, book_id, default_value=None):
ids = self.table.book_col_map.get(book_id, ())
if ids:
ans = tuple(self.table.id_map[i] for i in ids)
ans = (self.table.id_map[i] for i in ids)
if self.table.sort_alpha:
ans = tuple(sorted(ans, key=sort_key))
else:
ans = tuple(ans)
else:
ans = default_value
return ans

View File

@ -539,6 +539,41 @@ class LibraryDatabase(object):
def rename_series(self, old_id, new_name, change_index=True):
self.new_api.rename_items('series', {old_id:new_name}, change_index=change_index)
def get_custom(self, index, label=None, num=None, index_is_id=False):
book_id = index if index_is_id else self.id(index)
ans = self.new_api.field_for(self.custom_field_name(label, num), book_id)
if isinstance(ans, tuple):
ans = list(ans)
return ans
def get_custom_extra(self, index, label=None, num=None, index_is_id=False):
data = self.backend.custom_field_metadata(label, num)
# add future datatypes with an extra column here
if data['datatype'] != 'series':
return None
book_id = index if index_is_id else self.id(index)
return self.new_api.field_for(self.custom_field_name(label, num) + '_index', book_id)
def get_custom_and_extra(self, index, label=None, num=None, index_is_id=False):
book_id = index if index_is_id else self.id(index)
data = self.backend.custom_field_metadata(label, num)
ans = self.new_api.field_for(self.custom_field_name(label, num), book_id)
if isinstance(ans, tuple):
ans = list(ans)
if data['datatype'] != 'series':
return (ans, None)
return (ans, self.new_api.field_for(self.custom_field_name(label, num) + '_index', book_id))
def get_next_cc_series_num_for(self, series, label=None, num=None):
data = self.backend.custom_field_metadata(label, num)
if data['datatype'] != 'series':
return None
return self.new_api.get_next_series_num_for(series, field=self.custom_field_name(label, num))
def is_item_used_in_multiple(self, item, label=None, num=None):
existing_tags = self.all_custom(label=label, num=num)
return icu_lower(item) in {icu_lower(t) for t in existing_tags}
# Private interface {{{
def __iter__(self):
for row in self.data.iterall():

View File

@ -44,6 +44,7 @@ class Table(object):
def __init__(self, name, metadata, link_table=None):
self.name, self.metadata = name, metadata
self.sort_alpha = metadata.get('is_multiple', False) and metadata.get('display', {}).get('sort_alpha', False)
# self.unserialize() maps values from the db to python objects
self.unserialize = \

View File

@ -612,9 +612,16 @@ class LegacyTest(BaseTest):
ndb = self.init_legacy(self.cloned_library)
db = self.init_old(self.cloned_library)
run_funcs(self, db, ndb, (
('all_custom', 'series'), ('all_custom', 'tags'), ('all_custom', 'rating'), ('all_custom', 'authors'),
('all_custom', 'series'), ('all_custom', 'tags'), ('all_custom', 'rating'), ('all_custom', 'authors'), ('all_custom', None, 7),
('get_next_cc_series_num_for', 'My Series One', 'series'), ('get_next_cc_series_num_for', 'My Series Two', 'series'),
('is_item_used_in_multiple', 'My Tag One', 'tags'),
('is_item_used_in_multiple', 'My Series One', 'series'),
('$get_custom_items_with_ids', 'series'), ('$get_custom_items_with_ids', 'tags'), ('$get_custom_items_with_ids', 'float'),
('$get_custom_items_with_ids', 'rating'), ('$get_custom_items_with_ids', 'authors'),
('$get_custom_items_with_ids', 'rating'), ('$get_custom_items_with_ids', 'authors'), ('$get_custom_items_with_ids', None, 7),
))
for label in ('tags', 'series', 'authors', 'comments', 'rating', 'date', 'yesno', 'isbn', 'enum', 'formats', 'float', 'comp_tags'):
for func in ('get_custom', 'get_custom_extra', 'get_custom_and_extra'):
run_funcs(self, db, ndb, [(func, idx, label) for idx in range(3)])
db.close()
# }}}