commit 1b295402aab71fa4fee29b3fb58cec89ed99c1b1
Author: dsorber <david.sorber@gmail.com>
Date:   Sun Jan 18 22:27:52 2015 -0500

    I haven't really don't anything yet other than cleanup some of the messiness that I left behind. I fixed pagination and renamed a few things to make them more clear.

diff --git a/software/bookmark_library/bmklib/web/base_controller.py b/software/bookmark_library/bmklib/web/base_controller.py
index 7f32dfa..3d50e29 100644
--- a/software/bookmark_library/bmklib/web/base_controller.py
+++ b/software/bookmark_library/bmklib/web/base_controller.py
@@ -7,7 +7,7 @@ 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
+MAX_PER_PAGE = 30
 
 class BaseHTMLController(object):
     
diff --git a/software/bookmark_library/bmklib/web/bmklib.wsgi b/software/bookmark_library/bmklib/web/bmklib.wsgi
index cf101bd..ec202d9 100644
--- a/software/bookmark_library/bmklib/web/bmklib.wsgi
+++ b/software/bookmark_library/bmklib/web/bmklib.wsgi
@@ -1,4 +1,6 @@
-from bmklib.web.controllers.bookmark import BookmarkPageController
+import re
+
+from bmklib.web.controllers.all_bookmarks import AllBookmarksPageController
 from bmklib.web.controllers.main import MainPageController
 
 # It accepts two arguments:
@@ -7,16 +9,19 @@ from bmklib.web.controllers.main import MainPageController
 # start_response is a callback function supplied by the server
 # which will be used to send the HTTP status and headers to the server
 def application(environ, start_response):
+    
+    path = environ['PATH_INFO']
 
-    if not environ['PATH_INFO'] or environ['PATH_INFO'] == '/':
+    if not path or path == '/':
         
         controller = MainPageController(environ)
         response_headers, response_body = controller.generate_response()
 
-    elif environ['PATH_INFO'].startswith('/bookmark'):
+    elif re.search('^/bookmarks$', path) or re.search('^/bookmarks/', path):
         
-        controller = BookmarkPageController(environ)
+        controller = AllBookmarksPageController(environ)
         response_headers, response_body = controller.generate_response()
+        
     else:
         # User supplied an invalid URL
         response_body = 'Invalid URL "%s"' % environ['PATH_INFO']
diff --git a/software/bookmark_library/bmklib/web/controllers/all_bookmarks.py b/software/bookmark_library/bmklib/web/controllers/all_bookmarks.py
new file mode 100644
index 0000000..83f94e9
--- /dev/null
+++ b/software/bookmark_library/bmklib/web/controllers/all_bookmarks.py
@@ -0,0 +1,75 @@
+import math
+import os.path
+
+import sqlite3
+
+from bmklib.lib.bookmark import Bookmark
+from bmklib.web.base_controller import BaseHTMLController, DB_PATH, MAX_PER_PAGE
+
+class AllBookmarksPageController(BaseHTMLController):
+    
+    def __init__(self, environ, template_name='all_bookmarks.html'):
+        super(AllBookmarksPageController, self).__init__(environ, template_name)
+    
+    def build_content(self):
+        
+        # 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 = conn.cursor()
+        db.execute("SELECT COUNT(*) FROM bookmarks")
+        total_bmks = int(db.fetchone()[0])
+        self.content['total_num_bmks'] = total_bmks
+        self.content['max_page_idx'] = math.ceil(float(total_bmks) / MAX_PER_PAGE)
+        
+        page_idx = 1
+        
+        # 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
+                    
+                if page_idx < 1:
+                    page_idx = 1
+                    
+                query = "SELECT id FROM bookmarks WHERE deleted=0 LIMIT {:d}"\
+                        " OFFSET {:d}".format(MAX_PER_PAGE, (page_idx - 1) * MAX_PER_PAGE)
+                db.execute(query)
+        else:
+            db.execute("SELECT id FROM bookmarks WHERE deleted=0 LIMIT {:d}".format(MAX_PER_PAGE))
+        
+        self.content['page_idx'] = page_idx
+        
+        # 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 link to control pagination in the template
+        if page_idx == 1:
+            self.content['previous'] = None
+        else:
+            prev_idx = page_idx - 1
+            prev_path = ['..']  * len(path_parts)
+            prev_path.extend(['bookmarks', str(prev_idx)])
+            self.content['previous'] = os.path.join(*prev_path)
+        
+        # Set the next link to control pagination in the template
+        next_idx = page_idx + 1
+        if next_idx > self.content['max_page_idx']:
+            self.content['next'] = None
+        else:
+            next_path = ['..']  * len(path_parts)
+            next_path.extend(['bookmarks', str(next_idx)])
+            self.content['next'] = os.path.join(*next_path)
diff --git a/software/bookmark_library/bmklib/web/controllers/bookmark.py b/software/bookmark_library/bmklib/web/controllers/bookmark.py
deleted file mode 100644
index 92e1729..0000000
--- a/software/bookmark_library/bmklib/web/controllers/bookmark.py
+++ /dev/null
@@ -1,59 +0,0 @@
-import sqlite3
-
-from bmklib.lib.bookmark import Bookmark
-from bmklib.web.base_controller import BaseHTMLController, DB_PATH, MAX_PER_PAGE
-
-class BookmarkPageController(BaseHTMLController):
-    
-    def __init__(self, environ, template_name='bookmark.html'):
-        super(BookmarkPageController, self).__init__(environ, template_name)
-    
-    def build_content(self):
-        
-        # 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()
-        
-        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/controllers/main.py b/software/bookmark_library/bmklib/web/controllers/main.py
index f7c9bbc..c9dfa6c 100644
--- a/software/bookmark_library/bmklib/web/controllers/main.py
+++ b/software/bookmark_library/bmklib/web/controllers/main.py
@@ -1,3 +1,5 @@
+import os.path
+
 import sqlite3
 
 from bmklib.web.base_controller import BaseHTMLController, DB_PATH
