Permalink
Browse files

Fixed a problem where the middleware accessed POST on every request, …

…freezing upload_handlers at that point.

git-svn-id: https://django-pagination.googlecode.com/svn/trunk@49 7f1efe38-554e-0410-b69d-834cb44da2d5
  • Loading branch information...
1 parent 8e03724 commit a4832f53b911a85a3a7589b0ee3dfec77ea6c8a6 floguy committed Mar 30, 2009
Showing with 35 additions and 19 deletions.
  1. +10 −0 docs/usage.txt
  2. +11 −4 pagination/middleware.py
  3. +14 −15 pagination/tests.py
View
@@ -70,6 +70,16 @@ That's it! You have now paginated ``object_list`` and given users of the site
a way to navigate between the different pages--all without touching your views.
+A Note About Uploads
+--------------------
+
+It is important, when using django-pagination in conjunction with file uploads,
+to be aware of when ``request.page`` is accessed. As soon as ``request.page``
+is accessed, ``request.upload_handlers`` is frozen and cannot be altered in any
+way. It's a good idea to access the ``page`` attribute on the request object
+as late as possible in your views.
+
+
Optional Settings
------------------
View
@@ -1,10 +1,17 @@
+def get_page(self):
+ """
+ A function which will be monkeypatched onto the request to get the current
+ integer representing the current page.
+ """
+ try:
+ return int(self.REQUEST['page'])
+ except (KeyError, ValueError, TypeError):
+ return 1
+
class PaginationMiddleware(object):
"""
Inserts a variable representing the current page onto the request object if
it exists in either **GET** or **POST** portions of the request.
"""
def process_request(self, request):
- try:
- request.page = int(request.REQUEST['page'])
- except (KeyError, ValueError, TypeError):
- request.page = 1
+ request.__class__.page = property(get_page)
View
@@ -30,32 +30,24 @@
>>> t = Template("{% load pagination_tags %}{% autopaginate var 2 %}{% paginate %}")
-# WARNING: Please, please nobody read this portion of the code!
->>> class GetProxy(object):
-... def __iter__(self): yield self.__dict__.__iter__
-... def copy(self): return self
-... def urlencode(self): return u''
-... def keys(self): return []
->>> class RequestProxy(object):
+>>> from django.http import HttpRequest as DjangoHttpRequest
+>>> class HttpRequest(DjangoHttpRequest):
... page = 1
-... GET = GetProxy()
->>>
-# ENDWARNING
->>> t.render(Context({'var': range(21), 'request': RequestProxy()}))
+>>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
u'\\n\\n<div class="pagination">...
>>>
>>> t = Template("{% load pagination_tags %}{% autopaginate var %}{% paginate %}")
->>> t.render(Context({'var': range(21), 'request': RequestProxy()}))
+>>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
u'\\n\\n<div class="pagination">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var 20 %}{% paginate %}")
->>> t.render(Context({'var': range(21), 'request': RequestProxy()}))
+>>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
u'\\n\\n<div class="pagination">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var by %}{% paginate %}")
->>> t.render(Context({'var': range(21), 'by': 20, 'request': RequestProxy()}))
+>>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
u'\\n\\n<div class="pagination">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var by as foo %}{{ foo }}")
->>> t.render(Context({'var': range(21), 'by': 20, 'request': RequestProxy()}))
+>>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
u'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]'
>>>
@@ -128,4 +120,11 @@
>>> p2.previous_link()
'/bacon/page/1'
+>>> from pagination.middleware import PaginationMiddleware
+>>> from django.core.handlers.wsgi import WSGIRequest
+>>> from StringIO import StringIO
+>>> middleware = PaginationMiddleware()
+>>> request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart', 'wsgi.input': StringIO()})
+>>> middleware.process_request(request)
+>>> request.upload_handlers.append('asdf')
"""

0 comments on commit a4832f5

Please sign in to comment.