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)
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({
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
import urllib
from math import ceil
import copy
+import decorators
+from webob import exc
TESTS_ENABLED = False
def _activate_testing():
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