can now add categories from the category viewer
authorJohn Ladan <jladan@uwaterloo.ca>
Sun, 25 Mar 2012 01:30:41 +0000 (21:30 -0400)
committerJohn Ladan <jladan@uwaterloo.ca>
Sun, 25 Mar 2012 01:30:41 +0000 (21:30 -0400)
browser.py
dbLayer.py
form.py
librarian.py

index bdb76a3..53c84ae 100644 (file)
@@ -1,6 +1,7 @@
+import sys
 import curses
 import dbLayer as db
-from form import bookForm
+from form import bookForm,categoryForm
 
 class browserWindow:
     hl=0
@@ -175,7 +176,22 @@ class categoryBrowser(browserWindow):
         cats = db.getCategories()
         for c in cats:
             self.entries.append({'category':c})
+        self.sortByColumn('category')
+
+    def addCategory(self):
+        w = curses.newwin(1,1,10,10)
+        cf = categoryForm(w)
+        cats = cf.eventLoop()
+        print >> sys.stderr, cats
+        for c in cats:
+            print >> sys.stderr, "adding "+str(c)
+            db.addCategory(c)
+        cf.clear()
 
     def handleInput(self,ch):
         browserWindow.handleInput(self,ch)
+        if ch==97:
+            self.addCategory()
+            self.refreshCategories()
+            self.refresh()
 
index 6661d20..4be0f29 100644 (file)
@@ -1,3 +1,4 @@
+import sys
 import sqlite3
 
 dbFile = 'sqLibrary.db'
@@ -181,6 +182,21 @@ def deleteBook(bookid):
 #########################################
 # Category related functions
 ########################################
+def categorizeBook(bookid, category):
+    conn = sqlite3.connect(dbFile)
+    c = conn.cursor()
+    if isinstance(category,str):
+        query = "INSERT OR IGNORE INTO "+categoryTable+" (category) VALUES ("+stringify(cat)+");"
+        conn.commit()
+        c.execute(query)
+        query = "SELECT cat_id FROM "+categoryTable+" WHERE category = "+stringify(category)+";"
+        c.execute(query)
+        category = c.fetchone()
+    query = "INSERT OR IGNORE INTO "+bookCategoryTable+" (id,cat_id) VALUES ("+str(bookid)+", "+str(category)+");"
+    conn.commit()
+    c.close()
+        
+
 def getCategories():
     conn = sqlite3.connect(dbFile)
     c = conn.cursor()
@@ -196,7 +212,9 @@ def addCategory(cat):
     conn = sqlite3.connect(dbFile)
     c = conn.cursor()
     query = "INSERT OR IGNORE INTO "+categoryTable+" (category) VALUES ("+stringify(cat)+");"
-    c.execte(query)
+    print >>sys.stderr, query
+    c.execute(query)
+    conn.commit()
     c.close()
 
 #########################################
diff --git a/form.py b/form.py
index 899032c..cacdc87 100644 (file)
--- a/form.py
+++ b/form.py
@@ -209,3 +209,11 @@ class bookForm(formWindow):
                     self.updateEntries(book)
                 self.refresh()
         formWindow.handleInput(self,ch)
+
+class categoryForm(formWindow):
+    caption = "Add a Category"
+    blabel = "Add"
+    labels = ["Category"]
+
+    def returnValues(self):
+        return self.entries
index adee0d3..f61a649 100755 (executable)
@@ -83,9 +83,9 @@ def redrawMenu(w,items,highlight):
 def addForm():
     w=curses.newwin(1,1)
     (my,mx)=stdscr.getmaxyx()
+    bf = form.bookForm(w)
     (r,c)=w.getmaxyx()
     w.mvwin((my-r)/2,(mx-c)/2)
-    bf = form.bookForm(w)
     bf.lookup=bookData.openLibrary
     bf.caption='Add a Book'
     bf.blabel = 'Add'