From 19b8869134ad3b4c285c1b85b1afe9258de16267 Mon Sep 17 00:00:00 2001 From: Felix Bauckholt Date: Sat, 6 Feb 2016 16:44:34 -0500 Subject: [PATCH] Button-only forms, including BookView The checkout process should be way more convenient now! Also, when viewing books (by pressing Enter), you can see their categories and see if they're checked out. --- library/database.py | 9 ++++++++ library/interface/browser.py | 10 +++------ library/interface/checkout.py | 5 +++-- library/interface/form.py | 42 ++++++++++++++++++++++++----------- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/library/database.py b/library/database.py index 7693fea..0940ba6 100644 --- a/library/database.py +++ b/library/database.py @@ -330,6 +330,15 @@ def checkout_book(book_id, uwid): conn.commit() c.close() +def get_checkout_status(book_id): + conn = sqlite3.connect(_checkout_db_file) + c = conn.cursor() + query = "SELECT uwid,date_out FROM "+ _checkout_table + " WHERE id = :id ;" + c.execute(query, {"id": book_id}) + data = c.fetchone() + c.close() + if data: return {"uwid":data[0], "date_out":data[1]} + return None @permissions.check_permissions(permissions.PERMISSION_OFFICE) def return_book(book_id): diff --git a/library/interface/browser.py b/library/interface/browser.py index c327313..d3e380b 100644 --- a/library/interface/browser.py +++ b/library/interface/browser.py @@ -1,6 +1,6 @@ import curses import library.database as db -from library.interface.form import BookForm,CategoryForm,error_form,catch_error +from library.interface.form import BookForm,BookView,CategoryForm,error_form,catch_error from library.exceptions import NoHighlightedEntry class browserWindow: @@ -277,10 +277,8 @@ class trashBrowser(browserWindow): def viewSelection(self,book): bookid = book['id'] w=curses.newwin(1,1) - bf = BookForm(w, self.hb, book, width=self.mx-10) + bf = BookView(w, self.hb, book, width=self.mx-10) self.centreChild(w) - bf.caption='Viewing Book '+str(bookid) - bf.blabel='done' bf.event_loop() bf.clear() @@ -359,10 +357,8 @@ class bookBrowser(browserWindow): def viewSelection(self,book): bookid = book['id'] w=curses.newwin(1,1) - bf = BookForm(w,self.hb,book, width=self.mx-20) + bf = BookView(w,self.hb,book, width=self.mx-20) self.centreChild(w) - bf.caption='Viewing Book '+str(bookid) - bf.blabel='done' bf.event_loop() bf.clear() diff --git a/library/interface/checkout.py b/library/interface/checkout.py index 154cfb5..7248769 100644 --- a/library/interface/checkout.py +++ b/library/interface/checkout.py @@ -1,5 +1,5 @@ import curses -from library.interface.form import FormWindow,BookForm,catch_error_with +from library.interface.form import FormWindow,BookForm,BookView,catch_error_with import library.database as db @@ -18,7 +18,7 @@ class BookIDForm(FormWindow): self.clear() bookid = self.entries[0].value book = db.get_book(bookid) - bf = BookForm(self.w, self.hb, book, width=self.mx-10) + bf = BookView(self.w, self.hb, book, width=self.mx-10) (y,x) = self.w.getbegyx() (r,c) = self.w.getmaxyx() self.w.mvwin( y+(self.my-r)//2,x+(self.mx-c)//2) @@ -42,6 +42,7 @@ class FinalCheck(FormWindow): caption = "Is this information correct?" blabel = "Check Out" labels = ["Username", "Book ID"] + buttononly = True def _return_values(self): return True diff --git a/library/interface/form.py b/library/interface/form.py index 8dc9635..0cad7be 100644 --- a/library/interface/form.py +++ b/library/interface/form.py @@ -1,5 +1,7 @@ import curses from library.exceptions import LibrarianException +from copy import copy +import library.database as db class TextEntry: @@ -121,6 +123,7 @@ class FormWindow: left = 0 top = 2 row = 2 + buttononly = False caption = "Form" blabel = "Done" labels = ["label1"] @@ -144,8 +147,12 @@ class FormWindow: def event_loop(self): self.w.keypad(1) self.refresh() - self.hl=0; - self.entries[self.hl].gain_focus() + self.hl=0 + if self.buttononly: + self.bt = 1 + self._highlight_button() + else: + self.entries[self.hl].gain_focus() ch = self.w.getch() while ch != 27: @@ -215,6 +222,7 @@ class FormWindow: self.w.chgat(self.brow,1,self.mx-2,curses.A_NORMAL) def _mv_focus(self,delta): + if self.buttononly: return if self.bt==-1: self.entries[self.hl].lose_focus() else: @@ -224,7 +232,6 @@ class FormWindow: self.hl = new if new == len(self.labels): self.bt = 1 - self.bt = min(self.bt,1) self._highlight_button() else: self.bt=-1 @@ -308,6 +315,24 @@ class BookForm(FormWindow): else: FormWindow.handle_input(self,ch) +class BookView(BookForm): + blabel = "Done" + buttononly = True + labels = ["ISBN", "LCCN", "Title", "Subtitle", "Authors", "Edition", + "Publisher", "Publish Date", "Publish Year", "Publish Month", "Publish location", + "Pages", "Pagination", "Weight", "Categories", "Status"] + + def __init__(self, window, helpbar, book, width=50): + book = copy(book) + book["categories"] = " - ".join([cat["category"] for cat in db.getBookCategories(book)]) + status = db.get_checkout_status(book["id"]) + if status: + book["status"] = "Checked out by %s (%s)" % (status["uwid"], status["date_out"]) + else: + book["status"] = "On shelf" + self.caption = "Viewing Book " + str(book["id"]) + super().__init__(window, helpbar, book, width) + class CategoryForm(FormWindow): caption = "Add a Category" blabel = "Add" @@ -316,21 +341,13 @@ class CategoryForm(FormWindow): def _return_values(self): return self.entries[0].value -class DummyTextEntry(TextEntry): - def redraw(self): pass - class ErrorForm(FormWindow): caption = "Error" blabel = "OK" + buttononly = True def __init__(self,window,helpbar,errortext,width=50): self.labels = errortext.split("\n") super().__init__(window, helpbar, width=width) - self.bt = 1 - - def _make_entries(self): - self.entries = [] - for e in range(len(self.labels)): - self.entries.append(DummyTextEntry(self.w)) def redraw(self): self.w.box() @@ -339,7 +356,6 @@ class ErrorForm(FormWindow): for l in self.labels: c = 2 self.w.addstr(r+self.top,c,l) - self.entries[r].redraw() r+=1 self.w.addstr(self.brow,self.bcol[1], "<"+self.blabel+">") self.w.chgat(self.brow, self.bcol[1], len(self.blabel)+2, curses.A_REVERSE)