Split the list view code up in three pieces
1. getListContents which returns the required contents dictionary
2. _list which returns the response for a specified list of contents
3. list which constructs just one content dict and passes it to _list
This way it is easier to do step 1 and 2 in other code than list(),
which makes it possible to display multiple list pages (by calling
getListContents multiple times and passing the result to _list).
--- a/app/soc/views/helper/lists.py Sat Nov 22 14:01:39 2008 +0000
+++ b/app/soc/views/helper/lists.py Sat Nov 22 14:53:23 2008 +0000
@@ -23,6 +23,7 @@
]
+from soc.logic import dicts
from soc.views import helper
import soc.views.helper.forms
@@ -78,14 +79,14 @@
return max(0, offset), max(1, min(limit, MAX_PAGINATION))
-def getList(request, data, offset, limit):
+def getListContent(request, params, logic, filter):
"""Returns a dict with fields used for rendering lists.
Args:
request: the Django HTTP request object
- data: array of data to be displayed in the list
- offset: offset in list which defines first item to return
- limit: max amount of items per page
+ 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
Returns:
A a dictionary with the following values set:
@@ -105,6 +106,15 @@
}
"""
+ offset, limit = helper.lists.cleanListParameters(
+ offset=request.GET.get('offset'), limit=request.GET.get('limit'))
+
+ # Fetch one more to see if there should be a 'next' link
+ if not filter:
+ data = logic.getForLimitAndOffset(limit+1, offset=offset)
+ else:
+ data = logic.getForFields(filter, limit=limit+1, offset=offset)
+
if not data:
data = []
@@ -112,9 +122,7 @@
if more:
del data[limit:]
- newest = ''
- next = ''
- prev = ''
+ newest = next = prev = ''
if more:
next = request.path + '?offset=%d&limit=%d' % (offset+limit, limit)
@@ -129,12 +137,16 @@
'data': data,
'first': offset+1,
'last': len(data) > 1 and offset+len(data) or None,
+ 'logic': logic,
'limit': limit,
'newest': newest,
'next': next,
'prev': prev,
}
+ updates = dicts.rename(params, params['list_params'])
+ content.update(updates)
+
return content
--- a/app/soc/views/models/base.py Sat Nov 22 14:01:39 2008 +0000
+++ b/app/soc/views/models/base.py Sat Nov 22 14:53:23 2008 +0000
@@ -365,27 +365,28 @@
except out_of_band.Error, error:
return error.response(request)
- context = helper.responses.getUniversalContext(request)
- context['page_name'] = page_name
+ content = helper.lists.getListContent(request, params, self._logic, filter)
+ contents = [content]
+
+ return self._list(request, params, contents, page_name)
+
+ def _list(self, request, params, contents, page_name):
+ """Returns the list page for the specified contents
+
+ Args:
+ request: the standard Django HTTP request object
+ params: a dict with params for this View
+ contents: a list of content dicts
+ 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'))
- # Fetch one more to see if there should be a 'next' link
- if not filter:
- entities = self._logic.getForLimitAndOffset(limit+1, offset=offset)
- else:
- entities = self._logic.getForFields(filter, limit=limit+1, offset=offset)
-
+ context = helper.responses.getUniversalContext(request)
+ context['page_name'] = page_name
context['pagination_form'] = helper.lists.makePaginationForm(request, limit)
-
- updates = dicts.rename(params, params['list_params'])
- updates['logic'] = self._logic
-
- content = helper.lists.getList(request, entities, offset, limit)
- content.update(updates)
-
- context['list'] = soc.logic.lists.Lists([content])
+ context['list'] = soc.logic.lists.Lists(contents)
context['entity_type'] = params['name']
context['entity_type_plural'] = params['name_plural']