def list_source(source_name, page_num): site = g.site.piecrust_app source = site.getSource(source_name) if source is None: abort(400) i = 0 default_author = site.config.get('site/author') data = {'title': "List %s" % source_name} data['pages'] = [] pgn = Paginator(source, None, sub_num=page_num, items_per_page=20) for p in pgn.items: page_data = { 'title': p.get('title') or _get_first_line_title(p), 'author': p.get('author') or default_author, 'timestamp': p.get('timestamp'), 'tags': p.get('tags', []), 'category': p.get('category'), 'source': source_name, 'url': url_for('.edit_page', url=p['rel_url']) } data['pages'].append(page_data) prev_page_url = None if pgn.prev_page_number: prev_page_url = url_for( '.list_source', source_name=source_name, page_num=pgn.prev_page_number) next_page_url = None if pgn.next_page_number: next_page_url = url_for( '.list_source', source_name=source_name, page_num=pgn.next_page_number) page_urls = [] for i in pgn.all_page_numbers(7): url = None if i != page_num: url = url_for('.list_source', source_name=source_name, page_num=i) page_urls.append({'num': i, 'url': url}) data['pagination'] = { 'prev_page': prev_page_url, 'next_page': next_page_url, 'nums': page_urls } with_menu_context(data) return render_template('list_source.html', **data)
def list_source(source_name, page_num): site = g.site.piecrust_app source = site.getSource(source_name) if source is None: abort(400) i = 0 default_author = site.config.get('site/author') data = {'title': "List %s" % source_name} data['pages'] = [] pgn = Paginator(source, None, sub_num=page_num, items_per_page=20) for p in pgn.items: page_data = { 'title': p.get('title') or _get_first_line_title(p), 'author': p.get('author') or default_author, 'timestamp': p.get('timestamp'), 'tags': p.get('tags', []), 'category': p.get('category'), 'source': source_name, 'url': url_for('.edit_page', url=p['rel_url']) } data['pages'].append(page_data) prev_page_url = None if pgn.prev_page_number: prev_page_url = url_for('.list_source', source_name=source_name, page_num=pgn.prev_page_number) next_page_url = None if pgn.next_page_number: next_page_url = url_for('.list_source', source_name=source_name, page_num=pgn.next_page_number) page_urls = [] for i in pgn.all_page_numbers(7): url = None if i != page_num: url = url_for('.list_source', source_name=source_name, page_num=i) page_urls.append({'num': i, 'url': url}) data['pagination'] = { 'prev_page': prev_page_url, 'next_page': next_page_url, 'nums': page_urls } with_menu_context(data) return render_template('list_source.html', **data)
def _paginate(self, value, items_per_page=5): ctx = self.app.env.render_ctx_stack.current_ctx if ctx is None or ctx.page is None: raise Exception("Can't paginate when no page has been pushed " "on the execution stack.") return Paginator(value, ctx.page, ctx.sub_num, items_per_page=items_per_page)
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
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
def list_source(source_name, page_num): site = g.site.piecrust_app source = site.getSource(source_name) if source is None: abort(400) i = 0 data = {'title': "List %s" % source_name} data['pages'] = [] pgn = Paginator(None, source, page_num=page_num, items_per_page=20) for p in pgn.items: page_data = { 'title': p['title'], 'slug': p['slug'], 'source': source_name, 'url': url_for('edit_page', slug=p['slug']), 'text': text_preview(html_to_text(p['content']), length=300)} data['pages'].append(page_data) prev_page_url = None if pgn.prev_page_number: prev_page_url = url_for( 'list_source', source_name=source_name, page_num=pgn.prev_page_number) next_page_url = None if pgn.next_page_number: next_page_url = url_for( 'list_source', source_name=source_name, page_num=pgn.next_page_number) page_urls = [] for i in pgn.all_page_numbers(7): url = None if i != page_num: url = url_for('list_source', source_name=source_name, page_num=i) page_urls.append({'num': i, 'url': url}) data['pagination'] = { 'prev_page': prev_page_url, 'next_page': next_page_url, 'nums': page_urls } with_menu_context(data) return render_template('list_source.html', **data)
def _paginate(self, value, items_per_page=5): cpi = self.app.env.exec_info_stack.current_page_info if cpi is None or cpi.page is None or cpi.render_ctx is None: raise Exception("Can't paginate when no page has been pushed " "on the execution stack.") first_uri, _ = split_sub_uri(self.app, cpi.render_ctx.uri) return Paginator(cpi.page, value, page_num=cpi.render_ctx.page_num, items_per_page=items_per_page)
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
def test_paginator(uri, page_num, count): def _get_mock_uri(sub_num): res = uri if sub_num > 1: if res != '' and not res.endswith('/'): res += '/' res += '%d' % sub_num return res source = MockSource(count) p = Paginator(None, source, page_num=page_num) p._getPageUri = _get_mock_uri if count <= 5: # All posts fit on the page assert p.prev_page_number is None assert p.prev_page is None assert p.this_page_number == 1 assert p.this_page == uri assert p.next_page_number is None assert p.next_page is None elif page_num == 1: # First page in many assert p.prev_page_number is None assert p.prev_page is None assert p.this_page_number == 1 assert p.this_page == uri assert p.next_page_number == 2 np = '2' if uri == '' else (uri + '/2') assert p.next_page == np else: # Page in the middle of it all assert p.prev_page_number == page_num - 1 if page_num == 2: assert p.prev_page == uri else: pp = str(page_num - 1) if uri == '' else ( '%s/%d' % (uri, page_num - 1)) assert p.prev_page == pp assert p.this_page_number == page_num tp = str(page_num) if uri == '' else ( '%s/%d' % (uri, page_num)) assert p.this_page == tp if page_num * 5 > count: assert p.next_page_number is None assert p.next_page is None else: assert p.next_page_number == page_num + 1 np = str(page_num + 1) if uri == '' else ( '%s/%d' % (uri, page_num + 1)) assert p.next_page == np assert p.total_post_count == count page_count = math.ceil(count / 5.0) assert p.total_page_count == page_count assert p.all_page_numbers() == list(range(1, page_count + 1)) for radius in range(1, 8): width = radius * 2 + 1 if page_count == 0: nums = [] else: nums = list(filter( lambda i: i >= 1 and i <= page_count, range(page_num - radius, page_num + radius + 1))) if len(nums) < width: to_add = width - len(nums) if nums[0] > 1: to_add = min(to_add, nums[0] - 1) nums = list(range(1, to_add + 1)) + nums else: to_add = min(to_add, page_count - nums[-1]) nums = nums + list(range(nums[-1] + 1, nums[-1] + to_add + 1)) assert nums == p.all_page_numbers(radius) itp = count if count > 5: if page_num * 5 < count: itp = 5 else: itp = count % 5 assert p.items_this_page == itp indices = list(range(count)) indices = indices[(page_num - 1) * 5 : (page_num - 1) * 5 + itp] expected = list(['item %d' % i for i in indices]) items = list(p.items) assert items == expected
def test_paginator(uri, page_num, count): def _get_mock_uri(sub_num): res = uri if sub_num > 1: if res != '' and not res.endswith('/'): res += '/' res += '%d' % sub_num return res source = MockSource(count) p = Paginator(source, None, page_num) p._items_per_page = 5 p._getPageUri = _get_mock_uri if count <= 5: # All posts fit on the page assert p.prev_page_number is None assert p.prev_page is None assert p.this_page_number == 1 assert p.this_page == uri assert p.next_page_number is None assert p.next_page is None elif page_num == 1: # First page in many assert p.prev_page_number is None assert p.prev_page is None assert p.this_page_number == 1 assert p.this_page == uri assert p.next_page_number == 2 np = '2' if uri == '' else (uri + '/2') assert p.next_page == np else: # Page in the middle of it all assert p.prev_page_number == page_num - 1 if page_num == 2: assert p.prev_page == uri else: pp = str(page_num - 1) if uri == '' else ('%s/%d' % (uri, page_num - 1)) assert p.prev_page == pp assert p.this_page_number == page_num tp = str(page_num) if uri == '' else ('%s/%d' % (uri, page_num)) assert p.this_page == tp if page_num * 5 > count: assert p.next_page_number is None assert p.next_page is None else: assert p.next_page_number == page_num + 1 np = str(page_num + 1) if uri == '' else ('%s/%d' % (uri, page_num + 1)) assert p.next_page == np assert p.total_post_count == count page_count = math.ceil(count / 5.0) assert p.total_page_count == page_count assert p.all_page_numbers() == list(range(1, page_count + 1)) for radius in range(1, 8): width = radius * 2 + 1 if page_count == 0: nums = [] else: nums = list( filter(lambda i: i >= 1 and i <= page_count, range(page_num - radius, page_num + radius + 1))) if len(nums) < width: to_add = width - len(nums) if nums[0] > 1: to_add = min(to_add, nums[0] - 1) nums = list(range(1, to_add + 1)) + nums else: to_add = min(to_add, page_count - nums[-1]) nums = nums + list( range(nums[-1] + 1, nums[-1] + to_add + 1)) assert nums == p.all_page_numbers(radius) itp = count if count > 5: if page_num * 5 < count: itp = 5 else: itp = count % 5 assert p.items_this_page == itp indices = list(range(count)) indices = indices[(page_num - 1) * 5:(page_num - 1) * 5 + itp] expected = list(['item %d' % i for i in indices]) items = list(p.items) assert items == expected