commit 1371822e5e853405c1d68dfc808c2d25f7da78b5
Author: dsorber <david.sorber@gmail.com>
Date:   Sat Dec 6 22:14:05 2014 -0500

    Fixed an issue with the Bookmark class was not properly creating the category and description tag sets. Also fixed an issue with both the Bookmark and Tag classes where the passed in DB connection was not being type checked before being used. Finally I added basic pagination to the BookmarkPageController.

diff --git a/software/bookmark_library/bmklib/lib/bookmark.py b/software/bookmark_library/bmklib/lib/bookmark.py
index cf5a2ed..1ab682b 100644
--- a/software/bookmark_library/bmklib/lib/bookmark.py
+++ b/software/bookmark_library/bmklib/lib/bookmark.py
@@ -46,7 +46,7 @@ class Bookmark(object):
     def __init__(self, db_conn, id=None, url=None):
         
         # Make sure the DB connection is valid before doing anything else
-        if not db_conn:
+        if not db_conn or not isinstance(db_conn, sqlite3.Connection):
             raise DBConnectionError('Invalid database connection object!')
         
         # Setup internal parameters
@@ -93,11 +93,11 @@ class Bookmark(object):
                 
             # Grab category tags
             for row in db.execute(SQL_SELECT_CAT_TAGS, (self.id,)):
-                self.category_tags.add(Tag(int(row[0])))
+                self.category_tags.add(Tag(db_conn, int(row[0])))
                 
             # Grab description tags
             for row in db.execute(SQL_SELECT_DESC_TAGS, (self.id,)):
-                self.description_tags.add(Tag(int(row[0])))
+                self.description_tags.add(Tag(db_conn, int(row[0])))
             
         # Store hash of tag sets for dirty comparison
         self._cat_tags_hash = hash(frozenset(self.category_tags))
diff --git a/software/bookmark_library/bmklib/lib/tag.py b/software/bookmark_library/bmklib/lib/tag.py
index c3a8aa1..f7ec82a 100644
--- a/software/bookmark_library/bmklib/lib/tag.py
+++ b/software/bookmark_library/bmklib/lib/tag.py
@@ -21,7 +21,7 @@ class Tag(object):
     def __init__(self, db_conn, id=None, value=None):
         
         # Make sure the DB connection is valid before doing anything else
-        if not db_conn:
+        if not db_conn or not isinstance(db_conn, sqlite3.Connection):
             raise DBConnectionError('Invalid database connection object!')
             
         self._db_conn = db_conn
diff --git a/software/bookmark_library/bmklib/web/base_controller.py b/software/bookmark_library/bmklib/web/base_controller.py
index 02c537b..7f32dfa 100644
--- a/software/bookmark_library/bmklib/web/base_controller.py
+++ b/software/bookmark_library/bmklib/web/base_controller.py
@@ -6,6 +6,9 @@ BASE_PATH = '/home/dsorber/Documents/phalanx-repo/software/bookmark_library/'
 DB_PATH = os.path.join(BASE_PATH, 'bookmarks.db')
 TEMPLATE_PATH = os.path.join(BASE_PATH, 'bmklib', 'web', 'templates')
 
+# Someday this should go into some sort of configuration
+MAX_PER_PAGE = 20
+
 class BaseHTMLController(object):
     
     def __init__(self, environ, template_name=None):
diff --git a/software/bookmark_library/bmklib/web/controllers/bookmark.py b/software/bookmark_library/bmklib/web/controllers/bookmark.py
index ce2caf7..92e1729 100644
--- a/software/bookmark_library/bmklib/web/controllers/bookmark.py
+++ b/software/bookmark_library/bmklib/web/controllers/bookmark.py
@@ -1,7 +1,7 @@
 import sqlite3
 
 from bmklib.lib.bookmark import Bookmark
-from bmklib.web.base_controller import BaseHTMLController, DB_PATH
+from bmklib.web.base_controller import BaseHTMLController, DB_PATH, MAX_PER_PAGE
 
 class BookmarkPageController(BaseHTMLController):
     
@@ -9,11 +9,51 @@ class BookmarkPageController(BaseHTMLController):
         super(BookmarkPageController, self).__init__(environ, template_name)
     
     def build_content(self):
-        conn = sqlite3.connect(DB_PATH)
         
+        # Split the path so we can parse it below
+        path = self.environ['PATH_INFO'][1:]
+        path_parts = path.split('/')
+        
+        conn = sqlite3.connect(DB_PATH)        
         db = conn.cursor()
-        db.execute("SELECT id FROM bookmarks WHERE deleted=0")
         
