Mercurial > piecrust2
comparison piecrust/data/base.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 | 422052d2e978 |
children | 4c9eab0e283b |
comparison
equal
deleted
inserted
replaced
368:2408eb6f4da8 | 369:4b1019bb2533 |
---|---|
1 import copy | |
1 import time | 2 import time |
2 import logging | 3 import logging |
3 from piecrust.data.assetor import Assetor | 4 from piecrust.data.assetor import Assetor |
4 from piecrust.uriutil import split_uri | 5 from piecrust.uriutil import split_uri |
5 | 6 |
95 self._loaders[attr_name] = loader | 96 self._loaders[attr_name] = loader |
96 | 97 |
97 def _load(self): | 98 def _load(self): |
98 if self._values is not None: | 99 if self._values is not None: |
99 return | 100 return |
100 self._values = dict(self._page.config.get()) | 101 self._values = self._page.config.getDeepcopy(self._page.app.debug) |
101 try: | 102 try: |
102 self._loadCustom() | 103 self._loadCustom() |
103 except Exception as ex: | 104 except Exception as ex: |
104 raise Exception( | 105 raise Exception( |
105 "Error while loading data for: %s" % | 106 "Error while loading data for: %s" % |
117 | 118 |
118 | 119 |
119 class PaginationData(LazyPageConfigData): | 120 class PaginationData(LazyPageConfigData): |
120 def __init__(self, page): | 121 def __init__(self, page): |
121 super(PaginationData, self).__init__(page) | 122 super(PaginationData, self).__init__(page) |
123 self._route = None | |
124 self._route_metadata = None | |
122 | 125 |
123 def _get_uri(self): | 126 def _get_uri(self): |
124 page = self._page | 127 page = self._page |
125 route = page.app.getRoute(page.source.name, page.source_metadata) | 128 if self._route is None: |
126 if route is None: | 129 # TODO: this is not quite correct, as we're missing parts of the |
127 raise Exception("Can't get route for page: %s" % page.path) | 130 # route metadata if the current page is a taxonomy page. |
128 return route.getUri(page.source_metadata, provider=page) | 131 self._route = page.app.getRoute(page.source.name, |
132 page.source_metadata) | |
133 self._route_metadata = copy.deepcopy(page.source_metadata) | |
134 if self._route is None: | |
135 raise Exception("Can't get route for page: %s" % page.path) | |
136 return self._route.getUri(self._route_metadata, provider=page) | |
129 | 137 |
130 def _loadCustom(self): | 138 def _loadCustom(self): |
131 page_url = self._get_uri() | 139 page_url = self._get_uri() |
132 _, slug = split_uri(self.page.app, page_url) | 140 _, slug = split_uri(self.page.app, page_url) |
133 self._setValue('url', page_url) | 141 self._setValue('url', page_url) |
159 | 167 |
160 if do_render: | 168 if do_render: |
161 uri = self._get_uri() | 169 uri = self._get_uri() |
162 try: | 170 try: |
163 from piecrust.rendering import ( | 171 from piecrust.rendering import ( |
164 PageRenderingContext, render_page_segments) | 172 QualifiedPage, PageRenderingContext, |
165 ctx = PageRenderingContext(self._page, uri) | 173 render_page_segments) |
174 qp = QualifiedPage(self._page, self._route, | |
175 self._route_metadata) | |
176 ctx = PageRenderingContext(qp) | |
166 segs = render_page_segments(ctx) | 177 segs = render_page_segments(ctx) |
167 except Exception as e: | 178 except Exception as e: |
168 raise Exception( | 179 raise Exception( |
169 "Error rendering segments for '%s'" % uri) from e | 180 "Error rendering segments for '%s'" % uri) from e |
170 else: | 181 else: |