Made list pagination part of Lists
This way the pagination for one list does not affect the pagination
of a another one.
Patch by: Sverre Rabbelier
--- a/app/soc/logic/lists.py Sat Nov 22 22:47:10 2008 +0000
+++ b/app/soc/logic/lists.py Sat Nov 22 23:08:26 2008 +0000
@@ -28,6 +28,7 @@
DEF_PASSTHROUGH_FIELDS = [
'pagination',
+ 'pagination_form',
'description',
'heading',
'row',
--- a/app/soc/templates/soc/list/pagination.html Sat Nov 22 22:47:10 2008 +0000
+++ b/app/soc/templates/soc/list/pagination.html Sat Nov 22 23:08:26 2008 +0000
@@ -1,6 +1,6 @@
<div class="pagination">
- {{ pagination_form.as_table }}
+ {{ list.pagination_form.as_table }}
{% if list.newest %}
<a class="novisit" href="{{ list.newest }}">« First</a>
--- a/app/soc/views/helper/lists.py Sat Nov 22 22:47:10 2008 +0000
+++ b/app/soc/views/helper/lists.py Sat Nov 22 23:08:26 2008 +0000
@@ -52,8 +52,8 @@
return DEF_PAGINATION
-def cleanListParameters(offset=None, limit=None):
- """Converts and validates offset and limit values of the list.
+def getLimitAndOffset(request, idx):
+ """Retrieves, converts and validates offset and limit values
Args:
offset: offset in list which defines first item to return
@@ -62,24 +62,34 @@
Returns:
updated offset and limit values
"""
- # update offset value
+
+ offset = request.GET.get('offset_%d' % idx)
+ limit = request.GET.get('limit_%d' % idx)
+
+ if offset is None:
+ offset = ''
+
+ if limit is None:
+ limit = ''
+
try:
offset = int(offset)
- except:
- # also catches offset=None case where offset not supplied
+ except ValueError:
offset = 0
- # update limit value
try:
limit = int(limit)
- except:
- # also catches limit=None case where limit not supplied
+ except ValueError:
limit = getPreferredListPagination()
- return max(0, offset), max(1, min(limit, MAX_PAGINATION))
+ offset = max(0, offset)
+ limit = max(1, limit)
+ limit = min(MAX_PAGINATION, limit)
+
+ return limit, offset
-def getListContent(request, params, logic, filter):
+def getListContent(request, params, logic, filter=None, idx=0):
"""Returns a dict with fields used for rendering lists.
Args:
@@ -87,6 +97,7 @@
params: a dict with params for the View this list belongs to
logic: the logic object for the View this list belongs to
filter: a filter for this list
+ idx: the index of this list
Returns:
A a dictionary with the following values set:
@@ -106,8 +117,9 @@
}
"""
- offset, limit = helper.lists.cleanListParameters(
- offset=request.GET.get('offset'), limit=request.GET.get('limit'))
+ limit, offset = getLimitAndOffset(request, idx)
+ arg_name = 'limit_%d' % idx
+ pagination_form = makePaginationForm(request, limit, arg_name)
# Fetch one more to see if there should be a 'next' link
if not filter:
@@ -125,13 +137,15 @@
newest = next = prev = ''
if more:
- next = request.path + '?offset=%d&limit=%d' % (offset+limit, limit)
+ next = request.path + '?offset_%d=%d&limit_%d=%d' % (
+ idx, offset+limit, idx, limit)
if offset > 0:
- prev = request.path + '?offset=%d&limit=%d' % (max(0, offset-limit), limit)
+ prev = request.path + '?offset_%d=%d&limit_%d=%d' % (
+ idx, max(0, offset-limit), idx, limit)
if offset > limit:
- newest = request.path + '?limit=%d' % limit
+ newest = request.path + '?limit_%d=%d' % (idx, limit)
content = {
'data': data,
@@ -141,6 +155,7 @@
'limit': limit,
'newest': newest,
'next': next,
+ 'pagination_form': pagination_form,
'prev': prev,
}
@@ -151,7 +166,7 @@
def makePaginationForm(
- request, limit, arg_name='limit', choices=DEF_PAGINATION_CHOICES,
+ request, limit, arg_name, choices=DEF_PAGINATION_CHOICES,
field_name_fmt=helper.forms.DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT):
"""Returns a customized pagination limit selection form.
--- a/app/soc/views/models/base.py Sat Nov 22 22:47:10 2008 +0000
+++ b/app/soc/views/models/base.py Sat Nov 22 23:08:26 2008 +0000
@@ -370,12 +370,8 @@
page_name: the page name displayed in templates as page and header title
"""
- offset, limit = helper.lists.cleanListParameters(
- offset=request.GET.get('offset'), limit=request.GET.get('limit'))
-
context = helper.responses.getUniversalContext(request)
context['page_name'] = page_name
- context['pagination_form'] = helper.lists.makePaginationForm(request, limit)
context['list'] = soc.logic.lists.Lists(contents)
context['entity_type'] = params['name']
--- a/app/soc/views/models/request.py Sat Nov 22 22:47:10 2008 +0000
+++ b/app/soc/views/models/request.py Sat Nov 22 23:08:26 2008 +0000
@@ -155,7 +155,7 @@
uh_params['list_description'] = ugettext_lazy(
"An overview of your unhandled requests")
- uh_list = helper.lists.getListContent(request, uh_params, self._logic, filter)
+ uh_list = helper.lists.getListContent(request, uh_params, self._logic, filter, 0)
# construct the Open Requests list
@@ -168,7 +168,7 @@
ar_params['list_description'] = ugettext_lazy(
"An overview of your requests, that haven't been handled by an admin yet")
- ar_list = helper.lists.getListContent(request, ar_params, self._logic, filter)
+ ar_list = helper.lists.getListContent(request, ar_params, self._logic, filter, 1)
# fill contents with all the needed lists
contents = [uh_list,ar_list]
--- a/app/soc/views/models/role.py Sat Nov 22 22:47:10 2008 +0000
+++ b/app/soc/views/models/role.py Sat Nov 22 23:08:26 2008 +0000
@@ -106,7 +106,7 @@
except out_of_band.Error, error:
return error.response(request)
- content = helper.lists.getListContent(request, params, user_logic.logic, None)
+ content = helper.lists.getListContent(request, params, user_logic.logic)
contents = [content]
return self._list(request, params, contents, page_name)