mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
...
This commit is contained in:
parent
8355955ef5
commit
09876260ca
@ -120,10 +120,9 @@ class Cache(object):
|
|||||||
mi.ondevice_col = self._field_for('ondevice', book_id, default_value='')
|
mi.ondevice_col = self._field_for('ondevice', book_id, default_value='')
|
||||||
mi.last_modified = self._field_for('last_modified', book_id,
|
mi.last_modified = self._field_for('last_modified', book_id,
|
||||||
default_value=n)
|
default_value=n)
|
||||||
formats = self._field_for('formats', book_id, default_value=())
|
formats = self._field_for('formats', book_id)
|
||||||
mi.format_metadata = {}
|
mi.format_metadata = {}
|
||||||
mi.languages = list(self._field_for('languages', book_id,
|
mi.languages = list(self._field_for('languages', book_id))
|
||||||
default_value=()))
|
|
||||||
if not formats:
|
if not formats:
|
||||||
good_formats = None
|
good_formats = None
|
||||||
else:
|
else:
|
||||||
@ -208,16 +207,13 @@ class Cache(object):
|
|||||||
``book_id``. If no such book exists or it has no defined value for the
|
``book_id``. If no such book exists or it has no defined value for the
|
||||||
field ``name`` or no such field exists, then ``default_value`` is returned.
|
field ``name`` or no such field exists, then ``default_value`` is returned.
|
||||||
|
|
||||||
default_values is not used for title, title_sort, authors, author_sort
|
default_value is not used for title, title_sort, authors, author_sort
|
||||||
and series_index. This is because these always have values in the db.
|
and series_index. This is because these always have values in the db.
|
||||||
default_value is used for all custom columns.
|
default_value is used for all custom columns.
|
||||||
|
|
||||||
The returned value for is_multiple fields are always tuples, unless
|
The returned value for is_multiple fields are always tuples, even when
|
||||||
default_value is returned.
|
no values are found (in other words, default_value is ignored). The
|
||||||
|
exception is identifiers for which the returned value is always a dict.
|
||||||
WARNING: When returning the value for a is_multiple custom field this
|
|
||||||
method returns None (the default_value) if no value is set. The
|
|
||||||
get_custom() method from the old interface returned []
|
|
||||||
|
|
||||||
WARNING: For is_multiple fields this method returns tuples, the old
|
WARNING: For is_multiple fields this method returns tuples, the old
|
||||||
interface generally returned lists.
|
interface generally returned lists.
|
||||||
@ -230,7 +226,13 @@ class Cache(object):
|
|||||||
return self.composite_for(name, book_id,
|
return self.composite_for(name, book_id,
|
||||||
default_value=default_value)
|
default_value=default_value)
|
||||||
try:
|
try:
|
||||||
return self.fields[name].for_book(book_id, default_value=default_value)
|
field = self.fields[name]
|
||||||
|
except KeyError:
|
||||||
|
return default_value
|
||||||
|
if field.is_multiple:
|
||||||
|
default_value = {} if name == 'identifiers' else ()
|
||||||
|
try:
|
||||||
|
return field.for_book(book_id, default_value=default_value)
|
||||||
except (KeyError, IndexError):
|
except (KeyError, IndexError):
|
||||||
return default_value
|
return default_value
|
||||||
|
|
||||||
|
@ -33,6 +33,8 @@ class Field(object):
|
|||||||
self._default_sort_key = UNDEFINED_DATE
|
self._default_sort_key = UNDEFINED_DATE
|
||||||
if self.name == 'languages':
|
if self.name == 'languages':
|
||||||
self._sort_key = lambda x:sort_key(calibre_langcode_to_name(x))
|
self._sort_key = lambda x:sort_key(calibre_langcode_to_name(x))
|
||||||
|
self.is_multiple = (bool(self.metadata['is_multiple']) or self.name ==
|
||||||
|
'formats')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def metadata(self):
|
def metadata(self):
|
||||||
@ -141,6 +143,7 @@ class OnDeviceField(OneToOneField):
|
|||||||
def __init__(self, name, table):
|
def __init__(self, name, table):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.book_on_device_func = None
|
self.book_on_device_func = None
|
||||||
|
self.is_multiple = False
|
||||||
|
|
||||||
def book_on_device(self, book_id):
|
def book_on_device(self, book_id):
|
||||||
if callable(self.book_on_device_func):
|
if callable(self.book_on_device_func):
|
||||||
|
@ -34,9 +34,9 @@ class ReadingTest(BaseTest):
|
|||||||
'series' : None,
|
'series' : None,
|
||||||
'series_index': 1.0,
|
'series_index': 1.0,
|
||||||
'rating': None,
|
'rating': None,
|
||||||
'tags': None,
|
'tags': (),
|
||||||
'formats':None,
|
'formats':(),
|
||||||
'identifiers': None,
|
'identifiers': {},
|
||||||
'timestamp': datetime.datetime(2011, 9, 7, 13, 54, 41,
|
'timestamp': datetime.datetime(2011, 9, 7, 13, 54, 41,
|
||||||
tzinfo=local_tz),
|
tzinfo=local_tz),
|
||||||
'pubdate': datetime.datetime(2011, 9, 7, 13, 54, 41,
|
'pubdate': datetime.datetime(2011, 9, 7, 13, 54, 41,
|
||||||
@ -44,15 +44,15 @@ class ReadingTest(BaseTest):
|
|||||||
'last_modified': datetime.datetime(2011, 9, 7, 13, 54, 41,
|
'last_modified': datetime.datetime(2011, 9, 7, 13, 54, 41,
|
||||||
tzinfo=local_tz),
|
tzinfo=local_tz),
|
||||||
'publisher': None,
|
'publisher': None,
|
||||||
'languages': None,
|
'languages': (),
|
||||||
'comments': None,
|
'comments': None,
|
||||||
'#enum': None,
|
'#enum': None,
|
||||||
'#authors':None,
|
'#authors':(),
|
||||||
'#date':None,
|
'#date':None,
|
||||||
'#rating':None,
|
'#rating':None,
|
||||||
'#series':None,
|
'#series':None,
|
||||||
'#series_index': None,
|
'#series_index': None,
|
||||||
'#tags':None,
|
'#tags':(),
|
||||||
'#yesno':None,
|
'#yesno':None,
|
||||||
'#comments': None,
|
'#comments': None,
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ class ReadingTest(BaseTest):
|
|||||||
'series' : 'Series One',
|
'series' : 'Series One',
|
||||||
'series_index': 1.0,
|
'series_index': 1.0,
|
||||||
'tags':('Tag Two', 'Tag One'),
|
'tags':('Tag Two', 'Tag One'),
|
||||||
'formats': None,
|
'formats': (),
|
||||||
'rating': 4.0,
|
'rating': 4.0,
|
||||||
'identifiers': {'test':'one'},
|
'identifiers': {'test':'one'},
|
||||||
'timestamp': datetime.datetime(2011, 9, 5, 15, 6,
|
'timestamp': datetime.datetime(2011, 9, 5, 15, 6,
|
||||||
@ -96,7 +96,7 @@ class ReadingTest(BaseTest):
|
|||||||
'series_index': 2.0,
|
'series_index': 2.0,
|
||||||
'rating': 6.0,
|
'rating': 6.0,
|
||||||
'tags': ('Tag One',),
|
'tags': ('Tag One',),
|
||||||
'formats':None,
|
'formats':(),
|
||||||
'identifiers': {'test':'two'},
|
'identifiers': {'test':'two'},
|
||||||
'timestamp': datetime.datetime(2011, 9, 6, 0, 0,
|
'timestamp': datetime.datetime(2011, 9, 6, 0, 0,
|
||||||
tzinfo=local_tz),
|
tzinfo=local_tz),
|
||||||
@ -120,8 +120,10 @@ class ReadingTest(BaseTest):
|
|||||||
}
|
}
|
||||||
for book_id, test in tests.iteritems():
|
for book_id, test in tests.iteritems():
|
||||||
for field, expected_val in test.iteritems():
|
for field, expected_val in test.iteritems():
|
||||||
self.assertEqual(expected_val,
|
val = cache.field_for(field, book_id)
|
||||||
cache.field_for(field, book_id))
|
self.assertEqual(expected_val, val,
|
||||||
|
'Book id: %d Field: %s failed: %r != %r'%(
|
||||||
|
book_id, field, expected_val, val))
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
def test_sorting(self): # {{{
|
def test_sorting(self): # {{{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user