From 4b0db35899a54498a38b49337d3c769d05944d59 Mon Sep 17 00:00:00 2001 From: Felix Bauckholt Date: Fri, 19 Feb 2016 21:57:54 -0500 Subject: [PATCH] Replace the stub of the library with a system call to "librarian" --- ceo/library.py | 122 --------------- ceo/urwid/library.py | 343 +------------------------------------------ etc/library.cf | 5 - 3 files changed, 4 insertions(+), 466 deletions(-) delete mode 100644 ceo/library.py delete mode 100644 etc/library.cf diff --git a/ceo/library.py b/ceo/library.py deleted file mode 100644 index 8445064f9..000000000 --- a/ceo/library.py +++ /dev/null @@ -1,122 +0,0 @@ -from sqlobject import * -from sqlobject.sqlbuilder import * -from ceo import conf -from ceo import members -from ceo import terms -import time -from datetime import datetime, timedelta - -CONFIG_FILE = "/etc/csc/library.cf" - -cfg = {} - -def configure(): - """ - Load configuration - """ - cfg_fields = [ "library_connect_string" ] - - temp_cfg = conf.read(CONFIG_FILE) - conf.check_string_fields(CONFIG_FILE, cfg_fields, temp_cfg) - cfg.update(temp_cfg) - - sqlhub.processConnection = connectionForURI(cfg["library_connect_string"]) - -class Book(SQLObject): - """ - A book. This does all the stuff we could - ever want to do with a book. - """ - isbn = StringCol() - title = StringCol() - year = StringCol() - publisher = StringCol() - authors = SQLRelatedJoin("Author") - signouts = SQLMultipleJoin("Signout") - - def sign_out(self, u): - """ - Call this with a username to sign out - a book. - """ - if members.registered(u, terms.current()): - s = Signout(username=u, book=self, - outdate=datetime.today(), indate=None) - - def sign_in(self, u): - """ - Call this to check a book back in to - the library. Username is used to - disambiguate in case more than one - copy of this book has been signed out. - """ - s = self.signouts.filter(AND(Signout.q.indate==None, Signout.q.username==u)) - if s.count() > 0: - list(s.orderBy(Signout.q.outdate).limit(1))[0].sign_in() - return True - else: - raise Exception("PEBKAC: Book not signed out!") - - def __str__(self): - """ - Magic drugs to make books display - nicely. - """ - book = "%s [%s]" % (self.title, self.year) - book += "\nBy: " - for a in self.authors: - book += a.name - book += ", " - - if self.authors.count() < 1: - book += "(unknown)" - - book = book.strip(", ") - - signouts = self.signouts.filter(Signout.q.indate==None) - if signouts.count() > 0: - book += "\nSigned Out: " - for s in signouts: - book += s.username + " (" + str(s.due_date) + "), " - - book = book.strip(", ") - - return book - - -class Author(SQLObject): - """ - An author can author many books, and a book - can have many authors. This lets us map - both ways. - """ - name = StringCol() - books = RelatedJoin("Book") - -class Signout(SQLObject): - """ - An instance of a signout associates usernames, - books, signout dates, and return dates to mark - that a book has been signed out by a particular - user. - """ - username = StringCol() - book = ForeignKey("Book") - outdate = DateCol() - indate = DateCol() - - def sign_in(self): - """ - Terminate the signout (return the book). - """ - self.indate = datetime.today() - - def _get_due_date(self): - """ - Compute the due date of the book based on the sign-out - date. - """ - return self.outdate + timedelta(weeks=2) - -if __name__ == "__main__": - print "This functionality isn't implemented yet." diff --git a/ceo/urwid/library.py b/ceo/urwid/library.py index 2aca3aa1b..fac59b217 100644 --- a/ceo/urwid/library.py +++ b/ceo/urwid/library.py @@ -1,343 +1,8 @@ -import urwid -from ceo import members -from ceo.urwid import search -from ceo.urwid.widgets import * +import os from ceo.urwid.window import * -from sqlobject.sqlbuilder import * -from datetime import datetime, timedelta -from ceo.pymazon import PyMazon -from ceo.pymazon import PyMazonError -from ceo import conf -from ceo import terms - -import ceo.library as lib - -CONFIG_FILE = "/etc/csc/library.cf" - -cfg = {} - -def configure(): - """ - Load configuration - """ - cfg_fields = [ "aws_account_key", "aws_secret_key" ] - temp_cfg = conf.read(CONFIG_FILE) - conf.check_string_fields(CONFIG_FILE, cfg_fields, temp_cfg) - cfg.update(temp_cfg) def library(data): - """ - Create the main menu for the library system. - """ - menu = make_menu([ - ("Checkout Book", checkout_book, None), - ("Return Book", return_book, None), - ("Search Books", search_books, None), - ("Add Book", add_book, None), - ("Back", raise_back, None), - ]) - push_window(menu, "Library") - -def search_books(data): - """ - Define menus for searching books. - """ - menu = make_menu([ - ("Overdue Books", overdue_books, None), - ("Signed Out Books", outbooks_search, None), - ]) - push_window(menu, "Book Search") - -def add_book(data): - """ - Add book to library. Also stab Sapphyre. - """ - push_wizard("Add Book", [BookAddPage]) - -def overdue_books(data): - """ - Display a list of all books that are overdue. - """ - oldest = datetime.today() - timedelta(weeks=2) - overdue = lib.Signout.select(AND(lib.Signout.q.outdate the username to sign the book to - task -> used for the confirmation dialog - """ - self.state["user"] = "ERROR" - self.state["task"] = "sign_out" - self.user = LdapWordEdit(csclub_uri, csclub_base, 'uid', "Username: ") - - self.widgets = [ - urwid.Text("Book Checkout"), - urwid.Divider(), - self.user, - ] - - def check(self): - self.state['user'] = self.user.get_edit_text() - if not members.registered(self.state['user'], terms.current()): - set_status("User not registered for this term!") - return True - return False - -class ConfirmPage(WizardPanel): - """ - The confirmation screen when checking-in and checking-out - a book. - """ - def init_widgets(self): - """ - Initialize widgets and state. - - task -> used to deterimine the action - """ - self.user = urwid.Text("Username: ") - self.book = urwid.Text("Book: ") - - title = "" - if self.state["task"] and self.state["task"]=="sign_in": - title = "Checkin" - else: - title = "Checkout" - - self.widgets = [ - urwid.Text("Confirm " + title), - urwid.Divider(), - self.user, - self.book - ] - - def activate(self): - """ - Ensures that correct data is displayed. - """ - self.user.set_text("Username: " + self.state["user"]) - if self.state["book"]: - self.book.set_text("Book: " + self.state["book"].title) - - def check(self): - """ - Generally used for validation, but in this case it does - the actual book check-out. - """ - #TODO: Validate user at some point (preferrably user entry screen) - if self.state["task"] and self.state["task"]=="sign_in": - self.state["book"].sign_in(self.state["user"]) - else: - self.state["book"].sign_out(self.state["user"]) - pop_window() - - -class SearchPage(urwid.WidgetWrap): - """ - Displays search results. Can search on isbn, - title, or username (for books that are currently - out). - """ - def __init__(self, isbn, title, user, state): - """ - This does the actual search, and sets up the screen - when it's done. - - title -> search by (partial) title - isbn -> search by (partial) isbn - user -> search by username (for checked-out books) - """ - self.state = state - books = [] - widgets = [] - if not title is None and not title=="": - books = lib.Book.select(LIKE(lib.Book.q.title, "%" + title + "%")) - elif not isbn is None and not isbn=="": - books = lib.Book.select(lib.Book.q.isbn==isbn) - elif (not (user is None)) and (not (user=="")): - st = lib.Signout.select(AND(lib.Signout.q.username==user, lib.Signout.q.indate==None)) - for s in st: - books.append(s.book) - else: - st = lib.Signout.select(lib.Signout.q.indate==None) - for s in st: - books.append(s.book) - - if ((type(books) != type([])) and books.count() == 0) or books == []: - widgets.append(urwid.Text("No results. Hit ESC to return to search page.")) - widgets.append(urwid.Divider()) - - for b in books: - widgets.append(urwid.AttrWrap(ButtonText(self.select, b, str(b)), - None, 'selected')) - widgets.append(urwid.Divider()) - - urwid.WidgetWrap.__init__(self, urwid.ListBox(widgets)) - - def select(self, book): - """ - Marks a book for check-in or check-out. - """ - self.state["book"] = book - pop_window() - -class CheckinPage(WizardPanel): - """ - The initial page to start the check-in widget. - """ - def init_widgets(self): - """ - Throw some widgets on the screen and set up - some state. - - book -> The book to check out. - user -> Stupid people like books. - task -> What are we doing? (For confirm screen.) - """ - self.state["book"] = None - self.state["user"] = "ERROR" - self.state["task"] = "sign_in" - self.user = LdapWordEdit(csclub_uri, csclub_base, 'uid', "Username: ") - - self.widgets = [ - urwid.Text("Book Checkin"), - urwid.Divider(), - self.user, - ] - - def check(self): - """ - Pushes the search window. - - Should validate usernames. - """ - if self.state["book"] is None: - push_window(SearchPage(None, - None, - self.user.get_edit_text(), - self.state)) - return True - else: - self.state["user"] = self.user.get_edit_text() - return False + os.system("librarian") + ui.stop() + ui.start() diff --git a/etc/library.cf b/etc/library.cf deleted file mode 100644 index afdeccbb8..000000000 --- a/etc/library.cf +++ /dev/null @@ -1,5 +0,0 @@ -# /etc/csc/library.cf: Library Config - -library_connect_string = "postgres://librarian:PWPWPWPWPWPWPWPWPWPW@127.0.0.1/library" -aws_account_key = "KEYKEYKEYKEYKEYKEYKY" -aws_secret_key = "KEYKEYKEYKEYKEYKEYKY"