Exemplo n.º 1
0
def build_page_data(ctx):
    app = ctx.app
    page = ctx.page
    first_uri, _ = split_sub_uri(app, ctx.uri)
    pgn_source = ctx.pagination_source or get_default_pagination_source(page)

    pc_data = PieCrustData()
    config_data = PageData(page, ctx)
    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': config_data,
            'assets': assetor,
            'pagination': paginator,
            'family': linker
            }

    #TODO: handle slugified taxonomy terms.

    site_data = app.config.getAll()
    providers_data = DataProvidersData(page)
    data = MergedMapping([data, providers_data, site_data])

    # Do this at the end because we want all the data to be ready to be
    # displayed in the debugger window.
    if (app.config.get('site/show_debug_info') and
            not app.config.get('baker/is_baking')):
        pc_data.enableDebugInfo(page)

    return data
Exemplo n.º 2
0
def build_page_data(ctx):
    page = ctx.page
    sub_num = ctx.sub_num
    app = page.app

    pgn_source = ctx.pagination_source or get_default_pagination_source(page)

    pc_data = PieCrustData()
    config_data = PageData(page, ctx)
    paginator = Paginator(pgn_source,
                          page,
                          sub_num,
                          pgn_filter=ctx.pagination_filter)
    assetor = Assetor(page)
    linker = Linker(page.source, page.content_item)
    data = {
        'piecrust': pc_data,
        'page': config_data,
        'assets': assetor,
        'pagination': paginator,
        'family': linker
    }

    for route in app.routes:
        name = route.func_name
        if not name:
            continue

        func = data.get(name)
        if func is None:
            data[name] = RouteFunction(route)
        elif isinstance(func, RouteFunction):
            if not func._isCompatibleRoute(route):
                raise Exception(
                    "Route function '%s' can't target both route '%s' and "
                    "route '%s' as the 2 patterns are incompatible." %
                    (name, func._route.uri_pattern, route.uri_pattern))
        else:
            raise Exception("Route function '%s' collides with an "
                            "existing function or template data." % name)

    # TODO: handle slugified taxonomy terms.

    site_data = app.config.getAll()
    providers_data = DataProvidersData(page)

    # Put the site data first so that `MergedMapping` doesn't load stuff
    # for nothing just to find a value that was in the YAML config all
    # along.
    data = MergedMapping([site_data, data, providers_data])

    # Do this at the end because we want all the data to be ready to be
    # displayed in the debugger window.
    if (app.config.get('site/show_debug_info')
            and not app.config.get('baker/is_baking')):
        pc_data.enableDebugInfo(page)

    return data
Exemplo n.º 3
0
def test_missing_asset():
    with pytest.raises(KeyError):
        fs = mock_fs().withPage('pages/foo/bar')
        with mock_fs_scope(fs):
            page = MagicMock()
            page.app = fs.getApp(cache=False)
            page.path = fs.path('/kitchen/pages/foo/bar.md')
            assetor = Assetor(page, '/foo/bar')
            assetor['this_doesnt_exist']
Exemplo n.º 4
0
def test_missing_asset():
    with pytest.raises(KeyError):
        fs = (mock_fs().withConfig().withPage('pages/foo/bar'))
        with mock_fs_scope(fs):
            app = fs.getApp()
            app.config.set('site/asset_url_format', '%page_uri%/%filename%')
            page = get_simple_page(app, 'foo/bar')

            assetor = Assetor(page)
            assetor['this_doesnt_exist']
Exemplo n.º 5
0
def test_multiple_assets_with_same_name():
    with pytest.raises(UnsupportedAssetsError):
        fs = (mock_fs().withPage('pages/foo/bar').withPageAsset(
            'pages/foo/bar', 'one.txt',
            'one text').withPageAsset('pages/foo/bar', 'one.jpg',
                                      'one picture'))
        with mock_fs_scope(fs):
            page = MagicMock()
            page.app = fs.getApp(cache=False)
            page.path = fs.path('/kitchen/pages/foo/bar.md')
            assetor = Assetor(page, '/foo/bar')
            assetor['one']
