moved check for correct page values into decorator for view function
authorBernhard Keller <keller_bernhard@web.de>
Thu, 19 May 2011 20:52:18 +0000 (22:52 +0200)
committerBernhard Keller <keller_bernhard@web.de>
Thu, 19 May 2011 20:52:18 +0000 (22:52 +0200)
mediagoblin/decorators.py
mediagoblin/user_pages/views.py
mediagoblin/util.py

index 1774ce4e71a38b632fa0cf0272aa2d89efb6b1ce..161d99ffe55a1333fcec866dc870eb615ed4bb32 100644 (file)
@@ -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)
index 76f96cf913f40945afb6258e5a5a4ca6c83477de..cb2c58752063e940ac0c7ea7285c4ae9ed904cdc 100644 (file)
@@ -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
index 9247ac19f31d9cc7fbcdfe90b6b600597208a68a..5f5c59fbb0d660ef495ec1071a5ae8d60286c37c 100644 (file)
@@ -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