From 000b85ff812b8778ec574e58b15f75a69c283db2 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Fri, 1 May 2020 10:26:35 +0200 Subject: [PATCH] Fixes for deleting books(error handling and user feedback) --- cps/admin.py | 2 -- cps/editbooks.py | 7 ++++++- cps/helper.py | 29 ++++++++++++++++++++++------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index 43c15836..795bccb8 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -933,8 +933,6 @@ def edit_user(user_id): @login_required @admin_required def reset_user_password(user_id): - if not config.get_mail_server_configured(): - abort(404) if current_user is not None and current_user.is_authenticated: ret, message = reset_password(user_id) if ret == 1: diff --git a/cps/editbooks.py b/cps/editbooks.py index 6ba252a7..d66763bf 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -177,7 +177,10 @@ def delete_book(book_id, book_format): book = db.session.query(db.Books).filter(db.Books.id == book_id).first() if book: try: - helper.delete_book(book, config.config_calibre_dir, book_format=book_format.upper()) + result, error = helper.delete_book(book, config.config_calibre_dir, book_format=book_format.upper()) + if not result: + flash(error, category="error") + return redirect(url_for('editbook.edit_book', book_id=book_id)) if not book_format: # delete book from Shelfs, Downloads, Read list ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).delete() @@ -233,8 +236,10 @@ def delete_book(book_id, book_format): # book not found log.error('Book with id "%s" could not be deleted: not found', book_id) if book_format: + flash(_('Book Format Successfully Deleted'), category="success") return redirect(url_for('editbook.edit_book', book_id=book_id)) else: + flash(_('Book Successfully Deleted'), category="success") return redirect(url_for('web.index')) diff --git a/cps/helper.py b/cps/helper.py index c648fa48..4eebba55 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -296,15 +296,29 @@ def delete_book_file(book, calibrepath, book_format=None): if os.path.isdir(path): if len(next(os.walk(path))[1]): log.error("Deleting book %s failed, path has subfolders: %s", book.id, book.path) - return False - shutil.rmtree(path, ignore_errors=True) + return False , _("Deleting book %(id)s failed, path has subfolders: %(path)s", + id=book.id, + path=book.path) + try: + for root, __, files in os.walk(path): + for f in files: + os.unlink(os.path.join(root, f)) + shutil.rmtree(path) + except (IOError, OSError) as e: + log.error("Deleting book %s failed: %s", book.id, e) + return False, _("Deleting book %(id)s failed: %(message)s", id=book.id, message=e) authorpath = os.path.join(calibrepath, os.path.split(book.path)[0]) if not os.listdir(authorpath): - shutil.rmtree(authorpath, ignore_errors=True) - return True + try: + shutil.rmtree(authorpath) + except (IOError, OSError) as e: + log.error("Deleting authorpath for book %s failed: %s", book.id, e) + return True, None else: log.error("Deleting book %s failed, book path not valid: %s", book.id, book.path) - return False + return False, _("Deleting book %(id)s failed, book path not valid: %(path)s", + id=book.id, + path=book.path) def update_dir_structure_file(book_id, calibrepath, first_author): @@ -413,7 +427,7 @@ def update_dir_structure_gdrive(book_id, first_author): def delete_book_gdrive(book, book_format): - error = False + error = None if book_format: name = '' for entry in book.data: @@ -427,7 +441,8 @@ def delete_book_gdrive(book, book_format): gFile.Trash() else: error = _(u'Book path %(path)s not found on Google Drive', path=book.path) # file not found - return error + + return error is None, error def reset_password(user_id):