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 controller(request, *args, **kwargs)
|
||||||
|
|
||||||
return _make_safe(new_controller_func, controller)
|
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 mediagoblin.util import Pagination
|
||||||
from pymongo import ASCENDING, DESCENDING
|
from pymongo import ASCENDING, DESCENDING
|
||||||
|
|
||||||
|
from mediagoblin.decorators import uses_pagination
|
||||||
|
|
||||||
|
@uses_pagination
|
||||||
def user_home(request):
|
def user_home(request):
|
||||||
"""'Homepage' of a User()"""
|
"""'Homepage' of a User()"""
|
||||||
user = request.db.User.find_one({
|
user = request.db.User.find_one({
|
||||||
@ -32,13 +35,9 @@ def user_home(request):
|
|||||||
cursor = request.db.MediaEntry \
|
cursor = request.db.MediaEntry \
|
||||||
.find({'uploader': user, 'state': 'processed'}) \
|
.find({'uploader': user, 'state': 'processed'}) \
|
||||||
.sort('created', DESCENDING)
|
.sort('created', DESCENDING)
|
||||||
|
|
||||||
|
|
||||||
try:
|
pagination = Pagination( int(request.str_GET['page']), cursor)
|
||||||
page = int(request.str_GET['page'])
|
|
||||||
except KeyError:
|
|
||||||
page = 1
|
|
||||||
|
|
||||||
pagination = Pagination(cursor, page)
|
|
||||||
media_entries = pagination()
|
media_entries = pagination()
|
||||||
|
|
||||||
#if no data is available, return NotFound
|
#if no data is available, return NotFound
|
||||||
|
@ -29,6 +29,8 @@ from mediagoblin import globals as mgoblin_globals
|
|||||||
import urllib
|
import urllib
|
||||||
from math import ceil
|
from math import ceil
|
||||||
import copy
|
import copy
|
||||||
|
import decorators
|
||||||
|
from webob import exc
|
||||||
|
|
||||||
TESTS_ENABLED = False
|
TESTS_ENABLED = False
|
||||||
def _activate_testing():
|
def _activate_testing():
|
||||||
@ -298,11 +300,11 @@ def setup_gettext(locale):
|
|||||||
class Pagination(object):
|
class Pagination(object):
|
||||||
"""
|
"""
|
||||||
Pagination class,
|
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__()
|
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
|
initializes Pagination
|
||||||
-- page, requested page
|
-- page, requested page
|
||||||
|
Loading…
x
Reference in New Issue
Block a user