Content server: Fix changing the language of a book not working for non-English user interface language and for books that have no existing language. Fixes #2085005 [Calibre server fails updating book with Spanish characters](https://bugs.launchpad.net/calibre/+bug/2085005)

This commit is contained in:
Kovid Goyal 2024-10-20 15:32:00 +05:30
parent bc1986121f
commit babc0b9e48
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 15 additions and 1 deletions

View File

@ -17,6 +17,7 @@ from calibre.srv.metadata import book_as_json
from calibre.srv.routes import endpoint, json, msgpack_or_json
from calibre.srv.utils import get_db, get_library_data
from calibre.utils.imghdr import what
from calibre.utils.localization import canonicalize_lang, reverse_lang_map_for_ui
from calibre.utils.serialize import MSGPACK_MIME, json_loads, msgpack_loads
from calibre.utils.speedups import ReadOnlyFileBuffer
from polyglot.binary import from_base64_bytes
@ -206,6 +207,11 @@ def cdb_set_fields(ctx, rd, book_id, library_id):
dirtied.add(book_id)
for field, value in iteritems(changes):
if field == 'languages' and value:
rmap = reverse_lang_map_for_ui()
def to_lang_code(x):
return rmap.get(x, canonicalize_lang(x))
value = list(filter(None, map(to_lang_code, value)))
dirtied |= db.set_field(field, {book_id: value})
ctx.notify_changes(db.backend.library_path, metadata(dirtied))
all_ids = dirtied if all_dirtied else (dirtied & loaded_book_ids)

View File

@ -505,6 +505,13 @@ def lang_map_for_ui():
return ans
def reverse_lang_map_for_ui():
ans = getattr(reverse_lang_map_for_ui, 'ans', None)
if ans is None:
ans = reverse_lang_map_for_ui.ans = {v: k for k, v in lang_map_for_ui().items()}
return ans
def langnames_to_langcodes(names):
'''
Given a list of localized language names return a mapping of the names to 3

View File

@ -367,7 +367,8 @@ def multiple_line_edit(list_to_ui, ui_to_list, container_id, book_id, field, fm,
)
val = (resolved_metadata(mi, field) or v'[]')
if field is 'languages':
val = [mi.lang_names[l] or l for l in val]
ln = mi.lang_names or {}
val = [ln[l] or l for l in val]
if list_to_ui:
val = val.join(list_to_ui)
le.value = val