moved check for correct page values into decorator for view function
This commit is contained in:
parent
44e3e917fb
commit
3eb6fc4f2f
@ -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)
|
||||
|
@ -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({
|
||||
@ -33,12 +36,8 @@ def user_home(request):
|
||||
.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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user