From da52809b4d2392d471fc7ab333e9204f1c56b562 Mon Sep 17 00:00:00 2001 From: Felix Bauckholt Date: Sat, 6 Feb 2016 16:41:27 -0500 Subject: [PATCH] Added error messages for the checkout process Now, it's impossible to sign out stickered books --- library/database.py | 16 +++++++++++++--- library/exceptions.py | 6 ++++++ library/interface/checkout.py | 3 ++- library/interface/form.py | 21 +++++++++++++-------- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/library/database.py b/library/database.py index a188390..7693fea 100644 --- a/library/database.py +++ b/library/database.py @@ -1,6 +1,7 @@ import sqlite3 -from library import permissions +from library import permissions +from library.exceptions import * # because of the way that SQLite works we need to have these two # files be different, because the Office staff needs read/write @@ -248,6 +249,13 @@ def getBookCategories(book): c.close() return cats +def isStickered(book): + cats = getBookCategories(book) + for c in cats: + if c['category'] == "Stickered": + return True + return False + @permissions.check_permissions(permissions.PERMISSION_LIBCOM) def categorizeBook(book, cats): conn = sqlite3.connect(_catalogue_db_file) @@ -307,6 +315,9 @@ def deleteCategories(cats): ######################################### @permissions.check_permissions(permissions.PERMISSION_OFFICE) def checkout_book(book_id, uwid): + book = get_book(book_id) + if isStickered(book): + raise StickeredError() conn = sqlite3.connect(_checkout_db_file) c = conn.cursor() @@ -314,12 +325,11 @@ def checkout_book(book_id, uwid): query = "INSERT INTO " + _checkout_table + " (id, uwid) VALUES (?, ?);" c.execute(query, (book_id, uwid)) except sqlite3.IntegrityError: - return False # didn't work + raise CheckoutError() finally: conn.commit() c.close() - return True # worked @permissions.check_permissions(permissions.PERMISSION_OFFICE) def return_book(book_id): diff --git a/library/exceptions.py b/library/exceptions.py index 5ae429b..55bcee6 100644 --- a/library/exceptions.py +++ b/library/exceptions.py @@ -18,3 +18,9 @@ class PermissionsError(LibrarianException): class NoHighlightedEntry(LibrarianException): error_msg = "No highlighted entry" + +class CheckoutError(LibrarianException): + error_msg = "Checkout didn't work" + +class StickeredError(CheckoutError): + error_msg = "You can't sign out stickered books!" diff --git a/library/interface/checkout.py b/library/interface/checkout.py index 10f6560..154cfb5 100644 --- a/library/interface/checkout.py +++ b/library/interface/checkout.py @@ -1,5 +1,5 @@ import curses -from library.interface.form import FormWindow,BookForm +from library.interface.form import FormWindow,BookForm,catch_error_with import library.database as db @@ -46,6 +46,7 @@ class FinalCheck(FormWindow): def _return_values(self): return True +@catch_error_with(lambda w, hb, *args : (w, hb, None)) def checkout_procedure(w, hb, cy, cx, mx): """Procedure to check out a book diff --git a/library/interface/form.py b/library/interface/form.py index 9be8781..8dc9635 100644 --- a/library/interface/form.py +++ b/library/interface/form.py @@ -358,11 +358,16 @@ def error_form(text, w, hb): f.event_loop() f.clear() -def catch_error(fn): - def wrapper_fun(self, *args, **kwd): - try: - return fn(self, *args, **kwd) - except LibrarianException as e: - error_form(str(e), self.w, self.hb) - self.refresh() - return wrapper_fun +def catch_error_with(getwhb): + def decorator(fn): + def wrapper_fun(*args, **kwd): + try: + return fn(*args, **kwd) + except LibrarianException as e: + w, hb, cleanup = getwhb(*args, **kwd) + error_form(str(e), w, hb) + if cleanup: cleanup() + return wrapper_fun + return decorator + +catch_error = catch_error_with(lambda self, *args, **kwd : (self.w, self.hb, self.refresh))