From cc8918d4702c12888f35b10bd2aae086b10e601d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 14 Oct 2019 07:49:51 +0530 Subject: [PATCH] Content server: When adding books and a duplicate is suspected provide more information about what books match the duplicate --- src/calibre/srv/cdb.py | 11 ++++++++--- src/pyj/book_list/add.pyj | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/calibre/srv/cdb.py b/src/calibre/srv/cdb.py index f407399452..841375355e 100644 --- a/src/calibre/srv/cdb.py +++ b/src/calibre/srv/cdb.py @@ -69,9 +69,12 @@ def cdb_add_book(ctx, rd, job_id, add_duplicates, filename, library_id): Add a file as a new book. The file contents must be in the body of the request. The response will also have the title/authors/languages read from the - metadata of the file/filename. It will contain a `book_id` field specifying the id of the newly added book, - or if add_duplicates is not specified and a duplicate was found, no book_id will be present. It will also - return the value of `job_id` as the `id` field and `filename` as the `filename` field. + metadata of the file/filename. It will contain a `book_id` field specifying + the id of the newly added book, or if add_duplicates is not specified and a + duplicate was found, no book_id will be present, instead there will be a + `duplicates` field specifying the title and authors for all duplicate + matches. It will also return the value of `job_id` as the `id` field and + `filename` as the `filename` field. ''' db = get_db(ctx, rd, library_id) if ctx.restriction_for(rd, db): @@ -96,6 +99,8 @@ def cdb_add_book(ctx, rd, job_id, add_duplicates, filename, library_id): if ids: ans['book_id'] = ids[0] ctx.notify_changes(db.backend.library_path, books_added(ids)) + else: + ans['duplicates'] = [{'title': m.title, 'authors': m.authors} for m, _ in duplicates] return ans diff --git a/src/pyj/book_list/add.pyj b/src/pyj/book_list/add.pyj index b9cad2c31d..8b10658884 100644 --- a/src/pyj/book_list/add.pyj +++ b/src/pyj/book_list/add.pyj @@ -71,7 +71,10 @@ def list_added_book(container, data): def list_duplicate_book(container, container_id, job_id, data, file): - container.appendChild(E.div(style="padding: 1rem 1ex", _('A book with the title "{0}" already exists in the library.').format(data.title))) + container.appendChild(E.div( + style="padding: 1rem 1ex", + _('A book with the title "{0}" already exists in the library.' + ).format(data.title))) b = create_button(_('Add anyway'), action=def(): c = get_job_container(container_id, job_id) clear(c) @@ -79,7 +82,20 @@ def list_duplicate_book(container, container_id, job_id, data, file): c.appendChild(w) send_file(file, container_id, job_id, True) ) + + def list_duplicates(): + ans = E.ol(style='margin-left: 2rem') + for item in data.duplicates: + ans.appendChild(E.li(_('{0} by {1}').format(item.title, ' & '.join(item.authors)))) + return ans + container.appendChild(b) + container.appendChild(E.div( + style='margin: 1rem 1ex; border-top: solid 1px currentColor; padding-top: 1ex', + _('The book you are trying to add is:'), E.span(' ', E.b(data.title), ' ', _('by'), ' ', E.i(' & '.join(data.authors))), + _('. Books already in the library with that title are:'), + E.div(style='padding-top:1ex', list_duplicates()) + )) def write_access_error(msg, xhr):