From ca3ca51c5a1fa4c10b88c851c9bd04ae7978cb41 Mon Sep 17 00:00:00 2001 From: Bernhard Keller Date: Thu, 19 May 2011 17:24:31 +0200 Subject: [PATCH] changed some coding styles and changed the interface for pagination from __call__ to the __init__, also getting a cursor as input, instead of the query details --- .../mediagoblin/utils/object_gallery.html | 35 +++++++------ .../mediagoblin/utils/pagination.html | 41 +++++++++------ mediagoblin/user_pages/views.py | 15 +++--- mediagoblin/util.py | 52 ++++--------------- 4 files changed, 59 insertions(+), 84 deletions(-) diff --git a/mediagoblin/templates/mediagoblin/utils/object_gallery.html b/mediagoblin/templates/mediagoblin/utils/object_gallery.html index 6e59c380..9e8c1875 100644 --- a/mediagoblin/templates/mediagoblin/utils/object_gallery.html +++ b/mediagoblin/templates/mediagoblin/utils/object_gallery.html @@ -16,21 +16,22 @@ # along with this program. If not, see . #} +{% import 'mediagoblin/utils/pagination.html' as paginationmacro %} -
- {% if media_entries %} -
    - {% for entry in media_entries %} -
  • - - -
  • - {% endfor %} -
- - {% import 'mediagoblin/utils/pagination.html' as paginationmacro %} - {{ paginationmacro.render_pagination(pagination) }} - {% endif %} -
+
+ {% if media_entries %} +
    + {% for entry in media_entries %} +
  • + + +
  • + {% endfor %} +
+ + {{ paginationmacro.render_pagination(pagination) }} + + {% endif %} +
diff --git a/mediagoblin/templates/mediagoblin/utils/pagination.html b/mediagoblin/templates/mediagoblin/utils/pagination.html index 80b4b820..685a1bb9 100644 --- a/mediagoblin/templates/mediagoblin/utils/pagination.html +++ b/mediagoblin/templates/mediagoblin/utils/pagination.html @@ -18,24 +18,31 @@ {% macro render_pagination(pagination) %} -{# only display if {{pagination}} is defined #} + {# only display if {{pagination}} is defined #} -{% if pagination %} - -{% endif %} + {% endmacro %} diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 55d60c6b..26c67425 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -18,7 +18,8 @@ from webob import Response, exc from pymongo import DESCENDING from mongokit import ObjectId import wtforms -from ..util import Pagination +from mediagoblin.util import Pagination +from pymongo import ASCENDING, DESCENDING def user_home(request): """'Homepage' of a User()""" @@ -28,12 +29,12 @@ def user_home(request): if not user: return exc.HTTPNotFound() - pagination = Pagination() - media_entries = pagination( - { 'per_page': 2, - 'request': request, - 'collection':'MediaEntry', - 'query': { 'uploader':user, 'state':'processed'} } ) + cursor = request.db.MediaEntry \ + .find({'uploader': user, 'state': 'processed'}) \ + .sort('created', DESCENDING) + + pagination = Pagination(2, cursor, request) + media_entries = pagination() #if no data is available, return NotFound if media_entries == None: diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 0f28dd79..b79d6b05 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -27,7 +27,6 @@ import translitcodec from mediagoblin import globals as mgoblin_globals import urllib -from pymongo import ASCENDING, DESCENDING from math import ceil @@ -300,53 +299,20 @@ class Pagination(object): """ Pagination class """ - def __init__(self): - pass - - def __call__(self, args): - """ - input values: - {'page': ..., --- requested page - 'per_page': ..., --- objects per page - 'request': ..., --- webob request object for url generation - 'collection' ... --- db collection, thats to be queried - 'query': {'user': xxx}, query restrictions, db.collection.find(query) - } - - add: - option for sorting attribute - ascending, descending option - range based pagination - """ - self.per_page = args['per_page'] - self.request = args['request'] - + def __init__(self, per_page, cursor, request): try: - self.page = abs(int(args['request'].str_GET['page'])) - # set default page, if page value is not set + self.page = int(request.str_GET['page']) except KeyError: self.page = 1 - # return None(404 Error) if page is set, but has no value or has an invalid value - except ValueError: - return None - ###################################################### - # - # db queries should be changed into range based pagination - # save count and current page in some user session data - # - ###################################################### + self.per_page = per_page + self.cursor = cursor + self.request = request + self.total_count = self.cursor.count() - collection = getattr(self.request.db, args['collection']) - - self.total_count = collection.find(args['query']).count() - - #check if requested page is valid, not larger than available number of pages - if self.page > self.pages: - return None - - return collection.find(args['query']).sort('created',DESCENDING) \ - .skip((self.page-1)*self.per_page).limit(self.per_page) + def __call__(self): + return self.cursor.skip((self.page-1)*self.per_page) \ + .limit(self.per_page) @property def pages(self):