mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
DB: Ensure that set_metadata() sets author_sort to a correct value if the Metadata object has authors but no author_sort. Fixes #1116 (Generate an author_sort in fetch-ebook-metadata output)
This commit is contained in:
parent
b393487446
commit
0ada2ad42b
@ -26,7 +26,7 @@ from calibre.db.tables import VirtualTable
|
|||||||
from calibre.db.write import get_series_values, uniq
|
from calibre.db.write import get_series_values, uniq
|
||||||
from calibre.db.lazy import FormatMetadata, FormatsList, ProxyMetadata
|
from calibre.db.lazy import FormatMetadata, FormatsList, ProxyMetadata
|
||||||
from calibre.ebooks import check_ebook_format
|
from calibre.ebooks import check_ebook_format
|
||||||
from calibre.ebooks.metadata import string_to_authors, author_to_author_sort
|
from calibre.ebooks.metadata import string_to_authors, author_to_author_sort, authors_to_sort_string
|
||||||
from calibre.ebooks.metadata.book.base import Metadata
|
from calibre.ebooks.metadata.book.base import Metadata
|
||||||
from calibre.ebooks.metadata.opf2 import metadata_to_opf
|
from calibre.ebooks.metadata.opf2 import metadata_to_opf
|
||||||
from calibre.ptempfile import (base_dir, PersistentTemporaryFile,
|
from calibre.ptempfile import (base_dir, PersistentTemporaryFile,
|
||||||
@ -1297,6 +1297,7 @@ class Cache(object):
|
|||||||
if set_title and mi.title:
|
if set_title and mi.title:
|
||||||
path_changed = True
|
path_changed = True
|
||||||
set_field('title', mi.title)
|
set_field('title', mi.title)
|
||||||
|
authors_changed = False
|
||||||
if set_authors:
|
if set_authors:
|
||||||
path_changed = True
|
path_changed = True
|
||||||
if not mi.authors:
|
if not mi.authors:
|
||||||
@ -1305,6 +1306,7 @@ class Cache(object):
|
|||||||
for a in mi.authors:
|
for a in mi.authors:
|
||||||
authors += string_to_authors(a)
|
authors += string_to_authors(a)
|
||||||
set_field('authors', authors)
|
set_field('authors', authors)
|
||||||
|
authors_changed = True
|
||||||
|
|
||||||
if path_changed:
|
if path_changed:
|
||||||
self._update_path({book_id})
|
self._update_path({book_id})
|
||||||
@ -1339,7 +1341,13 @@ class Cache(object):
|
|||||||
if val is not None:
|
if val is not None:
|
||||||
protected_set_field(field, val)
|
protected_set_field(field, val)
|
||||||
|
|
||||||
for field in ('author_sort', 'publisher', 'series', 'tags', 'comments',
|
val = mi.get('author_sort', None)
|
||||||
|
if authors_changed and (not val or mi.is_null('author_sort')):
|
||||||
|
val = authors_to_sort_string(mi.authors)
|
||||||
|
if authors_changed or (force_changes and val is not None) or not mi.is_null('author_sort'):
|
||||||
|
protected_set_field('author_sort', val)
|
||||||
|
|
||||||
|
for field in ('publisher', 'series', 'tags', 'comments',
|
||||||
'languages', 'pubdate'):
|
'languages', 'pubdate'):
|
||||||
val = mi.get(field, None)
|
val = mi.get(field, None)
|
||||||
if (force_changes and val is not None) or not mi.is_null(field):
|
if (force_changes and val is not None) or not mi.is_null(field):
|
||||||
|
@ -11,6 +11,7 @@ from functools import partial
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
from calibre.ebooks.metadata import author_to_author_sort, title_sort
|
from calibre.ebooks.metadata import author_to_author_sort, title_sort
|
||||||
|
from calibre.ebooks.metadata.book.base import Metadata
|
||||||
from calibre.utils.date import UNDEFINED_DATE
|
from calibre.utils.date import UNDEFINED_DATE
|
||||||
from calibre.db.tests.base import BaseTest, IMG
|
from calibre.db.tests.base import BaseTest, IMG
|
||||||
from polyglot.builtins import iteritems, itervalues, unicode_type
|
from polyglot.builtins import iteritems, itervalues, unicode_type
|
||||||
@ -436,6 +437,12 @@ class WritingTest(BaseTest):
|
|||||||
cache.set_metadata(3, mi)
|
cache.set_metadata(3, mi)
|
||||||
self.assertEqual(set(otags), set(cache.field_for('tags', 3)), 'case changes should not be allowed in set_metadata')
|
self.assertEqual(set(otags), set(cache.field_for('tags', 3)), 'case changes should not be allowed in set_metadata')
|
||||||
|
|
||||||
|
# test that setting authors without author sort results in an
|
||||||
|
# auto-generated authors sort
|
||||||
|
mi = Metadata('empty', ['a1', 'a2'])
|
||||||
|
cache.set_metadata(1, mi)
|
||||||
|
self.assertEqual('a1 & a2', cache.field_for('author_sort', 1))
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
def test_conversion_options(self): # {{{
|
def test_conversion_options(self): # {{{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user