Mercurial > piecrust2
comparison piecrust/data/paginator.py @ 6:f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 16 Aug 2014 08:15:30 -0700 |
parents | 474c9882decf |
children | dc8a6ff88f37 |
comparison
equal
deleted
inserted
replaced
5:474c9882decf | 6:f5ca5c5bed85 |
---|---|
1 import math | 1 import math |
2 import logging | 2 import logging |
3 from werkzeug.utils import cached_property | 3 from werkzeug.utils import cached_property |
4 from piecrust.data.base import IPaginationSource | |
4 from piecrust.data.filters import PaginationFilter | 5 from piecrust.data.filters import PaginationFilter |
5 from piecrust.data.iterators import PageIterator | 6 from piecrust.data.iterators import PageIterator |
6 | 7 |
7 | 8 |
8 logger = logging.getLogger(__name__) | 9 logger = logging.getLogger(__name__) |
80 def has_items(self): | 81 def has_items(self): |
81 return self.posts_this_page > 0 | 82 return self.posts_this_page > 0 |
82 | 83 |
83 @cached_property | 84 @cached_property |
84 def items_per_page(self): | 85 def items_per_page(self): |
85 return (self._parent_page.config.get('items_per_page') or | 86 if self._parent_page: |
86 self._source.items_per_page) | 87 ipp = self._parent_page.config.get('items_per_page') |
88 if ipp is not None: | |
89 return ipp | |
90 return self._source.getItemsPerPage() | |
87 | 91 |
88 @property | 92 @property |
89 def items_this_page(self): | 93 def items_this_page(self): |
90 self._load() | 94 self._load() |
91 return len(self._iterator) | 95 return len(self._iterator) |
150 total_page_count = self.total_page_count | 154 total_page_count = self.total_page_count |
151 if total_page_count == 0: | 155 if total_page_count == 0: |
152 return [] | 156 return [] |
153 | 157 |
154 if radius <= 0 or total_page_count < (2 * radius + 1): | 158 if radius <= 0 or total_page_count < (2 * radius + 1): |
155 return list(range(1, total_page_count)) | 159 return list(range(1, total_page_count + 1)) |
156 | 160 |
157 first_num = self._page_num - radius | 161 first_num = self._page_num - radius |
158 last_num = self._page_num + radius | 162 last_num = self._page_num + radius |
159 if first_num <= 0: | 163 if first_num <= 0: |
160 last_num += 1 - first_num | 164 last_num += 1 - first_num |
162 elif last_num > total_page_count: | 166 elif last_num > total_page_count: |
163 first_num -= (last_num - total_page_count) | 167 first_num -= (last_num - total_page_count) |
164 last_num = total_page_count | 168 last_num = total_page_count |
165 first_num = max(1, first_num) | 169 first_num = max(1, first_num) |
166 last_num = min(total_page_count, last_num) | 170 last_num = min(total_page_count, last_num) |
167 return list(range(first_num, last_num)) | 171 return list(range(first_num, last_num + 1)) |
168 | 172 |
169 def page(self, index): | 173 def page(self, index): |
170 return self._getPageUri(index) | 174 return self._getPageUri(index) |
171 | 175 |
172 def _load(self): | 176 def _load(self): |
189 f = PaginationFilter() | 193 f = PaginationFilter() |
190 | 194 |
191 if self._pgn_filter is not None: | 195 if self._pgn_filter is not None: |
192 f.addClause(self._pgn_filter.root_clause) | 196 f.addClause(self._pgn_filter.root_clause) |
193 | 197 |
194 conf = (self._parent_page.config.get('items_filters') or | 198 if isinstance(self._source, IPaginationSource): |
195 self._parent_page.app.config.get('site/items_filters')) | 199 sf = self._source.getPaginationFilter(self._parent_page) |
196 if conf == 'none' or conf == 'nil' or conf == '': | 200 if sf is not None: |
197 conf = None | 201 f.addClause(sf) |
198 if conf is not None: | |
199 f.addClausesFromConfig(conf) | |
200 | 202 |
201 return f | 203 return f |
202 | 204 |
203 def _getPageUri(self, index): | 205 def _getPageUri(self, index): |
204 uri = self._uri | 206 uri = self._uri |
207 uri += '/' | 209 uri += '/' |
208 uri += str(index) | 210 uri += str(index) |
209 return uri | 211 return uri |
210 | 212 |
211 def _onIteration(self): | 213 def _onIteration(self): |
212 if not self._pgn_set_on_ctx: | 214 if self._parent_page is not None and not self._pgn_set_on_ctx: |
213 eis = self._parent_page.app.env.exec_info_stack | 215 eis = self._parent_page.app.env.exec_info_stack |
214 eis.current_page_info.render_ctx.setPagination(self) | 216 eis.current_page_info.render_ctx.setPagination(self) |
215 self._pgn_set_on_ctx = True | 217 self._pgn_set_on_ctx = True |
216 | 218 |