Prevent non unique authors on upload

This commit is contained in:
Ozzie Isaacs 2024-06-23 13:07:28 +02:00
parent d5a57e3b07
commit 6441f5f96b

View File

@ -616,8 +616,9 @@ def prepare_authors(authr):
input_authors = [_('Unknown')] # prevent empty Author input_authors = [_('Unknown')] # prevent empty Author
renamed = list() renamed = list()
for in_aut in input_authors: for index,in_aut in enumerate(input_authors):
renamed_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == in_aut).first() # renamed_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == in_aut).first()
renamed_author = calibre_db.session.query(db.Authors).filter(func.lower(db.Authors.name).ilike(in_aut)).first()
if renamed_author and in_aut != renamed_author.name: if renamed_author and in_aut != renamed_author.name:
renamed.append(renamed_author.name) renamed.append(renamed_author.name)
all_books = calibre_db.session.query(db.Books) \ all_books = calibre_db.session.query(db.Books) \
@ -626,6 +627,7 @@ def prepare_authors(authr):
sorted_old_author = helper.get_sorted_author(in_aut) sorted_old_author = helper.get_sorted_author(in_aut)
for one_book in all_books: for one_book in all_books:
one_book.author_sort = one_book.author_sort.replace(sorted_renamed_author, sorted_old_author) one_book.author_sort = one_book.author_sort.replace(sorted_renamed_author, sorted_old_author)
input_authors[index] = renamed_author.name
return input_authors, renamed return input_authors, renamed
@ -642,7 +644,8 @@ def prepare_authors_on_upload(title, authr):
sort_authors_list = list() sort_authors_list = list()
db_author = None db_author = None
for inp in input_authors: for inp in input_authors:
stored_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == inp).first() # stored_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == inp).first()
stored_author = calibre_db.session.query(db.Authors).filter(func.lower(db.Authors.name).ilike(inp)).first()
if not stored_author: if not stored_author:
if not db_author: if not db_author:
db_author = db.Authors(inp, helper.get_sorted_author(inp), "") db_author = db.Authors(inp, helper.get_sorted_author(inp), "")
@ -1389,8 +1392,8 @@ def add_objects(db_book_object, db_object, db_session, db_type, add_elements):
if db_no_case: if db_no_case:
# check for new case of element # check for new case of element
db_element = create_objects_for_addition(db_element, add_element, db_type) db_element = create_objects_for_addition(db_element, add_element, db_type)
else: #else:
db_element = create_objects_for_addition(db_element, add_element, db_type) # db_element = create_objects_for_addition(db_element, add_element, db_type)
# add element to book # add element to book
db_book_object.append(db_element) db_book_object.append(db_element)