comparison piecrust/baking/single.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 b8ff1780b491
children e7b865f8f335
comparison
equal deleted inserted replaced
368:2408eb6f4da8 369:4b1019bb2533
1 import os.path 1 import os.path
2 import copy
2 import shutil 3 import shutil
3 import codecs 4 import codecs
4 import logging 5 import logging
5 import urllib.parse 6 import urllib.parse
6 from piecrust.baking.records import ( 7 from piecrust.baking.records import (
8 from piecrust.data.filters import ( 9 from piecrust.data.filters import (
9 PaginationFilter, HasFilterClause, 10 PaginationFilter, HasFilterClause,
10 IsFilterClause, AndBooleanClause, 11 IsFilterClause, AndBooleanClause,
11 page_value_accessor) 12 page_value_accessor)
12 from piecrust.rendering import ( 13 from piecrust.rendering import (
13 PageRenderingContext, render_page, 14 QualifiedPage, PageRenderingContext, render_page,
14 PASS_FORMATTING, PASS_RENDERING) 15 PASS_FORMATTING, PASS_RENDERING)
15 from piecrust.sources.base import ( 16 from piecrust.sources.base import (
16 PageFactory, 17 PageFactory,
17 REALM_NAMES, REALM_USER, REALM_THEME) 18 REALM_NAMES, REALM_USER, REALM_THEME)
18 from piecrust.uriutil import split_uri 19 from piecrust.uriutil import split_uri
20 21
21 logger = logging.getLogger(__name__) 22 logger = logging.getLogger(__name__)
22 23
23 24
24 def copy_public_page_config(config): 25 def copy_public_page_config(config):
25 res = config.get().copy() 26 res = config.getDeepcopy()
26 for k in list(res.keys()): 27 for k in list(res.keys()):
27 if k.startswith('__'): 28 if k.startswith('__'):
28 del res[k] 29 del res[k]
29 return res 30 return res
30 31
58 bake_path.append(decoded_uri) 59 bake_path.append(decoded_uri)
59 60
60 return os.path.normpath(os.path.join(*bake_path)) 61 return os.path.normpath(os.path.join(*bake_path))
61 62
62 def bake(self, factory, route, record_entry): 63 def bake(self, factory, route, record_entry):
64 # Get the page.
65 page = factory.buildPage()
66 route_metadata = copy.deepcopy(factory.metadata)
67
68 # Add taxonomy info in the template data and route metadata if needed.
63 bake_taxonomy_info = None 69 bake_taxonomy_info = None
64 route_metadata = dict(factory.metadata)
65
66 # Add taxonomy metadata for generating the URL if needed.
67 if record_entry.taxonomy_info: 70 if record_entry.taxonomy_info:
68 tax_name, tax_term, tax_source_name = record_entry.taxonomy_info 71 tax_name, tax_term, tax_source_name = record_entry.taxonomy_info
69 taxonomy = self.app.getTaxonomy(tax_name) 72 taxonomy = self.app.getTaxonomy(tax_name)
70 slugified_term = route.slugifyTaxonomyTerm(tax_term) 73 slugified_term = route.slugifyTaxonomyTerm(tax_term)
71 route_metadata[taxonomy.term_name] = slugified_term 74 route_metadata[taxonomy.term_name] = slugified_term
72 bake_taxonomy_info = (taxonomy, tax_term) 75 bake_taxonomy_info = (taxonomy, tax_term)
73 76
74 # Generate the URL using the route. 77 # Generate the URI.
75 page = factory.buildPage()
76 uri = route.getUri(route_metadata, provider=page) 78 uri = route.getUri(route_metadata, provider=page)
77 79
78 # See if this URL has been overriden by a previously baked page. 80 # See if this URL has been overriden by a previously baked page.
79 # If that page is from another realm (e.g. a user page vs. a theme 81 # If that page is from another realm (e.g. a user page vs. a theme
80 # page), we silently skip this page. If they're from the same realm, 82 # page), we silently skip this page. If they're from the same realm,
207 cache_key) 209 cache_key)
208 record_sub_entry.flags |= \ 210 record_sub_entry.flags |= \
209 BakeRecordSubPageEntry.FLAG_FORMATTING_INVALIDATED 211 BakeRecordSubPageEntry.FLAG_FORMATTING_INVALIDATED
210 212
211 logger.debug(" p%d -> %s" % (cur_sub, out_path)) 213 logger.debug(" p%d -> %s" % (cur_sub, out_path))
212 ctx, rp = self._bakeSingle(page, sub_uri, cur_sub, out_path, 214 qp = QualifiedPage(page, route, route_metadata)
215 ctx, rp = self._bakeSingle(qp, cur_sub, out_path,
213 bake_taxonomy_info) 216 bake_taxonomy_info)
214 except Exception as ex: 217 except Exception as ex:
215 if self.app.debug: 218 if self.app.debug:
216 logger.exception(ex) 219 logger.exception(ex)
217 page_rel_path = os.path.relpath(page.path, self.app.root_dir) 220 page_rel_path = os.path.relpath(page.path, self.app.root_dir)
255 if ctx.used_pagination is not None: 258 if ctx.used_pagination is not None:
256 if ctx.used_pagination.has_more: 259 if ctx.used_pagination.has_more:
257 cur_sub += 1 260 cur_sub += 1
258 has_more_subs = True 261 has_more_subs = True
259 262
260 def _bakeSingle(self, page, sub_uri, num, out_path, 263 def _bakeSingle(self, qualified_page, num, out_path, taxonomy_info=None):
261 taxonomy_info=None): 264 ctx = PageRenderingContext(qualified_page, page_num=num)
262 ctx = PageRenderingContext(page, sub_uri)
263 ctx.page_num = num
264 if taxonomy_info: 265 if taxonomy_info:
265 ctx.setTaxonomyFilter(taxonomy_info[0], taxonomy_info[1]) 266 ctx.setTaxonomyFilter(taxonomy_info[0], taxonomy_info[1])
266 267
267 rp = render_page(ctx) 268 rp = render_page(ctx)
268 269