mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
When adding a book import its link map for any items that dont have an existing link
This commit is contained in:
parent
8447e8791f
commit
866c15f233
@ -1989,6 +1989,11 @@ class Cache:
|
||||
if cover is not None:
|
||||
mi.cover, mi.cover_data = None, (None, cover)
|
||||
self._set_metadata(book_id, mi, ignore_errors=True)
|
||||
lm = getattr(mi, 'link_maps', None)
|
||||
if lm:
|
||||
for field, link_map in lm.items():
|
||||
if self._has_link_map(field):
|
||||
self._set_link_map(field, link_map, only_set_if_no_existing_link=True)
|
||||
if preserve_uuid and mi.uuid:
|
||||
self._set_field('uuid', {book_id:mi.uuid})
|
||||
# Update the caches for fields from the books table
|
||||
@ -2397,7 +2402,7 @@ class Cache:
|
||||
return links
|
||||
|
||||
@write_api
|
||||
def set_link_map(self, field, value_to_link_map):
|
||||
def set_link_map(self, field, value_to_link_map, only_set_if_no_existing_link=False):
|
||||
'''
|
||||
Sets links for item values in field
|
||||
|
||||
@ -2418,7 +2423,11 @@ class Cache:
|
||||
self.link_maps_cache = {}
|
||||
|
||||
fids = self._get_item_ids(field, value_to_link_map)
|
||||
id_to_link_map = {fid:value_to_link_map[k] for k, fid in fids.items() if fid is not None}
|
||||
if only_set_if_no_existing_link:
|
||||
lm = table.link_map
|
||||
id_to_link_map = {fid:value_to_link_map[k] for k, fid in fids.items() if fid is not None and not lm.get(fid)}
|
||||
else:
|
||||
id_to_link_map = {fid:value_to_link_map[k] for k, fid in fids.items() if fid is not None}
|
||||
result_map = table.set_links(id_to_link_map, self.backend)
|
||||
changed_books = set()
|
||||
for id_ in result_map:
|
||||
|
@ -148,7 +148,10 @@ class AddRemoveTest(BaseTest):
|
||||
'Test the creation of new book entries'
|
||||
from calibre.ebooks.metadata.book.base import Metadata
|
||||
cache = self.init_cache()
|
||||
cache.set_field('authors', {1: 'Creator Two'})
|
||||
cache.set_link_map('authors', {'Creator Two': 'original'})
|
||||
mi = Metadata('Created One', authors=('Creator One', 'Creator Two'))
|
||||
mi.link_maps = {'authors': {'Creator One': 'link1', 'Creator Two': 'changed'}}
|
||||
|
||||
book_id = cache.create_book_entry(mi)
|
||||
self.assertIsNot(book_id, None)
|
||||
@ -163,6 +166,7 @@ class AddRemoveTest(BaseTest):
|
||||
self.assertEqual(('Created One', ('Creator One', 'Creator Two')), (cache.field_for('title', book_id), cache.field_for('authors', book_id)))
|
||||
self.assertEqual(cache.field_for('series_index', book_id), 1.0)
|
||||
self.assertEqual(cache.field_for('pubdate', book_id), UNDEFINED_DATE)
|
||||
self.assertEqual(cache.get_all_link_maps_for_book(book_id), {'authors': {'Creator One': 'link1', 'Creator Two': 'original'}})
|
||||
|
||||
do_test(cache, book_id)
|
||||
# Test that the db contains correct data
|
||||
|
Loading…
x
Reference in New Issue
Block a user