Categorizing and uncategorizing based on selection
[public/library.git] / library / database.py
index 0940ba6..9da5950 100644 (file)
@@ -269,6 +269,22 @@ def categorizeBook(book, cats):
     c.close()
 
 @permissions.check_permissions(permissions.PERMISSION_LIBCOM)
+def categorizeBooks(cat, books):
+    conn = sqlite3.connect(_catalogue_db_file)
+    c = conn.cursor()
+    query = ("INSERT OR IGNORE INTO "+_book_category_table+
+             " (id,cat_id) VALUES (?, ?);")
+    exists_query = "SELECT * FROM "+_book_category_table+" WHERE (id = ? AND cat_id = ?);"
+    for book in books:
+        args = (book['id'],cat['id'])
+        c.execute(exists_query, args)
+        if len(c.fetchall()) == 0:
+            c.execute(query,args)
+    conn.commit()
+    c.close()
+
+
+@permissions.check_permissions(permissions.PERMISSION_LIBCOM)
 def uncategorizeBook(book, cats):
     conn = sqlite3.connect(_catalogue_db_file)
     c = conn.cursor()
@@ -279,6 +295,17 @@ def uncategorizeBook(book, cats):
     conn.commit()
     c.close()
 
+@permissions.check_permissions(permissions.PERMISSION_LIBCOM)
+def uncategorizeBooks(books, cat):
+    conn = sqlite3.connect(_catalogue_db_file)
+    c = conn.cursor()
+    query = "DELETE FROM "+_book_category_table+" WHERE (id = ? AND cat_id = ?);"
+    for book in books:
+        args = (book['id'],cat['id'])
+        c.execute(query,args)
+    conn.commit()
+    c.close()
+
 def getCategories():
     conn = sqlite3.connect(_catalogue_db_file)
     c = conn.cursor()