Commit | Line | Data |
---|---|---|
03ae172a | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
03ae172a AW |
3 | # |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | import urllib | |
18 | import copy | |
19 | from math import ceil, floor | |
20 | from itertools import izip, count | |
1fef79f4 | 21 | from werkzeug.datastructures import MultiDict |
ee91c2b8 | 22 | |
03ae172a AW |
23 | PAGINATION_DEFAULT_PER_PAGE = 30 |
24 | ||
ee91c2b8 | 25 | |
03ae172a AW |
26 | class Pagination(object): |
27 | """ | |
bc142abc | 28 | Pagination class for database queries. |
03ae172a AW |
29 | |
30 | Initialization through __init__(self, cursor, page=1, per_page=2), | |
31 | get actual data slice through __call__(). | |
32 | """ | |
33 | ||
34 | def __init__(self, page, cursor, per_page=PAGINATION_DEFAULT_PER_PAGE, | |
35 | jump_to_id=False): | |
36 | """ | |
37 | Initializes Pagination | |
38 | ||
39 | Args: | |
40 | - page: requested page | |
41 | - per_page: number of objects per page | |
ee91c2b8 | 42 | - cursor: db cursor |
71717fd5 | 43 | - jump_to_id: object id, sets the page to the page containing the |
5c2b8486 | 44 | object with id == jump_to_id. |
03ae172a AW |
45 | """ |
46 | self.page = page | |
47 | self.per_page = per_page | |
48 | self.cursor = cursor | |
49 | self.total_count = self.cursor.count() | |
50 | self.active_id = None | |
51 | ||
52 | if jump_to_id: | |
53 | cursor = copy.copy(self.cursor) | |
54 | ||
55 | for (doc, increment) in izip(cursor, count(0)): | |
5c2b8486 | 56 | if doc.id == jump_to_id: |
03ae172a AW |
57 | self.page = 1 + int(floor(increment / self.per_page)) |
58 | ||
59 | self.active_id = jump_to_id | |
60 | break | |
61 | ||
03ae172a AW |
62 | def __call__(self): |
63 | """ | |
64 | Returns slice of objects for the requested page | |
65 | """ | |
10f1f2f5 SS |
66 | # TODO, return None for out of index so templates can |
67 | # distinguish between empty galleries and out-of-bound pages??? | |
68 | return self.cursor.slice( | |
69 | (self.page - 1) * self.per_page, | |
70 | self.page * self.per_page) | |
03ae172a AW |
71 | |
72 | @property | |
73 | def pages(self): | |
74 | return int(ceil(self.total_count / float(self.per_page))) | |
75 | ||
76 | @property | |
77 | def has_prev(self): | |
78 | return self.page > 1 | |
79 | ||
80 | @property | |
81 | def has_next(self): | |
82 | return self.page < self.pages | |
83 | ||
84 | def iter_pages(self, left_edge=2, left_current=2, | |
85 | right_current=5, right_edge=2): | |
86 | last = 0 | |
87 | for num in xrange(1, self.pages + 1): | |
88 | if num <= left_edge or \ | |
89 | (num > self.page - left_current - 1 and \ | |
90 | num < self.page + right_current) or \ | |
91 | num > self.pages - right_edge: | |
92 | if last + 1 != num: | |
93 | yield None | |
94 | yield num | |
95 | last = num | |
96 | ||
97 | def get_page_url_explicit(self, base_url, get_params, page_no): | |
ee91c2b8 | 98 | """ |
03ae172a | 99 | Get a page url by adding a page= parameter to the base url |
ee91c2b8 | 100 | """ |
1fef79f4 CAW |
101 | if isinstance(get_params, MultiDict): |
102 | new_get_params = get_params.to_dict() | |
103 | else: | |
104 | new_get_params = dict(get_params) or {} | |
105 | ||
03ae172a AW |
106 | new_get_params['page'] = page_no |
107 | return "%s?%s" % ( | |
108 | base_url, urllib.urlencode(new_get_params)) | |
109 | ||
110 | def get_page_url(self, request, page_no): | |
ee91c2b8 | 111 | """ |
03ae172a AW |
112 | Get a new page url based of the request, and the new page number. |
113 | ||
114 | This is a nice wrapper around get_page_url_explicit() | |
ee91c2b8 | 115 | """ |
03ae172a | 116 | return self.get_page_url_explicit( |
05788ef4 | 117 | request.full_path, request.GET, page_no) |