Fix deletion of books with multiple custom column links

Fix delete of author-folder in case last book of author was deleted (#1269)
pull/1335/head
Ozzieisaacs 5 years ago
parent d9dde36c74
commit bc89b0658a

@ -154,47 +154,59 @@ def delete_book(book_id, book_format):
if current_user.role_delete_books(): if current_user.role_delete_books():
book = db.session.query(db.Books).filter(db.Books.id == book_id).first() book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
if book: if book:
helper.delete_book(book, config.config_calibre_dir, book_format=book_format.upper()) try:
if not book_format: helper.delete_book(book, config.config_calibre_dir, book_format=book_format.upper())
# delete book from Shelfs, Downloads, Read list if not book_format:
ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).delete() # delete book from Shelfs, Downloads, Read list
ub.session.query(ub.ReadBook).filter(ub.ReadBook.book_id == book_id).delete() ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).delete()
ub.delete_download(book_id) ub.session.query(ub.ReadBook).filter(ub.ReadBook.book_id == book_id).delete()
ub.session.commit() ub.delete_download(book_id)
ub.session.commit()
# check if only this book links to:
# author, language, series, tags, custom columns # check if only this book links to:
modify_database_object([u''], book.authors, db.Authors, db.session, 'author') # author, language, series, tags, custom columns
modify_database_object([u''], book.tags, db.Tags, db.session, 'tags') modify_database_object([u''], book.authors, db.Authors, db.session, 'author')
modify_database_object([u''], book.series, db.Series, db.session, 'series') modify_database_object([u''], book.tags, db.Tags, db.session, 'tags')
modify_database_object([u''], book.languages, db.Languages, db.session, 'languages') modify_database_object([u''], book.series, db.Series, db.session, 'series')
modify_database_object([u''], book.publishers, db.Publishers, db.session, 'publishers') modify_database_object([u''], book.languages, db.Languages, db.session, 'languages')
modify_database_object([u''], book.publishers, db.Publishers, db.session, 'publishers')
cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
for c in cc: cc = db.session.query(db.Custom_Columns).\
cc_string = "custom_column_" + str(c.id) filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
if not c.is_multiple: for c in cc:
if len(getattr(book, cc_string)) > 0: cc_string = "custom_column_" + str(c.id)
if c.datatype == 'bool' or c.datatype == 'integer' or c.datatype == 'float': if not c.is_multiple:
del_cc = getattr(book, cc_string)[0] if len(getattr(book, cc_string)) > 0:
getattr(book, cc_string).remove(del_cc) if c.datatype == 'bool' or c.datatype == 'int' or c.datatype == 'float':
db.session.delete(del_cc) del_cc = getattr(book, cc_string)[0]
elif c.datatype == 'rating': getattr(book, cc_string).remove(del_cc)
del_cc = getattr(book, cc_string)[0] log.debug('remove ' + str(c.id))
getattr(book, cc_string).remove(del_cc)
if len(del_cc.books) == 0:
db.session.delete(del_cc) db.session.delete(del_cc)
else: db.session.commit()
del_cc = getattr(book, cc_string)[0] elif c.datatype == 'rating':
getattr(book, cc_string).remove(del_cc) del_cc = getattr(book, cc_string)[0]
db.session.delete(del_cc) getattr(book, cc_string).remove(del_cc)
else: if len(del_cc.books) == 0:
modify_database_object([u''], getattr(book, cc_string), db.cc_classes[c.id], log.debug('remove ' + str(c.id))
db.session, 'custom') db.session.delete(del_cc)
db.session.query(db.Books).filter(db.Books.id == book_id).delete() db.session.commit()
else: else:
db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == book_format).delete() del_cc = getattr(book, cc_string)[0]
db.session.commit() getattr(book, cc_string).remove(del_cc)
log.debug('remove ' + str(c.id))
db.session.delete(del_cc)
db.session.commit()
else:
modify_database_object([u''], getattr(book, cc_string), db.cc_classes[c.id],
db.session, 'custom')
db.session.query(db.Books).filter(db.Books.id == book_id).delete()
else:
db.session.query(db.Data).filter(db.Data.book == book.id).\
filter(db.Data.format == book_format).delete()
db.session.commit()
except Exception as e:
log.debug(e)
db.session.rollback()
else: else:
# book not found # book not found
log.error('Book with id "%s" could not be deleted: not found', book_id) log.error('Book with id "%s" could not be deleted: not found', book_id)

@ -283,6 +283,9 @@ def delete_book_file(book, calibrepath, book_format=None):
log.error("Deleting book %s failed, path has subfolders: %s", book.id, book.path) log.error("Deleting book %s failed, path has subfolders: %s", book.id, book.path)
return False return False
shutil.rmtree(path, ignore_errors=True) shutil.rmtree(path, ignore_errors=True)
authorpath = os.path.join(calibrepath, os.path.split(book.path)[0])
if not os.listdir(authorpath):
shutil.rmtree(authorpath, ignore_errors=True)
return True return True
else: else:
log.error("Deleting book %s failed, book path not valid: %s", book.id, book.path) log.error("Deleting book %s failed, book path not valid: %s", book.id, book.path)

Loading…
Cancel
Save