newdb: Fix temp duplicate catalog entries in book list

Creating a catalog with an already existing catalog in the library would
cause a temporary duplicate entry in the book list (the entry would go
away on restart. Also fix the author sort for catalogs generated in the
AZW3 format not being correct.
This commit is contained in:
Kovid Goyal 2013-08-27 09:38:37 +05:30
parent 40cf046a07
commit 4751aa1286
2 changed files with 6 additions and 3 deletions

View File

@ -106,6 +106,7 @@ def add_catalog(cache, path, title):
from calibre.utils.date import utcnow
fmt = os.path.splitext(path)[1][1:].lower()
new_book_added = False
with lopen(path, 'rb') as stream:
with cache.write_lock:
matches = cache._search('title:="%s" and tags:="%s"' % (title.replace('"', '\\"'), _('Catalog')), None)
@ -118,17 +119,19 @@ def add_catalog(cache, path, title):
except:
mi = Metadata(title, ['calibre'])
mi.title, mi.authors = title, ['calibre']
mi.author_sort = 'calibre' # The MOBI/AZW3 format sets author sort to date
mi.tags = [_('Catalog')]
mi.pubdate = mi.timestamp = utcnow()
if fmt == 'mobi':
mi.cover, mi.cover_data = None, (None, None)
if db_id is None:
db_id = cache._create_book_entry(mi, apply_import_tags=False)
new_book_added = True
else:
cache._set_metadata(db_id, mi)
cache.add_format(db_id, fmt, stream) # Cant keep write lock since post-import hooks might run
return db_id
return db_id, new_book_added
def add_news(cache, path, arg):
from calibre.ebooks.metadata.meta import get_metadata

View File

@ -252,8 +252,8 @@ class LibraryDatabase(object):
return recursive_import(self, root, single_book_per_directory=single_book_per_directory, callback=callback, added_ids=added_ids)
def add_catalog(self, path, title):
book_id = add_catalog(self.new_api, path, title)
if book_id is not None:
book_id, new_book_added = add_catalog(self.new_api, path, title)
if book_id is not None and new_book_added:
self.data.books_added((book_id,))
return book_id