Exemplo n.º 6
0
def test_multiple_assets_with_same_name():
    with pytest.raises(UnsupportedAssetsError):
        fs = (mock_fs().withConfig().withPage('pages/foo/bar').withPageAsset(
            'pages/foo/bar', 'one.txt',
            'one text').withPageAsset('pages/foo/bar', 'one.jpg',
                                      'one picture'))
        with mock_fs_scope(fs):
            app = fs.getApp()
            app.config.set('site/asset_url_format', '%page_uri%/%filename%')
            page = get_simple_page(app, 'foo/bar')

            assetor = Assetor(page)
            assetor['one']
Exemplo n.º 7
0
def test_assets(fs, site_root, expected):
    fs.withConfig({'site': {'root': site_root}})
    with mock_fs_scope(fs):
        page = MagicMock()
        page.app = fs.getApp(cache=False)
        page.app.env.base_asset_url_format = '%uri%'
        page.path = fs.path('/kitchen/pages/foo/bar.md')
        assetor = Assetor(page, site_root.rstrip('/') + '/foo/bar')
        for en in expected.keys():
            assert hasattr(assetor, en)
            path = site_root.rstrip('/') + '/foo/bar/%s.txt' % en
            assert getattr(assetor, en) == path
            assert assetor[en] == path
Exemplo n.º 8
0
def _edit_page_form(page, url):
    data = {}
    data['is_new_page'] = False
    data['url_postback'] = url_for('.edit_page', url=url)
    data['url_upload_asset'] = url_for('.upload_page_asset', url=url)
    data['url_preview'] = url_for('.preview_page', url=url)
    data['url_cancel'] = url_for(
        '.list_source', source_name=page.source.name)
    with page.source.openItem(page.content_item, 'r',
                              encoding='utf8', newline='') as fp:
        data['page_text'] = fp.read()
    data['is_dos_nl'] = "1" if '\r\n' in data['page_text'] else "0"

    page.app.env.base_asset_url_format = \
        page.app.config.get('site/root') + '_asset/%path%'
    assetor = Assetor(page)
    assets_data = []
    for n in assetor._getAssetNames():
        assets_data.append({'name': n, 'url': assetor[n]})
    data['assets'] = assets_data

    with_menu_context(data)
    return render_template('edit_page.html', **data)
Exemplo n.º 9
0
def test_assets(fs_fac, site_root, expected):
    fs = fs_fac()
    fs.withConfig({'site': {'root': site_root}})
    with mock_fs_scope(fs):
        app = fs.getApp()
        app.config.set('site/asset_url_format', '%page_uri%/%filename%')
        page = get_simple_page(app, 'foo/bar')

        assetor = Assetor(page)
        for en in expected.keys():
            assert en in assetor
            assert hasattr(assetor, en)
            path = site_root.rstrip('/') + '/foo/bar/%s.txt' % en
            assert str(assetor[en]) == path
            assert str(getattr(assetor, en)) == path
Exemplo n.º 10
0
    def _loadCustom(self):
        page_url = self._get_uri()
        _, slug = split_uri(self.page.app, page_url)
        self._setValue('url', page_url)
        self._setValue('slug', slug)
        self._setValue('timestamp',
                       time.mktime(self.page.datetime.timetuple()))
        date_format = self.page.app.config.get('site/date_format')
        if date_format:
            self._setValue('date', self.page.datetime.strftime(date_format))

        assetor = Assetor(self.page, page_url)
        self._setValue('assets', assetor)

        segment_names = self.page.config.get('segments')
        for name in segment_names:
            self.mapLoader(name, self._load_rendered_segment)
Exemplo n.º 11
0
def build_page_data(ctx):
    app = ctx.app
    page = ctx.page
    first_uri, _ = split_sub_uri(app, ctx.uri)

    pc_data = PieCrustData()
    pgn_source = ctx.pagination_source or get_default_pagination_source(page)
    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': {},
        '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['timestamp'] = time.mktime(page.datetime.timetuple())
    date_format = app.config.get('site/date_format')
    if date_format:
        page_data['date'] = page.datetime.strftime(date_format)

    #TODO: handle slugified taxonomy terms.

    site_data = build_site_data(page)
    merge_dicts(data, site_data)

    # Do this at the end because we want all the data to be ready to be
    # displayed in the debugger window.
    if (app.config.get('site/show_debug_info')
            and not app.config.get('baker/is_baking')):
        pc_data._enableDebugInfo(page, data)

    return data
Exemplo n.º 12
0
def _load_assets(data, name):
    from piecrust.data.assetor import Assetor
    return Assetor(data._page)