Patches to library for adding books.
authorMichael Gregson <mgregson@csclub.uwaterloo.ca>
Wed, 11 Mar 2009 05:33:25 +0000 (01:33 -0400)
committerMichael Gregson <mgregson@csclub.uwaterloo.ca>
Wed, 11 Mar 2009 05:33:25 +0000 (01:33 -0400)
.cf are ignored now too.

.gitignore
ceo/urwid/library.py
etc/library.cf.example [new file with mode: 0644]

index 307fdb4..c8b62c6 100644 (file)
@@ -1,3 +1,4 @@
 /build-stamp
 /build
 *.pyc
+*.cf
\ No newline at end of file
index 3cc02e8..20b7d55 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 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):
     """
@@ -35,6 +47,12 @@ def search_books(data):
     ])
     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.
@@ -83,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.
diff --git a/etc/library.cf.example b/etc/library.cf.example
new file mode 100644 (file)
index 0000000..030a0f1
--- /dev/null
@@ -0,0 +1,5 @@
+# /etc/csc/library.cf: Library Config
+
+library_db_path = /users/office/library.db
+library_connect_string = "sqlite:///home/mgregson/csc/pyceo/test.db"
+aws_account_key = "1TNCT5S0RNDV13CJJCG2"
\ No newline at end of file