@@ -6,12 +8,26 @@ class MainPageController(BaseHTMLController):
     
     def __init__(self, environ, template_name='main.html'):
         super(MainPageController, self).__init__(environ, template_name)
+        
+    def build_link(self, link):
+        # Create links based on the PATH_INFO
+        prefix = []
+        if not self.environ['PATH_INFO']:
+            prefix = ['/', 'bmk']
+        
+        prefix.append(link)
+        return os.path.join(*prefix)
     
     def build_content(self):
         conn = sqlite3.connect(DB_PATH)
-        
+
+        # Grab the total number of bookmarks
         db = conn.cursor()
         db.execute("SELECT COUNT(*) FROM bookmarks")
-        row = db.fetchone()
+        self.content['total_num_bmks'] = db.fetchone()[0]
+        
+        
+        links = {'All Bookmarks': self.build_link('bookmarks'), 
+                 'All Tags': self.build_link('tags')}
         
-        self.content['total_num_bmks'] = row[0]
+        self.content['links'] = links
diff --git a/software/bookmark_library/bmklib/web/templates/all_bookmarks.html b/software/bookmark_library/bmklib/web/templates/all_bookmarks.html
new file mode 100644
index 0000000..74a4fe2
--- /dev/null
+++ b/software/bookmark_library/bmklib/web/templates/all_bookmarks.html
@@ -0,0 +1,24 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/">
+<head>
+<title>Bookmark Library</title>
+<link rel="stylesheet" type="text/css" href="http://127.0.0.1/htdocs/css/bmk_main.css"></link>
+</head>
+
+<body>
+<h1>All Bookmarks</h1>
+<p>${page_idx} of ${max_page_idx}</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"><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/bookmark.html b/software/bookmark_library/bmklib/web/templates/bookmark.html
deleted file mode 100644
index 6a7b1f4..0000000
--- a/software/bookmark_library/bmklib/web/templates/bookmark.html
+++ /dev/null
@@ -1,26 +0,0 @@
-<html xmlns="http://www.w3.org/1999/xhtml"
-      xmlns:py="http://genshi.edgewall.org/">
-<head>
-<title>Bookmark Library</title>
-<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"><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 5be1506..db534cd 100644
--- a/software/bookmark_library/bmklib/web/templates/main.html
+++ b/software/bookmark_library/bmklib/web/templates/main.html
@@ -8,10 +8,10 @@
 <body>
 <h1>Bookmark Library</h1>
 
-There are currently ${total_num_bmks} bookmarks in the library.
+<p>There are currently ${total_num_bmks} bookmarks in the library.</p>
+
 <ul>
-    <li><a href="bmk/bookmark">All bookmarks</a></li>
-    <li>All tags</li>
+    <li py:for="title, link in links.items()"><a href="${link}">${title}</a></li>
 </ul>
 </body>
 </html>
