diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py index 1774ce4e..161d99ff 100644 --- a/mediagoblin/decorators.py +++ b/mediagoblin/decorators.py @@ -44,3 +44,22 @@ def require_active_login(controller): return controller(request, *args, **kwargs) return _make_safe(new_controller_func, controller) + + +def uses_pagination(controller): + """ + Check request GET 'page' key for wrong values + """ + def wrapper(request, *args, **kwargs): + try: + page = int(request.str_GET['page']) + if page < 0: + return exc.HTTPNotFound() + except ValueError: + return exc.HTTPNotFound() + except KeyError: + request.str_GET['page'] = 1 + + return controller(request, *args, **kwargs) + + return _make_safe(wrapper,controller) 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..d1809e80 100644 --- a/mediagoblin/templates/mediagoblin/user_pages/user.html +++ b/mediagoblin/templates/mediagoblin/user_pages/user.html @@ -20,17 +20,10 @@ {% if user %}

User page for '{{ user.username }}'

- {#- 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..8ae337f5 --- /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 . +#} + +{% block object_gallery_content -%} +
+ {% if media_entries %} + + {% include "mediagoblin/utils/pagination.html" %} + {% endif %} + +
+{% endblock %} diff --git a/mediagoblin/templates/mediagoblin/utils/pagination.html b/mediagoblin/templates/mediagoblin/utils/pagination.html new file mode 100644 index 00000000..b74cbfcf --- /dev/null +++ b/mediagoblin/templates/mediagoblin/utils/pagination.html @@ -0,0 +1,47 @@ +{# 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 . +#} + +{# only display if {{pagination}} is defined #} + +{% if pagination %} + +{% endif %} + diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 9e9e3f51..d8665915 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -16,8 +16,11 @@ from webob import Response, exc from mediagoblin.db.util import ObjectId, DESCENDING +from mediagoblin.util import Pagination +from mediagoblin.decorators import uses_pagination +@uses_pagination def user_home(request): """'Homepage' of a User()""" user = request.db.User.find_one({ @@ -26,18 +29,27 @@ def user_home(request): if not user: return exc.HTTPNotFound() - medias = request.db.MediaEntry.find({ - 'uploader': user, - 'state': 'processed'}).sort('created', DESCENDING) + cursor = request.db.MediaEntry \ + .find({'uploader': user, 'state': 'processed'}) \ + .sort('created', DESCENDING) + + pagination = Pagination( int(request.str_GET['page']), cursor) + media_entries = pagination() + + #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()""" @@ -56,3 +68,4 @@ def media_home(request): template.render( {'request': request, 'media': media})) + diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 680ff62e..b05aad1d 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -26,6 +26,11 @@ import translitcodec from mediagoblin import globals as mgoblin_globals +import urllib +from math import ceil +import copy +import decorators +from webob import exc TESTS_ENABLED = False def _activate_testing(): @@ -290,3 +295,66 @@ def setup_gettext(locale): mgoblin_globals.setup_globals( translations=this_gettext) + + +class Pagination(object): + """ + Pagination class, + initialization through __init__(self, cursor, page=1, per_page=2): + get actual data slice through __call__() + """ + + def __init__(self, page, cursor, per_page=2): + """ + initializes Pagination + -- page, requested page + -- per_page, number of objects per page + -- cursor, db cursor + """ + self.page = page + self.per_page = per_page + self.cursor = cursor + self.total_count = self.cursor.count() + + 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) + + @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 get_page_url(self, path_info, page_no, get_params=None): + """ + Get a new page based of the path_info, the new page number, + and existing get parameters. + """ + new_get_params = copy.copy(get_params or {}) + new_get_params['page'] = page_no + return "%s?%s" % ( + path_info, urllib.urlencode(new_get_params)) +