From ae85ed0f971147ce7cee9ce02b498f909d21ce79 Mon Sep 17 00:00:00 2001 From: Bernhard Keller Date: Wed, 18 May 2011 17:32:49 +0200 Subject: [PATCH] added Pagination class, usage description in Pagination,__call__ added pagination.html, object_gallery.html as templates --- mediagoblin/templates/mediagoblin/root.html | 2 +- .../mediagoblin/user_pages/user.html | 11 +-- .../mediagoblin/utils/object_gallery.html | 36 ++++++++ .../mediagoblin/utils/pagination.html | 41 +++++++++ mediagoblin/user_pages/views.py | 23 +++-- mediagoblin/util.py | 86 +++++++++++++++++++ 6 files changed, 183 insertions(+), 16 deletions(-) create mode 100644 mediagoblin/templates/mediagoblin/utils/object_gallery.html create mode 100644 mediagoblin/templates/mediagoblin/utils/pagination.html diff --git a/mediagoblin/templates/mediagoblin/root.html b/mediagoblin/templates/mediagoblin/root.html index e2b2730a..a93a7c75 100644 --- a/mediagoblin/templates/mediagoblin/root.html +++ b/mediagoblin/templates/mediagoblin/root.html @@ -53,4 +53,4 @@ -{% endblock %} +{% endblock %} diff --git a/mediagoblin/templates/mediagoblin/user_pages/user.html b/mediagoblin/templates/mediagoblin/user_pages/user.html index 5c8692fc..48516679 100644 --- a/mediagoblin/templates/mediagoblin/user_pages/user.html +++ b/mediagoblin/templates/mediagoblin/user_pages/user.html @@ -23,14 +23,9 @@ {#- Should we outsource such a media 'gallery' view to it's own file? It could be useful for the home page and other views too -#} {% else %} {# This *should* not occur as the view makes sure we pass in a user. #} diff --git a/mediagoblin/templates/mediagoblin/utils/object_gallery.html b/mediagoblin/templates/mediagoblin/utils/object_gallery.html new file mode 100644 index 00000000..6e59c380 --- /dev/null +++ b/mediagoblin/templates/mediagoblin/utils/object_gallery.html @@ -0,0 +1,36 @@ +{# +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011 Free Software Foundation, Inc +# +# 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +#} + + +
+ {% if media_entries %} +
    + {% for entry in media_entries %} +
  • + + +
  • + {% endfor %} +
+ + {% import 'mediagoblin/utils/pagination.html' as paginationmacro %} + {{ paginationmacro.render_pagination(pagination) }} + {% endif %} +
diff --git a/mediagoblin/templates/mediagoblin/utils/pagination.html b/mediagoblin/templates/mediagoblin/utils/pagination.html new file mode 100644 index 00000000..80b4b820 --- /dev/null +++ b/mediagoblin/templates/mediagoblin/utils/pagination.html @@ -0,0 +1,41 @@ +{# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011 Free Software Foundation, Inc +# +# 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +#} + + +{% macro render_pagination(pagination) %} + +{# 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 c99556c2..55d60c6b 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -18,7 +18,7 @@ from webob import Response, exc from pymongo import DESCENDING from mongokit import ObjectId import wtforms - +from ..util import Pagination def user_home(request): """'Homepage' of a User()""" @@ -28,18 +28,26 @@ def user_home(request): if not user: return exc.HTTPNotFound() - medias = request.db.MediaEntry.find({ - 'uploader': user, - 'state': 'processed'}).sort('created', DESCENDING) - + pagination = Pagination() + media_entries = pagination( + { 'per_page': 2, + 'request': request, + 'collection':'MediaEntry', + 'query': { 'uploader':user, 'state':'processed'} } ) + + #if no data is available, return NotFound + if media_entries == None: + return exc.HTTPNotFound() + template = request.template_env.get_template( 'mediagoblin/user_pages/user.html') + return Response( template.render( {'request': request, 'user': user, - 'media_entries': medias})) - + 'media_entries': media_entries, + 'pagination': pagination})) def media_home(request): """'Homepage' of a MediaEntry()""" @@ -58,3 +66,4 @@ def media_home(request): template.render( {'request': request, 'media': media})) + diff --git a/mediagoblin/util.py b/mediagoblin/util.py index a66e2ba5..0f28dd79 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -26,6 +26,10 @@ import translitcodec from mediagoblin import globals as mgoblin_globals +import urllib +from pymongo import ASCENDING, DESCENDING +from math import ceil + TESTS_ENABLED = False def _activate_testing(): @@ -290,3 +294,85 @@ def setup_gettext(locale): mgoblin_globals.setup_globals( translations=this_gettext) + + +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'] + + try: + self.page = abs(int(args['request'].str_GET['page'])) + # set default page, if page value is not set + 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 + # + ###################################################### + + 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) + + @property + def pages(self): + return int(ceil(self.total_count / float(self.per_page))) + + @property + def has_prev(self): + return self.page > 1 + + @property + def has_next(self): + return self.page < self.pages + + def iter_pages(self, left_edge=2, left_current=2, + right_current=5, right_edge=2): + last = 0 + for num in xrange(1, self.pages + 1): + if num <= left_edge or \ + (num > self.page - left_current - 1 and \ + num < self.page + right_current) or \ + num > self.pages - right_edge: + if last + 1 != num: + yield None + yield num + last = num + + def url_generator(self, page): + return '%s?%s' % (self.request.path_info, \ + urllib.urlencode({'page':str(page)}))