Workout With Friends Stay fit with a little motivation

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Properties
pagination.py
Go to the documentation of this file.
1 ##
2 #
3 # Pagination related functionality.
4 #
5
6 from pyramid.threadlocal import get_current_request
7 from webhelpers.paginate import Page as _Page
8
9
10 DEFAULT_LIMIT = 20
11
12
13 def _get_url(page):
14 request = get_current_request()
15 _query = dict(request.GET)
16 _query['page'] = page
17 return request.current_route_url(_query=_query)
18
19
20 class Page(_Page):
21
22 ##
23 #
24 # @param collection A callable which returns a collection of items
25 # @param count A callable which returns the total number of items in the collection
26 # @param limit The max number of items to return
27 # @param kwargs Arguments to pass the collection callable.
28 #
29 def __init__(self, collection, count, limit, **kwargs):
30 request = get_current_request()
31 try:
32 page = int(request.GET.get('page', 1))
33 except ValueError:
34 page = 1
35 if callable(collection):
36 collection = collection(limit=limit, page=page, **kwargs)
37 if callable(count):
38 count = count(**kwargs)
39 super(Page, self).__init__(
40 collection, item_count=count, items_per_page=limit, page=page,
41 presliced_list=True, url=_get_url)
42
43
44 ##
45 #
46 # Utility class to calculate pagination offset from page and limit.
47 #
48 class Pager(object):
49
50 def __init__(self, page, limit):
51 try:
52 page = int(page)
53 except Exception:
54 page = 1
55
56 try:
57 limit = int(limit)
58 except Exception:
59 limit = DEFAULT_LIMIT
60
61 self.page = max(page, 1)
62 self.limit = max(limit, 1)
63 self.offset = limit * (self.page - 1)
64