comparison piecrust/sources/mixins.py @ 369:4b1019bb2533

serve: Giant refactor to change how we handle data when serving pages. * We need a distinction between source metadata and route metadata. In most cases they're the same, but in cases like taxonomy pages, route metadata contains more things that can't be in source metadata if we want to re-use cached pages. * Create a new `QualifiedPage` type which is a page with a specific route and route metadata. Pass this around in many places. * Instead of passing an URL around, use the route in the `QualifiedPage` to generate URLs. This is better since it removes the guess-work from trying to generate URLs for sub-pages. * Deep-copy app and page configurations before passing them around to things that could modify them, like data builders and such. * Exclude taxonomy pages from iterator data providers. * Properly nest iterator data providers for when the theme and user page sources are merged inside `site.pages`.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 03 May 2015 18:47:10 -0700
parents 8c0c53a315ae
children e7b865f8f335
comparison
equal deleted inserted replaced
368:2408eb6f4da8 369:4b1019bb2533
3 import logging 3 import logging
4 from piecrust.data.base import PaginationData 4 from piecrust.data.base import PaginationData
5 from piecrust.data.filters import PaginationFilter, page_value_accessor 5 from piecrust.data.filters import PaginationFilter, page_value_accessor
6 from piecrust.sources.base import PageFactory 6 from piecrust.sources.base import PageFactory
7 from piecrust.sources.interfaces import IPaginationSource, IListableSource 7 from piecrust.sources.interfaces import IPaginationSource, IListableSource
8 from piecrust.sources.pageref import PageNotFoundError
8 9
9 10
10 logger = logging.getLogger(__name__) 11 logger = logging.getLogger(__name__)
11 12
12 13
18 # iterator chain. It acts as the end. 19 # iterator chain. It acts as the end.
19 self.it = None 20 self.it = None
20 21
21 def __iter__(self): 22 def __iter__(self):
22 return self.source.getPages() 23 return self.source.getPages()
24
25
26 class SourceFactoryWithoutTaxonomiesIterator(object):
27 def __init__(self, source):
28 self.source = source
29 self._taxonomy_pages = None
30 # See comment above.
31 self.it = None
32
33 def __iter__(self):
34 self._cacheTaxonomyPages()
35 for p in self.source.getPages():
36 if p.rel_path in self._taxonomy_pages:
37 continue
38 yield p
39
40 def _cacheTaxonomyPages(self):
41 if self._taxonomy_pages is not None:
42 return
43
44 self._taxonomy_pages = set()
45 for tax in self.source.app.taxonomies:
46 page_ref = tax.getPageRef(self.source.name)
47 try:
48 self._taxonomy_pages.add(page_ref.rel_path)
49 except PageNotFoundError:
50 pass
23 51
24 52
25 class DateSortIterator(object): 53 class DateSortIterator(object):
26 def __init__(self, it, reverse=True): 54 def __init__(self, it, reverse=True):
27 self.it = it 55 self.it = it
50 """ 78 """
51 def getItemsPerPage(self): 79 def getItemsPerPage(self):
52 return self.config['items_per_page'] 80 return self.config['items_per_page']
53 81
54 def getSourceIterator(self): 82 def getSourceIterator(self):
55 return SourceFactoryIterator(self) 83 if self.config.get('iteration_includes_taxonomies', False):
84 return SourceFactoryIterator(self)
85 return SourceFactoryWithoutTaxonomiesIterator(self)
56 86
57 def getSorterIterator(self, it): 87 def getSorterIterator(self, it):
58 return DateSortIterator(it) 88 return DateSortIterator(it)
59 89
60 def getTailIterator(self, it): 90 def getTailIterator(self, it):