diff 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
line wrap: on
line diff
--- a/piecrust/data/builder.py	Sun May 03 18:43:28 2015 -0700
+++ b/piecrust/data/builder.py	Sun May 03 18:47:10 2015 -0700
@@ -1,22 +1,23 @@
 import re
 import time
+import copy
 import logging
+from werkzeug.utils import cached_property
 from piecrust import APP_VERSION
 from piecrust.configuration import merge_dicts
 from piecrust.data.assetor import Assetor
 from piecrust.data.debug import build_debug_info
 from piecrust.data.linker import PageLinkerData
 from piecrust.data.paginator import Paginator
-from piecrust.uriutil import split_uri, split_sub_uri
+from piecrust.uriutil import split_sub_uri
 
 
 logger = logging.getLogger(__name__)
 
 
 class DataBuildingContext(object):
-    def __init__(self, page, uri, page_num=1):
-        self.page = page
-        self.uri = uri
+    def __init__(self, qualified_page, page_num=1):
+        self.page = qualified_page
         self.page_num = page_num
         self.pagination_source = None
         self.pagination_filter = None
@@ -25,29 +26,34 @@
     def app(self):
         return self.page.app
 
+    @cached_property
+    def uri(self):
+        return self.page.getUri(self.page_num)
+
 
 def build_page_data(ctx):
+    app = ctx.app
     page = ctx.page
-    app = page.app
     first_uri, _ = split_sub_uri(app, ctx.uri)
-    _, slug = split_uri(app, ctx.uri)
 
     pc_data = PieCrustData()
     pgn_source = ctx.pagination_source or get_default_pagination_source(page)
-    paginator = Paginator(page, pgn_source, ctx.page_num,
-                          ctx.pagination_filter)
+    paginator = Paginator(page, pgn_source,
+                          page_num=ctx.page_num,
+                          pgn_filter=ctx.pagination_filter)
     assetor = Assetor(page, first_uri)
     linker = PageLinkerData(page.source, page.rel_path)
     data = {
             'piecrust': pc_data,
-            'page': dict(page.config.get()),
+            'page': {},
             'assets': assetor,
             'pagination': paginator,
             'family': linker
             }
     page_data = data['page']
+    page_data.update(copy.deepcopy(page.source_metadata))
+    page_data.update(page.config.getDeepcopy(app.debug))
     page_data['url'] = ctx.uri
-    page_data['slug'] = slug
     page_data['timestamp'] = time.mktime(page.datetime.timetuple())
     date_format = app.config.get('site/date_format')
     if date_format:
@@ -68,13 +74,11 @@
 
 
 def build_layout_data(page, page_data, contents):
-    data = dict(page_data)
     for name, txt in contents.items():
-        if name in data:
+        if name in page_data:
             logger.warning("Content segment '%s' will hide existing data." %
-                    name)
-        data[name] = txt
-    return data
+                           name)
+        page_data[name] = txt
 
 
 class PieCrustData(object):
@@ -109,7 +113,7 @@
 
 def build_site_data(page):
     app = page.app
-    data = dict(app.config.get())
+    data = app.config.getDeepcopy(app.debug)
     for source in app.sources:
         endpoint_bits = re_endpoint_sep.split(source.data_endpoint)
         endpoint = data
@@ -119,8 +123,6 @@
             endpoint = endpoint[e]
         user_data = endpoint.get(endpoint_bits[-1])
         provider = source.buildDataProvider(page, user_data)
-        if endpoint_bits[-1] in endpoint:
-            provider.user_data = endpoint[endpoint_bits[-1]]
         endpoint[endpoint_bits[-1]] = provider
     return data