From 706b9c401301c5afb5059ee6415f0a7d53876e97 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sun, 21 Jan 2024 08:12:49 +0100 Subject: [PATCH] Improved errorhandling for adding invalid book_id to shelf --- cps/shelf.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cps/shelf.py b/cps/shelf.py index 5d05cfe2..abd28a26 100644 --- a/cps/shelf.py +++ b/cps/shelf.py @@ -71,6 +71,15 @@ def add_to_shelf(shelf_id, book_id): else: maxOrder = maxOrder[0] + if not ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id, + ub.BookShelf.book_id == book_id).one_or_none(): + log.error("Invalid Book Id: %s. Could not be added to shelf %s", book_id, shelf.name) + if not xhr: + flash(_("%(book_id)s is a invalid Book Id. Could not be added to Shelf", book_id=book_id), + category="error") + return redirect(url_for('web.index')) + return "%s is a invalid Book Id. Could not be added to Shelf" % book_id, 400 + shelf.books.append(ub.BookShelf(shelf=shelf.id, book_id=book_id, order=maxOrder + 1)) shelf.last_modified = datetime.utcnow() try: