Fixed Copyright Headers
[mediagoblin.git] / mediagoblin / tools / pagination.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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 count
21 from werkzeug.datastructures import MultiDict
22
23 from six.moves import zip
24
25 PAGINATION_DEFAULT_PER_PAGE = 30
26
27
28 class Pagination(object):
29 """
30 Pagination class for database queries.
31
32 Initialization through __init__(self, cursor, page=1, per_page=2),
33 get actual data slice through __call__().
34 """
35
36 def __init__(self, page, cursor, per_page=PAGINATION_DEFAULT_PER_PAGE,
37 jump_to_id=False):
38 """
39 Initializes Pagination
40
41 Args:
42 - page: requested page
43 - per_page: number of objects per page
44 - cursor: db cursor
45 - jump_to_id: object id, sets the page to the page containing the
46 object with id == jump_to_id.
47 """
48 self.page = page
49 self.per_page = per_page
50 self.cursor = cursor
51 self.total_count = self.cursor.count()
52 self.active_id = None
53
54 if jump_to_id:
55 cursor = copy.copy(self.cursor)
56
57 for (doc, increment) in list(zip(cursor, count(0))):
58 if doc.id == jump_to_id:
59 self.page = 1 + int(floor(increment / self.per_page))
60
61 self.active_id = jump_to_id
62 break
63
64 def __call__(self):
65 """
66 Returns slice of objects for the requested page
67 """
68 # TODO, return None for out of index so templates can
69 # distinguish between empty galleries and out-of-bound pages???
70 return self.cursor.slice(
71 (self.page - 1) * self.per_page,
72 self.page * self.per_page)
73
74 @property
75 def pages(self):
76 return int(ceil(self.total_count / float(self.per_page)))
77
78 @property
79 def has_prev(self):
80 return self.page > 1
81
82 @property
83 def has_next(self):
84 return self.page < self.pages
85
86 def iter_pages(self, left_edge=2, left_current=2,
87 right_current=5, right_edge=2):
88 last = 0
89 for num in xrange(1, self.pages + 1):
90 if num <= left_edge or \
91 (num > self.page - left_current - 1 and \
92 num < self.page + right_current) or \
93 num > self.pages - right_edge:
94 if last + 1 != num:
95 yield None
96 yield num
97 last = num
98
99 def get_page_url_explicit(self, base_url, get_params, page_no):
100 """
101 Get a page url by adding a page= parameter to the base url
102 """
103 if isinstance(get_params, MultiDict):
104 new_get_params = get_params.to_dict()
105 else:
106 new_get_params = dict(get_params) or {}
107
108 new_get_params['page'] = page_no
109 return "%s?%s" % (
110 base_url, urllib.urlencode(new_get_params))
111
112 def get_page_url(self, request, page_no):
113 """
114 Get a new page url based of the request, and the new page number.
115
116 This is a nice wrapper around get_page_url_explicit()
117 """
118 return self.get_page_url_explicit(
119 request.full_path, request.GET, page_no)