Fixing imports.
[public/pyceo-broken.git] / ceo / urwid / library.py
index 8eb40d8..ba4a15e 100644 (file)
@@ -5,12 +5,24 @@ from ceo.urwid.widgets import *
 from ceo.urwid.window import *
 from sqlobject.sqlbuilder import *
 from datetime import datetime, timedelta
+from ceo.pymazon import PyMazon
 
 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" ]
+    temp_cfg = conf.read(CONFIG_FILE)
+    conf.check_string_fields(CONFIG_FILE, cfg_fields, temp_cfg)
+    cfg.update(temp_cfg)
 
 def library(data):
     """
@@ -31,9 +43,16 @@ def search_books(data):
     """
     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.
@@ -52,6 +71,24 @@ def overdue_books(data):
 
     None
 
+def outbooks_search(data):
+    """
+    Display a list of all books that are signed out.
+    """
+    overdue = lib.Signout.select(lib.Signout.q.indate==None)
+
+    widgets = []
+
+    for s in overdue:
+        widgets.append(urwid.AttrWrap(ButtonText(None, s.book, str(s.book)),
+                                      None, 'selected'))
+        widgets.append(urwid.Divider())
+        
+    push_window(urwid.ListBox(widgets))
+
+    None
+
+
 def checkout_book(data):
     """
     Display the book checkout wizard.
@@ -64,6 +101,45 @@ def return_book(data):
     """
     push_wizard("Checkout", [CheckinPage, ConfirmPage])
 
+class BookAddPage(WizardPanel):
+    """
+    Thingy for going on screen to add books.
+    """
+    def init_widgets(self):
+        """
+        Make some widgets.
+        """
+        self.isbn = SingleEdit("ISBN: ")
+        
+        self.widgets = [
+            urwid.Text("Adding New Book"),
+            urwid.Divider(),
+            self.isbn
+        ]
+
+    def check(self):
+        """
+        Do black magic.
+        """
+        isbn = self.isbn.get_edit_text()
+
+        try:
+            pymazon = PyMazon(cfg["aws_account_key"])
+            book = pymazon.lookup(isbn)
+
+            currents = lib.Book.select(lib.Book.q.isbn==isbn)
+            if len(currents) == 0:
+                lib.Book(isbn=isbn, title=book.title,
+                         year=book.year, publisher=book.publisher)
+            else:
+                sys.stderr.write("Fuck you.\n")
+                set_status("Book already exists, fucker.")
+                
+        except PyMazonError, e:
+            sys.stderr.write("Book not added: " + e.message + "\n")
+            set_status("Amazon thinks this is not a book.  Take it up with them.")
+        
+
 class BookSearchPage(WizardPanel):
     """
     The page used when searching for books.
@@ -112,7 +188,7 @@ class CheckoutPage(WizardPanel):
         """
         self.state["user"] = "ERROR"
         self.state["task"] = "sign_out"
-        self.user = SingleEdit("Username: ")
+        self.user = LdapWordEdit(csclub_uri, csclub_base, 'uid', "Username: ")
         
         self.widgets = [
             urwid.Text("Book Checkout"),
@@ -122,7 +198,7 @@ class CheckoutPage(WizardPanel):
 
     def check(self):
         self.state['user'] = self.user.get_edit_text()
-        if not members.registered(self.state['user'], [terms.current()]):
+        if not members.registered(self.state['user'], terms.current()):
             set_status("User not registered for this term!")
             return True
         return False
@@ -232,7 +308,7 @@ class CheckinPage(WizardPanel):
         self.state["book"] = None
         self.state["user"] = "ERROR"
         self.state["task"] = "sign_in"
-        self.user = SingleEdit("Username: ")
+        self.user = LdapWordEdit(csclub_uri, csclub_base, 'uid', "Username: ")
         
         self.widgets = [
             urwid.Text("Book Checkin"),