From ae85ed0f971147ce7cee9ce02b498f909d21ce79 Mon Sep 17 00:00:00 2001 From: Bernhard Keller Date: Wed, 18 May 2011 17:32:49 +0200 Subject: [PATCH 1/4] 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)})) From ca3ca51c5a1fa4c10b88c851c9bd04ae7978cb41 Mon Sep 17 00:00:00 2001 From: Bernhard Keller Date: Thu, 19 May 2011 17:24:31 +0200 Subject: [PATCH 2/4] 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): From 44e3e917fbfc89409bac08d500c5a9246e0dc5f4 Mon Sep 17 00:00:00 2001 From: Bernhard Keller Date: Thu, 19 May 2011 20:37:04 +0200 Subject: [PATCH 3/4] removed request arg from Pagination class added get_page_url() in Pagination class, to generate proper urls without losing other get arguments --- .../mediagoblin/user_pages/user.html | 2 - .../mediagoblin/utils/object_gallery.html | 35 +++++++++-------- .../mediagoblin/utils/pagination.html | 23 ++++++----- mediagoblin/user_pages/views.py | 9 ++++- mediagoblin/util.py | 38 +++++++++++++------ 5 files changed, 61 insertions(+), 46 deletions(-) diff --git a/mediagoblin/templates/mediagoblin/user_pages/user.html b/mediagoblin/templates/mediagoblin/user_pages/user.html index 48516679..d1809e80 100644 --- a/mediagoblin/templates/mediagoblin/user_pages/user.html +++ b/mediagoblin/templates/mediagoblin/user_pages/user.html @@ -20,8 +20,6 @@ {% 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 -#}
    {% include "mediagoblin/utils/object_gallery.html" %} diff --git a/mediagoblin/templates/mediagoblin/utils/object_gallery.html b/mediagoblin/templates/mediagoblin/utils/object_gallery.html index 9e8c1875..8ae337f5 100644 --- a/mediagoblin/templates/mediagoblin/utils/object_gallery.html +++ b/mediagoblin/templates/mediagoblin/utils/object_gallery.html @@ -16,22 +16,21 @@ # along with this program. If not, see . #} -{% import 'mediagoblin/utils/pagination.html' as paginationmacro %} +{% block object_gallery_content -%} +
    + {% if media_entries %} +
      + {% for entry in media_entries %} +
    • + + +
    • + {% endfor %} +
    + {% include "mediagoblin/utils/pagination.html" %} + {% endif %} -
    - {% if media_entries %} -
      - {% for entry in media_entries %} -
    • - - -
    • - {% endfor %} -
    - - {{ paginationmacro.render_pagination(pagination) }} - - {% endif %} -
    +
    +{% endblock %} diff --git a/mediagoblin/templates/mediagoblin/utils/pagination.html b/mediagoblin/templates/mediagoblin/utils/pagination.html index 685a1bb9..b74cbfcf 100644 --- a/mediagoblin/templates/mediagoblin/utils/pagination.html +++ b/mediagoblin/templates/mediagoblin/utils/pagination.html @@ -15,22 +15,21 @@ # along with this program. If not, see . #} +{# only display if {{pagination}} is defined #} -{% 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 26c67425..76f96cf9 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -33,9 +33,14 @@ def user_home(request): .find({'uploader': user, 'state': 'processed'}) \ .sort('created', DESCENDING) - pagination = Pagination(2, cursor, request) + try: + page = int(request.str_GET['page']) + except KeyError: + page = 1 + + pagination = Pagination(cursor, page) media_entries = pagination() - + #if no data is available, return NotFound if media_entries == None: return exc.HTTPNotFound() diff --git a/mediagoblin/util.py b/mediagoblin/util.py index b79d6b05..9247ac19 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -28,7 +28,7 @@ from mediagoblin import globals as mgoblin_globals import urllib from math import ceil - +import copy TESTS_ENABLED = False def _activate_testing(): @@ -297,20 +297,27 @@ def setup_gettext(locale): class Pagination(object): """ - Pagination class + Pagination class, + initialization through __init__(self, page=1, per_page=2, cursor) + get actual data slice through __call__() """ - def __init__(self, per_page, cursor, request): - try: - self.page = int(request.str_GET['page']) - except KeyError: - self.page = 1 + def __init__(self, cursor, page=1, 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.request = request 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) @@ -338,7 +345,14 @@ class Pagination(object): yield None yield num last = num - - def url_generator(self, page): - return '%s?%s' % (self.request.path_info, \ - urllib.urlencode({'page':str(page)})) + + 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)) + From 3eb6fc4f2f2b0a41677ab88bdd941b79e3e87b39 Mon Sep 17 00:00:00 2001 From: Bernhard Keller Date: Thu, 19 May 2011 22:52:18 +0200 Subject: [PATCH 4/4] moved check for correct page values into decorator for view function --- mediagoblin/decorators.py | 19 +++++++++++++++++++ mediagoblin/user_pages/views.py | 11 +++++------ mediagoblin/util.py | 6 ++++-- 3 files changed, 28 insertions(+), 8 deletions(-) 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/user_pages/views.py b/mediagoblin/user_pages/views.py index 76f96cf9..cb2c5875 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -21,6 +21,9 @@ import wtforms from mediagoblin.util import Pagination from pymongo import ASCENDING, DESCENDING +from mediagoblin.decorators import uses_pagination + +@uses_pagination def user_home(request): """'Homepage' of a User()""" user = request.db.User.find_one({ @@ -32,13 +35,9 @@ def user_home(request): cursor = request.db.MediaEntry \ .find({'uploader': user, 'state': 'processed'}) \ .sort('created', DESCENDING) + - try: - page = int(request.str_GET['page']) - except KeyError: - page = 1 - - pagination = Pagination(cursor, page) + pagination = Pagination( int(request.str_GET['page']), cursor) media_entries = pagination() #if no data is available, return NotFound diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 9247ac19..5f5c59fb 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -29,6 +29,8 @@ 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(): @@ -298,11 +300,11 @@ def setup_gettext(locale): class Pagination(object): """ Pagination class, - initialization through __init__(self, page=1, per_page=2, cursor) + initialization through __init__(self, cursor, page=1, per_page=2): get actual data slice through __call__() """ - def __init__(self, cursor, page=1, per_page=2): + def __init__(self, page, cursor, per_page=2): """ initializes Pagination -- page, requested page