+        page_idx = 0
+        
+        # Parse the path and modify the query as needed
+        if len(path_parts) == 2:
+            if path_parts[1] == 'all':
+                db.execute("SELECT id FROM bookmarks WHERE deleted=0")
+            else:
+                # Attempt to grab a new query index from the URL
+                try:
+                    page_idx = int(path_parts[1])
+                except ValueError:
+                    pass
+                    
+                query = "SELECT id FROM bookmarks WHERE deleted=0 LIMIT {:d}"\
+                        " OFFSET {:d}".format(MAX_PER_PAGE, page_idx * MAX_PER_PAGE)
+                db.execute(query)
+        else:
+            db.execute("SELECT id FROM bookmarks WHERE deleted=0 LIMIT {:d}".format(MAX_PER_PAGE))
+        
+        # Build Bookmark objects for all of the returned results
         bookmarks = [Bookmark(conn, id=row[0]) for row in db.fetchall()]
         
         self.content['bookmarks'] = bookmarks
+        self.content['path_parts'] = len(path_parts)
+        
+        # Set previous and next to control pagination in the template
+        if page_idx == 0:
+            self.content['previous'] = None
+        else:
+            prev_idx = page_idx - 1
+            if len(path_parts) == 2:
+                self.content['previous'] = '../../bookmark/{:d}'.format(prev_idx)
+            else:
+                self.content['previous'] = '../bookmark/{:d}'.format(prev_idx)
+        
+        next_idx = page_idx + 1
+        if len(path_parts) == 2:
+            self.content['next'] = '../../bookmark/{:d}'.format(next_idx)
+        else:
+            self.content['next'] = '../bookmark/{:d}'.format(next_idx)
diff --git a/software/bookmark_library/bmklib/web/htdocs/css/bmk_main.css b/software/bookmark_library/bmklib/web/htdocs/css/bmk_main.css
index 040bf49..12a9e48 100644
--- a/software/bookmark_library/bmklib/web/htdocs/css/bmk_main.css
+++ b/software/bookmark_library/bmklib/web/htdocs/css/bmk_main.css
@@ -10,7 +10,7 @@
     table {
         border: 1px solid #000;
         border-spacing: 0px;
-        margin-left: auto;
+        margin-left: 2em;
         margin-right: auto;
     }
 
@@ -47,3 +47,10 @@
         border-top: 1px solid #000;
         padding: 2px 5px;
     }
+
+    .tag {
+        background-color: #CCC;
+        font-size: 12px
+        padding: 4px;
+        border-right: 2px;
+    }
diff --git a/software/bookmark_library/bmklib/web/templates/bookmark.html b/software/bookmark_library/bmklib/web/templates/bookmark.html
index 6eb8e61..6a7b1f4 100644
--- a/software/bookmark_library/bmklib/web/templates/bookmark.html
+++ b/software/bookmark_library/bmklib/web/templates/bookmark.html
@@ -2,20 +2,25 @@
       xmlns:py="http://genshi.edgewall.org/">
 <head>
 <title>Bookmark Library</title>
-<link rel="stylesheet" type="text/css" href="../htdocs/css/bmk_main.css"></link>
+<link rel="stylesheet" type="text/css" href="http://127.0.0.1/htdocs/css/bmk_main.css"></link>
 </head>
 
 <body>
 <h1>Bookmarks</h1>
 
+<p>${path_parts}</p>
+
 <table>
     <tr py:for="idx, bookmark in enumerate(bookmarks)" class="${idx % 2 == 1 and 'odd' or 'even'}">
         <td>${bookmark.id}</td>
         <td><a href="${bookmark.url}">${bookmark.title and bookmark.title or 'no title'}</a></td>
-        <td><py:for each="tag in bookmark.category_tags"><div class="tag">${tag.value}</div></py:for></td>
-        <td><py:for each="tag in bookmark.description_tags"><div class="tag">${tag.value}</div></py:for></td>
+        <td><py:for each="tag in bookmark.category_tags"><span class="tag">${tag.value}</span></py:for></td>
+        <td><py:for each="tag in bookmark.description_tags"><span class="tag">${tag.value}</span></py:for></td>
     </tr>
 </table>
 
+<py:if test="previous is not None"><a href="bookmark/${previous}">Previous</a></py:if>
+
+<py:if test="next is not None"><a href="bookmark/${next}">Next</a></py:if>
 </body>
 </html>
diff --git a/software/bookmark_library/bmklib/web/templates/main.html b/software/bookmark_library/bmklib/web/templates/main.html
index b40f3e9..5be1506 100644
--- a/software/bookmark_library/bmklib/web/templates/main.html
+++ b/software/bookmark_library/bmklib/web/templates/main.html
@@ -10,7 +10,7 @@
 
 There are currently ${total_num_bmks} bookmarks in the library.
 <ul>
-    <li><a href="bookmark">All bookmarks</a></li>
+    <li><a href="bmk/bookmark">All bookmarks</a></li>
     <li>All tags</li>
 </ul>
 </body>
