Merge branch 'master' of git://gitorious.org/mediagoblin/mediagoblin
[mediagoblin.git] / mediagoblin / tools / pagination.py
index 859b60fb7983cbcd9cf71ac1bc600edbad49f20b..d0f08c9443ae4dd2c7711e2426567ef2b6def8e2 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
@@ -19,11 +19,13 @@ import copy
 from math import ceil, floor
 from itertools import izip, count
 
+
 PAGINATION_DEFAULT_PER_PAGE = 30
 
+
 class Pagination(object):
     """
-    Pagination class for mongodb queries.
+    Pagination class for database queries.
 
     Initialization through __init__(self, cursor, page=1, per_page=2),
     get actual data slice through __call__().
@@ -37,9 +39,9 @@ class Pagination(object):
         Args:
          - page: requested page
          - per_page: number of objects per page
-         - cursor: db cursor 
-         - jump_to_id: ObjectId, sets the page to the page containing the object
-           with _id == jump_to_id.
+         - cursor: db cursor
+         - jump_to_id: object id, sets the page to the page containing the
+           object with id == jump_to_id.
         """
         self.page = page
         self.per_page = per_page
@@ -51,19 +53,21 @@ class Pagination(object):
             cursor = copy.copy(self.cursor)
 
             for (doc, increment) in izip(cursor, count(0)):
-                if doc['_id'] == jump_to_id:
+                if doc.id == jump_to_id:
                     self.page = 1 + int(floor(increment / self.per_page))
 
                     self.active_id = jump_to_id
                     break
 
-
     def __call__(self):
         """
         Returns slice of objects for the requested page
         """
-        return self.cursor.skip(
-            (self.page - 1) * self.per_page).limit(self.per_page)
+        # TODO, return None for out of index so templates can
+        # distinguish between empty galleries and out-of-bound pages???
+        return self.cursor.slice(
+            (self.page - 1) * self.per_page,
+            self.page * self.per_page)
 
     @property
     def pages(self):
@@ -91,19 +95,19 @@ class Pagination(object):
                 last = num
 
     def get_page_url_explicit(self, base_url, get_params, page_no):
-        """ 
+        """
         Get a page url by adding a page= parameter to the base url
-        """ 
-        new_get_params = copy.copy(get_params or {})
+        """
+        new_get_params = dict(get_params) or {}
         new_get_params['page'] = page_no
         return "%s?%s" % (
             base_url, urllib.urlencode(new_get_params))
 
     def get_page_url(self, request, page_no):
-        """ 
+        """
         Get a new page url based of the request, and the new page number.
 
         This is a nice wrapper around get_page_url_explicit()
-        """ 
+        """
         return self.get_page_url_explicit(
-            request.path_info, request.GET, page_no)
+            request.full_path, request.GET, page_no)