comparison piecrust/dataproviders/pageiterator.py @ 939:abc52a6262a1

bake: Support the `draft` setting.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Oct 2017 20:40:43 -0700
parents 1d0364614665
children 45ad976712ec
comparison
equal deleted inserted replaced
935:7ecb946bfafd 939:abc52a6262a1
217 217
218 def _ensureSorter(self): 218 def _ensureSorter(self):
219 if self._has_sorter: 219 if self._has_sorter:
220 return 220 return
221 if self._is_content_source: 221 if self._is_content_source:
222 # For content sources, the default sorting is reverse
223 # date/time sorting.
222 self._it = DateSortIterator(self._it, reverse=True) 224 self._it = DateSortIterator(self._it, reverse=True)
223 self._has_sorter = True 225 self._has_sorter = True
224 226
225 def _initIterator(self): 227 def _initIterator(self):
226 if self._is_content_source: 228 if self._is_content_source:
227 self._it = PageContentSourceIterator(self._source) 229 self._it = PageContentSourceIterator(self._source)
230 app = self._source.app
231 if app.config.get('baker/is_baking'):
232 # While baking, automatically exclude any page with
233 # the `draft` setting.
234 draft_setting = app.config['baker/no_bake_setting']
235 self._it = NoDraftsIterator(self._it, draft_setting)
228 else: 236 else:
229 self._it = GenericSourceIterator(self._source) 237 self._it = GenericSourceIterator(self._source)
230 238
231 def _unload(self): 239 def _unload(self):
232 self._initIterator() 240 self._initIterator()
363 def __iter__(self): 371 def __iter__(self):
364 source = self.source 372 source = self.source
365 yield from source.getAllPages() 373 yield from source.getAllPages()
366 374
367 375
376 class NoDraftsIterator:
377 def __init__(self, source, no_draft_setting):
378 self.it = source
379 self.no_draft_setting = no_draft_setting
380
381 def __iter__(self):
382 nds = self.no_draft_setting
383 yield from filter(lambda i: not i.config.get(nds), self.it)
384
385
368 class PaginationDataBuilderIterator: 386 class PaginationDataBuilderIterator:
369 def __init__(self, it): 387 def __init__(self, it):
370 self.it = it 388 self.it = it
371 389
372 def __iter__(self): 390 def __iter__(self):