comparison piecrust/data/builder.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 498a917cd2d4
children 9caaf78a17db
comparison
equal deleted inserted replaced
368:2408eb6f4da8 369:4b1019bb2533
1 import re 1 import re
2 import time 2 import time
3 import copy
3 import logging 4 import logging
5 from werkzeug.utils import cached_property
4 from piecrust import APP_VERSION 6 from piecrust import APP_VERSION
5 from piecrust.configuration import merge_dicts 7 from piecrust.configuration import merge_dicts
6 from piecrust.data.assetor import Assetor 8 from piecrust.data.assetor import Assetor
7 from piecrust.data.debug import build_debug_info 9 from piecrust.data.debug import build_debug_info
8 from piecrust.data.linker import PageLinkerData 10 from piecrust.data.linker import PageLinkerData
9 from piecrust.data.paginator import Paginator 11 from piecrust.data.paginator import Paginator
10 from piecrust.uriutil import split_uri, split_sub_uri 12 from piecrust.uriutil import split_sub_uri
11 13
12 14
13 logger = logging.getLogger(__name__) 15 logger = logging.getLogger(__name__)
14 16
15 17
16 class DataBuildingContext(object): 18 class DataBuildingContext(object):
17 def __init__(self, page, uri, page_num=1): 19 def __init__(self, qualified_page, page_num=1):
18 self.page = page 20 self.page = qualified_page
19 self.uri = uri
20 self.page_num = page_num 21 self.page_num = page_num
21 self.pagination_source = None 22 self.pagination_source = None
22 self.pagination_filter = None 23 self.pagination_filter = None
23 24
24 @property 25 @property
25 def app(self): 26 def app(self):
26 return self.page.app 27 return self.page.app
27 28
29 @cached_property
30 def uri(self):
31 return self.page.getUri(self.page_num)
32
28 33
29 def build_page_data(ctx): 34 def build_page_data(ctx):
35 app = ctx.app
30 page = ctx.page 36 page = ctx.page
31 app = page.app
32 first_uri, _ = split_sub_uri(app, ctx.uri) 37 first_uri, _ = split_sub_uri(app, ctx.uri)
33 _, slug = split_uri(app, ctx.uri)
34 38
35 pc_data = PieCrustData() 39 pc_data = PieCrustData()
36 pgn_source = ctx.pagination_source or get_default_pagination_source(page) 40 pgn_source = ctx.pagination_source or get_default_pagination_source(page)
37 paginator = Paginator(page, pgn_source, ctx.page_num, 41 paginator = Paginator(page, pgn_source,
38 ctx.pagination_filter) 42 page_num=ctx.page_num,
43 pgn_filter=ctx.pagination_filter)
39 assetor = Assetor(page, first_uri) 44 assetor = Assetor(page, first_uri)
40 linker = PageLinkerData(page.source, page.rel_path) 45 linker = PageLinkerData(page.source, page.rel_path)
41 data = { 46 data = {
42 'piecrust': pc_data, 47 'piecrust': pc_data,
43 'page': dict(page.config.get()), 48 'page': {},
44 'assets': assetor, 49 'assets': assetor,
45 'pagination': paginator, 50 'pagination': paginator,
46 'family': linker 51 'family': linker
47 } 52 }
48 page_data = data['page'] 53 page_data = data['page']
54 page_data.update(copy.deepcopy(page.source_metadata))
55 page_data.update(page.config.getDeepcopy(app.debug))
49 page_data['url'] = ctx.uri 56 page_data['url'] = ctx.uri
50 page_data['slug'] = slug
51 page_data['timestamp'] = time.mktime(page.datetime.timetuple()) 57 page_data['timestamp'] = time.mktime(page.datetime.timetuple())
52 date_format = app.config.get('site/date_format') 58 date_format = app.config.get('site/date_format')
53 if date_format: 59 if date_format:
54 page_data['date'] = page.datetime.strftime(date_format) 60 page_data['date'] = page.datetime.strftime(date_format)
55 61
66 72
67 return data 73 return data
68 74
69 75
70 def build_layout_data(page, page_data, contents): 76 def build_layout_data(page, page_data, contents):
71 data = dict(page_data)
72 for name, txt in contents.items(): 77 for name, txt in contents.items():
73 if name in data: 78 if name in page_data:
74 logger.warning("Content segment '%s' will hide existing data." % 79 logger.warning("Content segment '%s' will hide existing data." %
75 name) 80 name)
76 data[name] = txt 81 page_data[name] = txt
77 return data
78 82
79 83
80 class PieCrustData(object): 84 class PieCrustData(object):
81 debug_render = ['version', 'url', 'branding', 'debug_info'] 85 debug_render = ['version', 'url', 'branding', 'debug_info']
82 debug_render_invoke = ['version', 'url', 'branding', 'debug_info'] 86 debug_render_invoke = ['version', 'url', 'branding', 'debug_info']
107 re_endpoint_sep = re.compile(r'[\/\.]') 111 re_endpoint_sep = re.compile(r'[\/\.]')
108 112
109 113
110 def build_site_data(page): 114 def build_site_data(page):
111 app = page.app 115 app = page.app
112 data = dict(app.config.get()) 116 data = app.config.getDeepcopy(app.debug)
113 for source in app.sources: 117 for source in app.sources:
114 endpoint_bits = re_endpoint_sep.split(source.data_endpoint) 118 endpoint_bits = re_endpoint_sep.split(source.data_endpoint)
115 endpoint = data 119 endpoint = data
116 for e in endpoint_bits[:-1]: 120 for e in endpoint_bits[:-1]:
117 if e not in endpoint: 121 if e not in endpoint:
118 endpoint[e] = {} 122 endpoint[e] = {}
119 endpoint = endpoint[e] 123 endpoint = endpoint[e]
120 user_data = endpoint.get(endpoint_bits[-1]) 124 user_data = endpoint.get(endpoint_bits[-1])
121 provider = source.buildDataProvider(page, user_data) 125 provider = source.buildDataProvider(page, user_data)
122 if endpoint_bits[-1] in endpoint:
123 provider.user_data = endpoint[endpoint_bits[-1]]
124 endpoint[endpoint_bits[-1]] = provider 126 endpoint[endpoint_bits[-1]] = provider
125 return data 127 return data
126 128
127 129
128 def get_default_pagination_source(page): 130 def get_default_pagination_source(page):